priya
priya

Reputation: 129

Racket Code: Higher-Order Functions

I'm trying to implement higher level functions in my Racket code, specifically with regards to this function:

(define (indivisible e L)
  (map (remove 0 ((map ((lambda (x y) (modulo x y))) L e)))))

Essentially, I'm trying to remove all the elements that are divisible by e from the list. However, it keeps giving me an error that says that "the expected number of arguments did not match the given number (0 vs 2)". Why is this so?

Upvotes: 0

Views: 423

Answers (1)

Sylwester
Sylwester

Reputation: 48745

Several places you have two sets of parentheses. Unless the parentheses are a part of a special form or macro, eg. let, it represent an application. Ie.

((lambda (x y) (modulo x y)))

Here the form (lambda ...) is evaluated and become a function. The second set of parentheses calls this function with no arguments. Since you have two arguments, x and y and not supplying any in your application it signals an error.

Another place where you do the same is around (map ....). Since I know map always evaluates to a list or null it looks kind of strange that you call it as a function ((map ...)).

If you are more familiar with algol languages like python, what you are doing is like someFunc(arg1 args2)() where you clearly see someFunc needs to return a function wince it's immediately called afterwards. The same in Scheme looks like ((some-func arg1 arg2)).

remove removes the first argument from the second argument list. It does not return a function so the outer map won't work.

To solve this I think you are looking for filter. You only need to make a predicate that is #f for the elements you don't want and you're done.

Upvotes: 1

Related Questions