• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Any scheme master here?? some help plz ??

TurtleMan

Golden Member
Im writing two scheme , but i run into some problems.. im using Dr.Schme.. and im limited only to use use the following pre-defined Scheme procedures : define, lambda, cond (including the else notation), if, cons, car, cdr, null?, =, equal?, list?, odd?, and, or, not, +, -, *, /, <, <=, >, >=

And i can't figure out how to get this one ..

(calc-depth L) takes a list L and returns its depth. The depth of a list is defined as the maximum level of nesting of a list.

(calc-depth '( )) => 1
(calc-depth '(1 2 3)) => 1
(calc-depth '(1 (2) 3)) => 2
(calc-depth '(1 (2 (3) 4) 5)) => 3


and aslo this one ..

(deep-reverse L) takes a list L and returns the deep reversal of L. The deep reversal of L is a list R that contains all of the elements of L in reverse order. Further, for each element L' in L which is a list, the corresponding element in R is the "deep reversal" of L'.

(deep-reverse '( )) => ( )
(deep-reverse '(a b c)) => (c b a)
(deep-reverse '((a b c) d ((e f) g))) => ((g (f e)) d (c b a))

im totally lost and i tried so many different combo and it still doesn't work .. any help would be appricated ;]
 
try this for the first one

(define (depth ls)
(cond ((null? ls) 1)
((list? (car ls)) (+ (depth (car ls)) (depth (cdr ls)))
(else
(depth (cdr ls)))))))))

my syntax may be a little of... but the idea is there, also I didnt count close parentheses

still working on the second one

EDIT: dammit the indentation is not correct
 
you da bomb !!! it works


(define calc-depth
(lambda (L)
(cond
((null? L) 1)
((list? (car L)) (+ (calc-depth (car L)) (calc-depth (cdr L))))
(else (calc-depth (cdr L))))))


why is this the code so simple yet i was writing like mad lines of code and still get it !! recursive !!
 
I was doing it, and the fourth case which is : (calc-depth '(1 (2 (3) 4) 5)) => 3
just totally throw me off , and yet that is how u suppose to do it !! 🙂
 
eh u know that I was not online when u refreshed the page rite??

ok now Im working on the second one... too lazy yesterday

EDIT: the second one is recursive too
 
ok try this for the second one

(define (deep-reverse L)
(cond ((null? L) nil)
((null? (cdr L)) L)
((listp? (car L)) (list (deep-reverse (cdr L)) (deep-reverse (car L))))
(else (cons (deep-reverse (cdr L)) (car L)))))))


If the result is kinda funny (the parentheses are wrong) try playing with changing the list and the cons in line 4 and 5
 
*Bowinng dow to z0mb13 *


you the best man !! I stuggle so bad on the deep-reverse ,
i even try to write a helper function it but still didn't work ...

you are my new idol now ^__^ thanks a lot !!!
 
scheme and lisp is powerful in that aspect: can do a lot of stuff with few line of code... and also it is recursive by nature (well maybe not.. but just the way I was taught at CAL)

EDIT: thanks for the praise.. I hope u doin well in the class!! 🙂


 
Originally posted by: z0mb13
scheme and lisp is powerful in that aspect: can do a lot of stuff with few line of code... and also it is recursive by nature (well maybe not.. but just the way I was taught at CAL)

EDIT: thanks for the praise.. I hope u doin well in the class!! 🙂


lol i almost pass the class, i think bc i missed 1 schme probelm that's why i can't advance to the next class :]
thats why im here to check it out again !!
 
Let me guess....UCI ICS major? Class : ICS 23....i had to do that same exact problem...lol...<Goes back to check his old program>
 
Back
Top