0

Hi,
I am a scheme newbie so any help or suggestion is appreciated. The follwoing function with the input '((kangaroo Australia) (Racoon USA) (Tiger Bangladesh)) should return '(kangaroo Racoon Tiger) but instead it returns Error: attempt to call a non-procedure
('((kangaroo australia) (racoon usa) (tiger bangladesh)))

What am I doing wrong?

;L is is the list of animals and countries
(define (animalList L)
 (cond ((null? L) '())

       ;caar L gets the first animal then the recursive
       ;call gets the rest of the animals
       (cons ((caar(L)) (animalList(cdr(L)))))
  )
)

Kindly help

2
Contributors
1
Reply
4
Views
10 Years
Discussion Span
Last Post by jimFan
0
;L is is the list of animals and countries
(define (animalList L)
 (cond ((null? L) '())

       ;caar L gets the first animal then the recursive
       ;call gets the rest of the animals
       (cons ((caar(L)) (animalList(cdr(L))))) ; <= LOOK AT HERE
  )
)

You have missed out the "else" in the indicated line. Recall the syntax of cond:

(cond ( (condition1) (consequence1) )
( (condition2) (consequence2) )
...... )

So you may want to change it to:

(cond ( (null? L) '() )
( else (...) )) ; #t would work too

More over the call of caar on L looks strange. Should it be:

(caar L)

instead of

((caar (L))

Similar mistake is found in the recursive call to animalList. Try fixing it :cheesy:

Hope this could help.


Jim

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.