i need to create this procedures: my-cons, my-car, my-cdr in Scheme.
It should work like this:
(define p1 (my-cons 3 8))
(p1 #t)
3
(p1 #f)
8
(my-car p1)
3
(my-cdr p1)
8
now, I have only this:
(define my-cons
(lambda (x y)
(cons x y)
(let ((a (car (cons x y))))
(lambda (a) (if (equal? a #f) y x)))))
but in this code i can't apply my-cons or my-cdr on defined p1 Can someone help me with this?
First, there's some extraneous code in my-cons
which doesn't seem to belong here. This is enough:
(define my-cons
(lambda (x y)
(lambda (a)
(if a x y))))
(Also, you don't need to compare a boolean value with #t
or #f
— it's usable in if
as it is.)
Now you have my-cons
that returns a function which returns either x
or y
depending on its arugment. You can use that when implementing my-car
and my-cdr
:
(define my-car
(lambda (c)
(c #t)))
(define my-cdr
(lambda (c)
(c #f)))