I am working on this school assignment and i kept running into this issue in the last line of the code below. The error is list-ref: contract violation. So, i was hoping that i can get help rewriting that last line so that i am able to use recursion because i am not allowed to use loops or begin as part of the assignment.
(define (second lst)
(cond [(and(list? lst)(not(null? lst)) (not(null? (cdr lst)))) (first (rest lst))]
[else '()]))
(define (extract-helper lst size counter)
(cond [(equal? size counter) counter]
[else (second (list-ref lst counter))(extract-helper (list-ref lst (+ counter 1)) size (+ counter 1))]))
Related
Learning some Scheme/Racket, so give me some leeway.
Currently trying to find the max value when given a list without using the built-in max() function.
Current Code:
#lang racket
(provide max-num)
(define (max-num lst)
(define (helper lst max)
(displayln lst)
(displayln max)
(displayln " ")
(when (null? max) ; first run
(helper (cdr lst) (car lst)))
(if (null? lst)
max ; then end
(if (> (car lst) max) ; else compare
(helper (cdr lst) (car lst)) ; then update max
(helper (cdr lst) max)))) ; else keep max
(if (null? lst)
#f ; then Error
(helper lst '())) ; else run helper
)
(max-num '())
(max-num '(1 5 2 4 3))
Output via DrRacket:
As far as I can tell, the displayln outputs tell me I am on the right track. However, it ends up with a contract violation real? error instead of returning the max value.
I'm guessing that the (if (null? lst)) doesn't want to return "max" at the end and instead pushes towards the else branch despite the list being empty. I've looked around and debugged for about an hour now to no avail. Any help would be greatly appreciated.
You have to know that when you do:
(when test
do-something)
do-something-else
It will always do-something-else regardless if test is true or not. SO what is happening is that the first round max is null? and it does (helper (cdr lst) (car lst))) and that returns the answer. Then it discard that answer and continue to the if with max being null? and it finally fails when it does (> (car lst) max) since a null? is not a number. The error message says it expected a real? but it got the initial value '().
So to hint you on your way you should have one expression in addition to the local definitions eg.
(if test1
result1
(if test2
result2
alternative2))
or
(cond (test1 result1)
(test2 result2)
(else alternative2))
And of course since you know the argument is not null? you could just call (helper (cdr lst) (car lst)) instead of passing the empty list and remove the when entirely. when and unless are for side effects and not really for good functional Scheme style.
I wanted to make a procedure that destructively increments the odd numbers of a list. I thought I'd recurse through it and just use 'set-car!' whenever 'odd?' was true.
Here is the code:
(define (test lst)
(cond ((null? lst) lst)
((odd? (car lst)) (set-car! lst (+ (car lst) 1))
(test (cdr lst)))
(else (test (cdr lst)))))
I'm not sure why it is not working, I guess I do not understand set-car! and set-cdr!
Could someone explain? Thank you.
The problem might be with your interpreter, or the language in which you're defining the procedure, or the way you're calling it. For instance, in DrRacket this works fine:
#lang r5rs
(define (test lst)
(cond ((null? lst) lst) ; this is the '() returned by the recursion
((odd? (car lst)) (set-car! lst (+ (car lst) 1))
(test (cdr lst)))
(else (test (cdr lst)))))
Bear in mind that your procedure will return an empty list, because that's the base case of the recursion and this is a tail-recursive procedure, which returns the value at the base case as its final result. But don't worry, the input list was modified in-place, you can check it after the procedure returns its value.
(define lst (list 1 2 3 4))
(display (test lst))
=> ()
(display lst)
=> (2 2 4 4)
See how mutability is confusing? a procedure is returning one value, but its input was modified and has a different value now. That's one of the reasons why functional programming (which favors immutable data) is cleaner and simpler to understand, and also demonstrates why is a bad idea to mutate input parameters inside a procedure ;)
If you absolutely want the procedure to return the mutated list, do as #sepp2k suggests, but remember - the input list was modified and in fact, is the same list that is returned as a result:
(define (test lst)
(cond ((null? lst) lst)
((odd? (car lst)) (set-car! lst (+ (car lst) 1))
(test (cdr lst))
lst) ; add this line
(else (test (cdr lst)))))
See for yourself:
(define lst (list 1 2 3 4))
(display (test lst))
=> (2 2 4 4)
(display lst)
=> (2 2 4 4)
was expecting the have the list containing (2 2 4 4) returned
The way you defined your function, it will return an empty list when called on the empty list and the result of the recursion in all other cases. So since the only base case is the empty list, you'll always return the empty list.
If you want to return the modified list, you'll need to do that after the recursion. That is after (test (cdr lst)), add lst to return the value of lst.
You are using set-car! correct. Here is how you tell it's working:
(define (test lst)
(cond ((null? lst) lst)
((odd? (car lst)) (set-car! lst (+ (car lst) 1))
(test (cdr lst)))
(else (test (cdr lst)))))
(define test-list (list 1 2 3 4))
(test test-list)
test-list ; ==> (2 2 4 4)
Your expectation that the function will return the modified list is wrong. To do that you need the first recursion step to return the argument. You need to wrap it:
(define (inc-odds lst)
(define (test lst)
(cond ((null? lst) lst)
((odd? (car lst)) (set-car! lst (+ (car lst) 1))
(test (cdr lst)))
(else (test (cdr lst)))))
(inc-odds lst) ; do the stuff
lst) ; return the list
(inc-odds (list 1 2 3 4)) ; ==> (2 2 4 4)
(inc-odds '(1 2 3 4)) ; ==> "pigs flying"
Notice the last one. In the RNRS upto R5RS passing a quoted literal to set-car! would produce an undefined behaviour which means anything is ok because technically the code isn't Scheme. In R6RS it's required to raise an exception.
This is the code I have so far. I believe that I'm close, but what returns is just #procedure, acknowledging that a procedure has created, it does not return the list with the newly added element. I have been working on this for hours and I am at a loss of where I have gone wrong.
(define (add-into new p lst)
(cond ((null? lst)
(cons new lst)
((eq? p 0)
(cons new lst)
(else
(cons (car lst) (add-into (- p 1) new (cdr l))))))))
You have parentheses problems, in one place you passed l instead of lst and the order of the parameters is incorrect when doing the recursive call. This should fix the errors:
(define (add-into new p lst)
(cond ((null? lst)
(cons new lst))
((= p 0)
(cons new lst))
(else
(cons (car lst) (add-into new (- p 1) (cdr lst))))))
Just pasting the code into DrRacket and pressing CTRL+i and I get this:
(define (add-into new p lst)
(cond ((null? lst)
(cons new lst)
((eq? p 0)
(cons new lst)
(else
(cons (car lst) (add-into (- p 1) new (cdr l))))))))
Notice that you only have one term. If (null? lst) is #f there are no more terms in the cond so it will evaluate to a implementation defined value as it is it is undefined in the specification. In #lang racket that value is the same as returned if you evaluate (void)
To fix it you need to close the terms in the cond so that you have 3 terms instead of one. You should have seen this when you press enter that the parentheses didn't align to the previous term. If you want to use another editor you should get one that aligns and indent lisp syntax as writing lisp without is slightly painful.
I have a recursive code and need to terminate it when condition is fullfilled. I am capable of display the list when the condition, but then there are another calls on stack that I don't need to process that doesn't let me to return the list.
A variation of Sylwester's solution:
(define (example n)
(call-with-current-continuation
(lambda (return)
(let loop ([n 0])
(if (= n 5) ; done
(return 'the-result)
(loop (+ n 1)))))))
(example 10)
Using the continuation in this way allows one to use an escape continuation instead of a full continuation with call/ec (if your implementation has escape continuations).
The best way to do it is by using an accumulator. Aborting is then just not recursing.
(define (copy-unless-contains-5 lst)
(define (helper lst acc)
(cond
((null? lst) (reverse acc))
((equal? (car lst) 5) #f)
(else (helper (cdr lst) (cons (car lst) acc)))))
(helper lst '()))
If you are recursing with a continuation and that is the optimum way of doing it, then call-with-current-continuation can give you a way to abort waiting continuations and choose what to return.
(define (copy-unless-contains-5 lst)
(call-with-current-continuation
(lambda (abort)
(define (helper lst)
(cond
((null? lst) '())
((equal? (car lst) 5) (abort #f))
(else (cons (car lst) (helper (cdr lst))))))
(helper lst))))
Needless to say this last version is overly complicated. Both work the same:
(copy-unless-contains-5 '(1 2 3 4)) ; ==> (1 2 3 4)
(copy-unless-contains-5 '(1 2 5 3 4)) ; ==> #f
I'm new to Scheme, so can anyone give me an example? There's no local variable in Scheme, so how can I keep track of the number of zeros that being encountered.
I tried
#lang scheme
(define zeroes
(lambda (ll)
(cond ((null? ll)
0)
(else (= 0 (car ll))))
(zeroes (cdr ll))
)
)
But the compiler complained:
cdr: expects argument of type <pair>; given ()
Thanks,
Here's my solution (since the OP's already posted theirs):
(define (count-zeroes lst)
(let loop ((lst lst)
(count 0))
(cond ((null? lst) count)
((zero? (car lst)) (loop (cdr lst) (+ count 1)))
(else (loop (cdr lst) count)))))
Of course, no treatment of this subject can be considered complete without talking about fold, which is usually used to "summarise" a list down to a single object (like we're doing for this question):
(define (count-zeroes lst)
(fold (lambda (elem count)
(if (zero? elem) (+ count 1) count))
0 lst))
Just figured out the solution,
(define count
(lambda (lst)
(cond ((null? lst) 0)
((= 0 (car lst)) (+ 1 (count (cdr lst))))
(else (+ 0 (count (cdr lst))))
)
)
)
I'm keeping this at a hint level for now.
Your function is doing two things. First it computes 0 if its argument is an empty list, or #t or #f if its argument is a list that begins with 0 or not. Then it throws that result out and calls itself recursively on the rest of the list.
You're going to have to do two things to make this work: 1) combine the results of the individual zero tests somehow (for a thought experiment, look at your code; how would it ever return the value 2 if the list had two zeroes?); 2) "bottom out" successfully when it calls itself recursively on an empty list.