Get 7 not (7) from the list?

Here is my code:

  (define step1_list1 '(1 3 (5 7) 9))

  (car (cdr (cdr (step1_list1))))


   (define step1_list2 '((7)))

   (car (step1_list2))


   (define step1_list3 '(1 (2 (3 (4 (5 (6 7)))))))

   (car (cdr (cdr (cdr (cdr (cdr step1_list3))))))

  ))

Running this code causes an error:

(1 3 (5 7) 9) is not applicable

What is the problem?

+5
source share
2 answers

Start small.

(define mylist '(1 2 3))

(display mylist)

(display (car mylist))

(display (car (mylist)))

Run each of them in turn and see what you get at each step. Once you understand why you get the conclusion you are making, you should be able to fix the code in your question.

+6
source

In the diagram (without quotes), parentheses indicate the function of the application. So (car (step1_list2)) tries to execute step1_list2 as a procedure (and then take the result car). Instead, you want:

(car step1_list2)
+2
source

All Articles