assasinx
assasinx

Reputation: 25

Racket - Map a function which takes an argument

I wanna map a function which takes an argument:

(map best-play (cdr tree) true) ;true is the argument I wanna pass

Is it possible?

Upvotes: 1

Views: 724

Answers (1)

Alexis King
Alexis King

Reputation: 43842

Yes. You can use a function called curry (or its sibling, curryr), which permits partial application, allowing you to pass in arguments without calling the function to yield a new function.

To understand that, consider this example.

> (define add2 (curry + 2))
> (add2 1)
3
> (add2 2)
4

Effectively, calling curry on + and passing a single argument creates a new function which is equivalent to this:

(lambda (x) (+ 2 x))

The function curryr is similar, but it passes arguments starting from the right, rather than the left. This is useful in functions where order matters.

> (define sub2 (curryr - 2))
> (sub2 4)
2

You can use these functions to perform the mapping you want. If the true argument comes second, then you'd want to use curryr, like this:

(map (curryr best-play true) (cdr tree))

Upvotes: 3

Related Questions