(define (comp f g)
(lambda (x)(f (g x))))
(define (complement f) (cond ((equal? (comp f (lambda (g) g)) #t) #f)
((equal? (comp f (lambda (g) g)) #f) #t)))
((complement odd?)2)
It keeps saying that ((complement odd?)2) is not a procedure. I'm not sure how to fix it.
When you run this code you'll see that
((complement odd?) 2)
is red in the definitions and you get the following error:So that would mean
(complement odd?)
doesn't return a procedure but the value#<void>
. Lets try that:If you really want to see it use it somewhere:
That means you are not handling all your possible checks in the
cond
incomplement
and I see why.. Have you triedcomp
?Surely enough the use of
comp
becomes a procedure. Not strange since the body has one lambda form that indicates the return would be a procedure. It will never be#t
or#f
When you don't have anelse
(default) term for when neither of your predicates became truecond
returns an implementation specific defauls value. In Racket this is#<void>
which is supressed by the REPL, but in other implementations it can bebanana
or whatever the implementers want it to be so you should always have aelse
clause. If you don't think you need it then do(else (error "should never happen"))
and you're good to go. (try it)The consequences in your
cond
are#t
and#f
. That means that if your code sould have worked you would have got this error message instead:For each place your return smething that is not a procedure that would be an error since you are using the result as a procedure. You need to change your implementation such that it returns a procedure always.
So, this is the answer to how to find “not a procedure” error. It's not the answer to how to fix your procedure but since this is the simplest procedure in the world I'll add it. You have a procedure
comp
that takes two procedures and return the composition of the two. eg. if you want add2 you can(define add2 (comp add1 add1))
. Thecomplement
must be that the false value#f
turns#t
while all the true values turn#f
.not
does this so the composition ofnot
andodd?
would become the procedure that works the same aseven?
:Since we don't mutate anything you can use substitution method to check what this does: