Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get 7 not (7) out of the list?

Tags:

scheme

Here's 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?

like image 427
Lei0417 Avatar asked Nov 04 '11 01:11

Lei0417


2 Answers

Start small.

(define mylist '(1 2 3))

(display mylist)

(display (car mylist))

(display (car (mylist)))

Run each of those in turn, and see what you get at each step. Once you understand why you get the output you do, then you should be able to fix the code in your question.

like image 94
Greg Hewgill Avatar answered Nov 13 '22 14:11

Greg Hewgill


In Scheme, (non-quoted) parentheses mean function application. So (car (step1_list2)) tries to execute step1_list2 as a procedure (and then take the car of the result). Instead, you want:

(car step1_list2)
like image 2
Ord Avatar answered Nov 13 '22 14:11

Ord