Reputation: 312
it's my first time posting and i have a doubt on scheme. I have to remove all occurrences of an element from a list, passed both as an arguments, when entering parameters like that:
]=> (rmobject '(1 2 3 5 0 2 3 5 3) 3)
I'm getting an error:
The object (3 5 3 2 3 6 3) is not applicable
I suppose it's because of the second lambda, that is not working properly but why?
(define (rmobject list1 obj)
(if (null? list1)
'()
(if (= obj (car list1))
((lambda (list1) (cdr list1)) list1)
((lambda (list1) (list1)) list1)))
(cons (car list1) (rmobject (cdr list1) obj)))
I rewrote the code and this works properly on removing the elements but the proper doesn't, and both are suppose the same. Thanks in advance
(define (rmobject list1 obj)
(if (null? list1)
'()
(if (= obj (car list1))
(rmobject (cdr list1) obj)
(cons (car list1) (rmobject (cdr list1) obj)))))
Upvotes: 1
Views: 4721
Reputation: 236004
The first version in your code doesn't make much sense. Why use lambda
s in this way? you're supposed to recursively call the same procedure being defined, not creating a one-shot anonymous procedure, that won't work for solving the problem at hand. And this part: (list1)
is causing the error The object is not applicable
: you're trying to invoke the list as if it were a procedure - because it's surrounded by parenthesis. Remember that in Scheme, a syntax such as this one: (foo)
indicates that the foo
procedure is to be called.
The second version of your code is fine, that's the simple way to implement a remove-all
procedure. A bit of nitpicking, though: when you find yourself nesting if
s, it's a sure sign that a cond
would be more appropriate. Also notice that it's a good idea to use equal?
instead of =
, in that way your procedure will work for more than just numbers:
(define (rmobject list1 obj)
(cond ((null? list1)
'())
((equal? obj (car list1))
(rmobject (cdr list1) obj))
(else
(cons (car list1)
(rmobject (cdr list1) obj)))))
For future reference: the procedure you're implementing it's generally included as part of the interpreter. For example, in Racket we have remove*
, which uses equal?
as the default procedure for testing equality:
(define (rmobject list1 obj)
(remove* (list obj) list1))
Also, you can use filter
as in @Maxwell's answer. Another way to write it:
(define (rmobject list1 obj)
(filter (negate (curry equal? obj)) list1))
Anyway, this works:
(rmobject '(1 2 3 5 0 2 3 5 3) 3)
=> '(1 2 5 0 2 5)
Upvotes: 2
Reputation: 3644
The problem is that in the line ((lambda (list1) (list1)) list1)))
you compose a function that accepts an argument (list1
), which you then attempt to call as a function. Because the function is actually passed a list instead, the interpreter exits with an error.
As you discovered in your second attempt, composing functions via lambda is not necessary or even coherent using the algorithm you came up with. Even so, there are many techniques for this that are much simpler than the one you use in your second attempt. I will demonstrate one that actually does use lambda so you can see how it works. The alternative technique I am referring to relies on filter
and works like so:
(define (rmobject list1 obj)
(filter (lambda (x) (not (equal? x obj))) list1))
Upvotes: 1