Function with fixed parameter

Discussion of Scheme and Racket
Post Reply
blooper
Posts: 6
Joined: Thu May 14, 2009 10:17 pm

Function with fixed parameter

Post by blooper » Wed May 20, 2009 7:07 am

How one can write in Scheme function Fix-first-parameter and Fix-second-parameter that for given function F of n arguments (parameters) and given number A, will return a new function (lets donote if by G) of (n-1) arguments (parameters), such that:

G(x_1, x_2, x_3, ..., x_n-1) = F(a, x_1, x_2, x_3, ..., x_n-1) (in case of Fix-first-parameter function),
G(x_1, x_2, x_3, ..., x_n-1) = F(x_1, a, x_2, x_3, ..., x_n-1) (in case of Fix-second-parameter function).

For example:

1.

Code: Select all

(define function1 (lambda (x y z) (+ x (* 2 y) (* 3 z))))
(display ((Fix-first-parameter function1 1) 2 3))
should return:

Code: Select all

14
2.

Code: Select all

(define function1 (lambda (x y z) (+ x (* 2 y) (* 3 z))))
(display ((Fix-second-parameter function1 1) 2 3 ))
should return:

Code: Select all

13
3.

Code: Select all

(define function2 (lambda (x y z s) (cons (+ x (* 2 y)) (+ z (* 4 s)))))
(display ((Fix-second-parameter function2 2) 1 3 2))
should return:

Code: Select all

(5 . 11)
---

Very important is fact, that arguments x_1, ..., x_n-1 are given not as a list by directly. I don't know how to extract (currying?) them and write a function that will work properly on the examples given above. I hope that some of you will help me. :)

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Function with fixed parameter

Post by gugamilare » Wed May 20, 2009 8:33 am

blooper wrote:Very important is fact, that arguments x_1, ..., x_n-1 are given not as a list by directly. I don't know how to extract (currying?) them and write a function that will work properly on the examples given above. I hope that some of you will help me. :)
To create a function that receives an arbitrary number of arguments, you need to use the directive &rest in the argument list of the function, like this:

Code: Select all

(lambda (first-arg second-arg &rest other-args)
  (do-something-with first-arg second-arg other-args))
Inside this function, except for the first and second arguments, all the others are rolled in a list. The variable other-args is that list. For instance, calling the function above with args 1, 2, 3, 4, 5 and 6, you will have first-arg = 1, second-arg = 2 and other-args = (3 4 5 6).

You can also do the opposite (unroll a list into the arguments of a function) using the function apply. For instance, this function will sum all its arguments.

Code: Select all

(lambda (&rest args)
  (apply + args))
If you collect the pieces and think a little bit, you should be able to do the function you want.

blooper
Posts: 6
Joined: Thu May 14, 2009 10:17 pm

Re: Function with fixed parameter

Post by blooper » Wed May 20, 2009 6:56 pm

Are you sure that &rest directive is acceptable in Scheme or is it just available in CL? I've tried to used it DrScheme with R5RS language turned on, but it doesn't work as it should have - it is trying to take &rest as if it was one of the arguments... :(

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Function with fixed parameter

Post by gugamilare » Wed May 20, 2009 7:43 pm

blooper wrote:Are you sure that &rest directive is acceptable in Scheme or is it just available in CL? I've tried to used it DrScheme with R5RS language turned on, but it doesn't work as it should have - it is trying to take &rest as if it was one of the arguments... :(
Hum, sorry, my mistake. I was sure I had tested this here with tinyscheme, but I was wrong. Well, there is an analogous way, though:

Code: Select all

(lambda all-args
  all-args)
rolls all arguments into all-args, and

Code: Select all

(lambda (first-arg second-arg . other-args)
  (do-something-with first-arg second-arg other-args))
fetches all arguments up from the third into other-args. This time I tested:

Code: Select all

> ((lambda args args) 1 2 3)
(1 2 3)
> ((lambda (first-arg second-arg . other-args) other-args) 1 2 3 4 5)
(3 4 5)

blooper
Posts: 6
Joined: Thu May 14, 2009 10:17 pm

Re: Function with fixed parameter

Post by blooper » Wed May 20, 2009 8:42 pm

Thank you! :D Now everything works like a charm! :)

Post Reply