tags:

views:

191

answers:

6

Hi Im facing a problem with the car and cdr functions

for example:

first I defined a list called it x

(define x (a (bc) d ( (ef) g ) ))

so x now is equal to (a (bc) d ( (ef) g ) )

now for example I need to get the g from this list using only car and cdr (!! noshortcuts as caddr cddr !!) the correct answer is:

(car(cdr(car(cdr(cdr(cdr x))))))

BUT how ? :-( I work according to the rules (the car gives the head of list and cdr gives the tail)

and instead of getting the answer above I keep reaching wrong answers. Can any one help me in understanding this ... give me step or a way to solve it step by step

Thanks in advance. I'm really sick of Scheme.

A: 

Have you tried using a REPL (read-eval-print-loop) such as csi? That way you can work on this interactively, which will make it easier for you to learn and work through this (and other) problems using Scheme.

Justin Ethier
+1  A: 
(cdr x) = ((bc) d ( (ef) g ) )
(cdr(cdr x)) = (d ( (ef) g ) )
(cdr(cdr(cdr x))) = (( (ef) g ) )
(car(cdr(cdr(cdr x)))) = ( (ef) g )
(cdr(car(cdr(cdr(cdr x))))) = (g)
(car(cdr(car(cdr(cdr(cdr x)))))) = g
Maciej Hehl
+14  A: 

Try to do it step by step:

  • cdr yields the list without the first element
  • car yields the first element of a list

                                  x         is  (a (bc) d ( (ef) g ))
                             (cdr x)        is  (  (bc) d ( (ef) g ))
                        (cdr (cdr x))       is  (       d ( (ef) g ))
                   (cdr (cdr (cdr x)))      is  (         ( (ef) g ))
              (car (cdr (cdr (cdr x))))     is            ( (ef) g )
         (cdr (car (cdr (cdr (cdr x)))))    is            (      g )
    (car (cdr (car (cdr (cdr (cdr x))))))   is                   g
    
Curd
@Curd|| so in the 5th line I used car to remove the outer parentheses (or the list) ? if so, Why i didnt remove the second parentheses on line 5 again by using another car before reaching the inside and using cdr ( i mean why we postpond the car to line 7 ?
kristian Roger
car at line 5 (i.e. `(car ( (ef) g ))` would leave you with `(ef)`. If you're looking to get g, you mustn't throw it away. ;) car doesn't remove the parentheses, it returns the first item in the following list. If the list only contains one item, then you get that item, no longer in a list.
dash-tom-bang
A: 

do it iteratively. Also, realize that scheme always looks backwards.

(cdr x) = ( (b c) ...)
(cdr (cdr (cdr x))) = (( (ef) g))
(car (cdr (cdr (cdr x)))) = ((ef) g)
(cdr (car (cdr (cdr (cdr x))))) = (g)
(car (cdr (car (cdr (cdr (cdr x)))))) = 'g

hope that helps

Brian Postow
+1  A: 

do the transforms one at a time. cdr gives you a list without the first element, car gives you the first element.

(cdr (a (bc) d ( (ef) g ) )) -> ( (bc) d ( (ef) g ) )
(cdr ( (bc) d ( (ef) g ) ))  -> ( d ( (ef) g ) )
(cdr ( d ( (ef) g ) ))       -> ( ( (ef) g ) )
(car ( ( (ef) g ) ))         -> ( (ef) g )  <- pulls the first element out, which happens to be a list itself
(cdr ( (ef) g ))             -> (g)
(car (g))                    -> 'g
dash-tom-bang
+1  A: 

this is easy/compact way to get the value of the list.

(cadr (cadddr x))

by combining repeating functions, you get elegant easy to read statement.

Mohamed