Click to See Complete Forum and Search --> : scheme hw help
Danger Fan
01-29-2002, 06:57 PM
does anyone here know scheme?
Well, I'm lost. If anyone can help, it would be greatly appreciated.
here are my expressions:
(define x (cons 'a (cons 'b (cons 'c empty))))
(define y (cons 'x (cons 'y x)))
now, using only cons, car, cdr, empty, x, and y (no defines or quotes), I need to enter an expression that produces this result:
(cons 'a (cons 'b (cons 'y empty)))
I'm lost :confused:
Strike
01-29-2002, 07:52 PM
I'm gonna try, and I'm gonna cheat and use the "bastardizations" cadr and such.
(cons 'a (cons 'b (cons 'y empty))) -> (a b y)
Well, you need to build the list backwards it seems. The y we can use, so we need the a and b. The b can be gotten as just the cadr of x, and the a is the car of x. So...
(cons (car x) (cons (cadr x) 'y))
> (cons 'a (cons 'b (cons 'y empty)))
(a b y)
> (cons (car x) (cons (cadr x) 'y))
(a b . y)
Note: you can use (cadr y) to get the quoted y if he didn't want you to sort of "cheat" by using the y directly quoted.
bwkaz
01-29-2002, 08:54 PM
mmmmm... Lisp-descended languages..... mmmmmmmm.....
</me snaps out of it>
Sorry, lost it for a bit there.
Yep, Strike pretty much has it. The only thing I'd add is that if Scheme does weird things (I know Lisp does) when trying to cons an atom onto another atom, you may want to replace the quoted y with something like (cons (cadr y) empty).
Of course, that's assuming a couple (pretty reasonable) things:
1) empty in Scheme is the equivalent of nil in Lisp
2) define in Scheme is the same as setf in Lisp
Danger Fan
01-29-2002, 09:04 PM
Originally posted by bwkaz:
<STRONG>mmmmm... Lisp-descended languages..... mmmmmmmm.....
</me snaps out of it>
Sorry, lost it for a bit there.
Yep, Strike pretty much has it. The only thing I'd add is that if Scheme does weird things (I know Lisp does) when trying to cons an atom onto another atom, you may want to replace the quoted y with something like (cons (cadr y) empty).
Of course, that's assuming a couple (pretty reasonable) things:
1) empty in Scheme is the equivalent of nil in Lisp
2) define in Scheme is the same as setf in Lisp</STRONG>
MY GOD!!! THAT DID IT!!!! I LOVE YOU GUYS!!!
heh, now on to db homework....