Can anyone clarify what this error means?
cadr: expects argument of type <cadrable value>; given (1)
cadr means car and cdr. (i.e, return the car of the cdr of a list). Both the following expressions have the same effect:
> (car (cdr '(1 2 3 4)))
2
> (cadr '(1 2 3 4))
2
(cadr '(1)) will fail because (cdr '(1)) evaluates to null.
Related
I have the following filter procedure:
; (2) filter
(define (filter test sequence)
; return a list of the elements that pass the predicate test
(let ((elem (if (null? sequence) nil (car sequence)))
(rest (if (null? sequence) nil (cdr sequence))))
(cond ((null? sequence) nil)
((test elem) (cons elem (filter test rest)))
(else (filter test rest)))))
And here would be an example of using it to return the even-numbered elements of a list:
(define even? (lambda (x) (= (modulo x 2) 0)))
(define sequence '(1 2 3 4 5 8 9 11 13 14 15 16 17))
(filter even? sequence)
; (2 4 8 14 16)
Is there a simple way to use the not test to invert the selection? For example, I thought the following might work:
(filter (not even?) sequence)
But it returns an error. I can define odd separately, of course:
(define odd? (lambda (x) (not (even? x))))
But I'm trying not to do this. Is there a way to write the odd procedure without defining it directly, but instead using the not directly like I'm trying to do above?
There is a complement function in Common Lisp that does what I think you are looking for. complement is a higher-order procedure that takes a procedure as its argument, and returns a procedure that takes the same arguments as the input procedure and performs the same actions, but the returned truth value is inverted.
Racket has a similar procedure, negate, and it is easy enough to implement this in Scheme:
(define (complement f)
(lambda xs (not (apply f xs))))
> (filter even? '(1 2 3 4 5))
(2 4)
> (filter (complement even?) '(1 2 3 4 5))
(1 3 5)
> (> 1 2 3 4 5)
#f
> ((complement >) 1 2 3 4 5)
#t
And in Racket:
scratch.rkt> (filter even? '(1 2 3 4 5))
'(2 4)
scratch.rkt> (filter (negate even?) '(1 2 3 4 5))
'(1 3 5)
scratch.rkt> (> 1 2 3 4 5)
#f
scratch.rkt> ((negate >) 1 2 3 4 5)
#t
The general answer to this is to simply compose not and the function you care about. Racket has a compose function which does this, but you can easily write a simple one yourself:
(define (compose-1 . functions)
;; simple-minded compose: each function other than the last must
;; take just one argument; all functions should return just one
;; value.
(define (compose-loop fns)
(cond
((null? fns)
(λ (x) x))
((null? (cdr fns))
(car fns))
(else
(λ (x) ((car fns) ((compose-loop (cdr fns)) x))))))
(compose-loop functions))
Making it efficient and more general takes more work of course.
Then you can define odd? (which is already defined of course):
(define odd? (compose-1 not even)
Or in fact define a more general CL-style complement function:
(define (complement f)
(compose-1 not f))
One option is to write an invert function which will curry things along (so the initial function still accepts one argument) until the final evaluation occurs:
(define invert (lambda (func) (lambda (x) (not (func x)))))
(define sequence '(1 2 3 4 5 6 8 9 11 13 14 15 16 17))
(filter (invert even?) sequence)
; (1 3 5 9 11 13 15 17)
I am trying to write a Racket function with tail recursion, it should return the inverted list but the last element should remain in the last position.
That is, I need to get from the example:
(reversar-lista '(1 2 3 4))
>(3 2 1 4)
Here is what I have so far:
(define (reversar-lista lista)
(define (reversa-aux lista aux)
(if (null? lista) aux
(reversa-aux (cdr lista) (reverse (cons (car lista) aux)))
)
)
(reversa-aux lista '())
)
I get the following output:
(3 1 2 4)
It's possible to solve this question using only built-in procedures, there's no need to implement explicit looping logic:
(define (reversar-lista lista)
(if (null? lista)
'()
(append (reverse (drop-right lista 1))
(take-right lista 1))))
Of course, it's also possible to write a solution by hand - but you have to be careful with the edge cases, in particular watch out for the empty list case.
The main problems with your solution are that you must stop the recursion before the last element, and that you must not reverse the result at every iteration, the list is being built in reverse anyway. This is what I mean:
(define (reversar-lista lista)
(define (reversa-aux lista aux)
(if (null? (cdr lista))
(append aux (list (car lista)))
(reversa-aux (cdr lista) (cons (car lista) aux))))
(if (null? lista)
'()
(reversa-aux lista '())))
Either way, it works as expected:
(reversar-lista '())
=> '()
(reversar-lista '(1))
=> '(1)
(reversar-lista '(1 2))
=> '(1 2)
(reversar-lista '(1 2 3))
=> '(2 1 3)
(reversar-lista '(1 2 3 4))
=> '(3 2 1 4)
I just found out, that:
(null? (car '('() 2 3)))
returns false, and:
(null? (car '(() 2 3)))
returns true.
What's the difference between this two formats?
Remember that 'X is just an abbreviation for the two element list: (quote X).
So (car '(() 2 3)) is an abbreviation for (car (quote (() 2 3))), while (car '('() 2 3))) is an abbreviation for (car (quote ((quote ()) 2 3))).
Since (quote something) evaluates to something, when the system evaluates (car (quote ((quote ()) 2 3))) the first step of evaluation produces: (car ((quote ()) 2 3)). And the car of that list produces its first element, that is (quote ()).
So it is not null, and the result is false.
Hello I am looking at the append function
(define ( append x y )
(if (null? x)
y)
(cons (car x)
(append (cdr x)
y))))
I understand how the list is generated but when the first list x is empty we directly return y,I don't see how we connect it to the first list "x".Does the process go like this (cons a1(cons a2....(cons an y).. )) and how does the program understand to plug in y at (cons an y),Is it because in the end the expression is (cons an-1 ,append (cdr x) y ) and the result of (append (cdr x ),y) is y?
Your function has an error in it such that is in parsing has one closing parens too much in the end. I thin kit's because you close if just after y. Because of that it will always do the last expression and it fails when x is empty.
The correct append looks like this:
(define (append x y)
(if (null? x)
y
(cons (car x)
(append (cdr x)
y))))
I like to explain recursive functions by simplest to general so we start off with the obvious, the base case:
(append '() '(3 4))
This will be #t for x being null? and the result is (3 4). Now lets try with a one element list as x:
(append '(2) '(3 4))
This is #f for x being `null? thus you can substitute it with:
(cons (car '(2))
(append (cdr '(2))
'(3 4)))
We can evaluate the accessors on '(2):
(cons 2 (append '() '(3 4))
Since we did the base case before we know the answer to the append part, which is '(3 4) so we end up with:
(cons 2 '(3 4)) ; ==> (2 3 4)
Lets do a new x:
(append '(1 2) '(3 4))
Here as the previous x is not null? so you substitute with the alternative again:
(cons (car '(1 2))
(append (cdr '(1 2))
'(3 4)))
As the previous time we can evaluate the accessors:
(cons 1
(append '(2)
'(3 4)))
Notice that again we have familiar arguments to append so we could just substitute that with our last result, however I take the step before so you see the pattern you noticed:
(cons 1 (cons 2 (append '() '(3 4)))) ; ==>
(cons 1 (cons 2 '(3 4))) ; ==>
(cons 1 '(2 3 4)) ; ==>
; ==> (1 2 3 4)
So if you have a 12 element x it gets 12 cons nested before hitting the base case and then it evaluates the the inner to the outer since list are always created from end to beginning since the functions need to evaluate their arguments before applying.
Racket is giving me a contract violation for the following code:
(define (fringe x)
(append (car x) (fringe (cdr x))))
Any ideas what's wrong with it?
It happens because (car x) is not returning a list (it's hard to tell for sure without knowing the actual value of x that's rising the error). append is an operation defined between two lists. If you want to add an element at the head of a list, use cons instead of append.
This is what I mean:
(append 1 '(2 3))
=> append: expected argument of type <proper list>; given 1
(append '(1) '(2 3))
=> '(1 2 3)
(cons 1 '(2 3)) ; the recommended way!
=> '(1 2 3)