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

;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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.