Wrapper procedure for sort function failing because of argument

191 Views Asked by At

I'm working through SICP, and for one of the exercises I need to create a list of 2 of the 3 larger Numbers in the list. I'm trying to use sort function, but when I use it inside of a function, I'm getting an error:

The object z, passed as the first argument to integer-less?, is not the correct type.

Function is:

(define (myList x y z) 
    (drop (sort '(x y z) <) 1))

It works fine if I run the second line in the interpreter (substituting variables for actual values), but when I try to use the function it blows up. I'm new to scheme/lisps, so I'm not that familiar with how lists work, but I'm guessing it has something to do with that. I know lisp uses linked lists, so I'm wondering if it has something to do with it reaching the last element and not knowing what to do after that.

Any help would be appreciated!

Edit:

I just tried running:

(define x 4)
(define y 10)
(define z 2)
(drop (sort '(x y z) <) 1)

and got the same error.

2

There are 2 best solutions below

1
On BEST ANSWER

'(x y z) is a list containing three symbols, x, y, and z. It's the same as (list 'x 'y 'z).

What you need to use, instead, is (list x y z).

The reason using '(4 10 2) (for example) works is that numbers are "self-evaluating". That means (list '4 '10 '2) is the same as (list 4 10 2).

0
On

In addition to @Chris' explanation, here's an easier way which will work for any number of parameters:

(define (myList . lst)  
  (drop (sort lst <) 1))

Testing:

> (myList 10 1 5)
'(5 10)
> (myList 10 1 5 8)
'(5 8 10)
> (myList 10 1 5 8 -1)
'(1 5 8 10)