This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Function with fixed parameter

5 posts · 6254 views

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.
(define function1 (lambda (x y z) (+ x (* 2 y) (* 3 z))))
(display ((Fix-first-parameter function1 1) 2 3))
should return:
14
2.
(define function1 (lambda (x y z) (+ x (* 2 y) (* 3 z))))
(display ((Fix-second-parameter function1 1) 2 3 ))
should return:
13
3.
(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:
(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. :)

Re: Function with fixed parameter

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:
(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.
(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.

Re: Function with fixed parameter

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... :(

Re: Function with fixed parameter

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:
(lambda all-args
  all-args)
rolls all arguments into all-args, and
(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:
> ((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)

Re: Function with fixed parameter

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