# Unity Scheme - lists and procedure arguments

This topic is 3750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Lets say I have a procedure like this:
(define (some-procedure a b c d) (some-code))

and a list like this:
(list 1 2 3)

How could I pass the elements of the list to the parameters expected by the procedure (ie: a -> 1, b -> 2 and c -> 3)? Or even better - a means to produce a new procedure which takes as arguments any arguments of the original procedure which could not be retrieved from the list (eg, if the list contains fewer arguments than the procedure). I hope I'm making sense . All useful responses greatly appreciated.

##### Share on other sites
There's apply, which lets you apply a list of arguments to a function:
(define (func a b c d) ...)(apply func '(1 2 3 4))
However, the list must contain the correct number of arguments (not more, not less), otherwise you will get a runtime error.

Your "even better" sounds a lot like currying, but it's not something Scheme supports AFAIK. You could start faking it:
(define (curry func n-args)  (define (curry-aux acc)    (lambda args      (if (< (+ (length acc) (length args)) n-args)          (curry-aux (append acc args))          (apply func (append acc args)))))  (curry-aux ()))(define cfunc (curry func 4))(func 1 2 3 4) == (cfunc 1 2 3 4) == ((cfunc 1 2) 3 4) == ((((cfunc 1) 2) 3) 4) == ...

apply is compatible with it, so you could apply a partial list of arguments.
(define partially (apply cfunc '(1 2)))(partially 3 4) == (func 1 2 3 4)

##### Share on other sites
Thank you both!

Taking ideas from SamLowry's post and the code for the explicit currying from SiCrane's link, I came up with this, which does exactly what I want it to:
(define (curry fun . args)  (lambda x (apply fun (append args x))))(define (func a b c d)  (display a) (newline)  (display b) (newline)  (display c) (newline)  (display d) (newline))(define cfunc (apply curry (cons func '(1 2))))
> (cfunc 3 4)1234

Rating++ for both of you.

[Edited by - issch on February 12, 2008 1:11:09 PM]

##### Share on other sites
I think that's a little more complicated than necessary :)

(define (curry fun arglst) ; why '.'?  (lambda x (apply fun (append arglst x))))(define (func a b c d)  (begin ; Don't you need this?    (display a) (newline)    (display b) (newline)    (display c) (newline)    (display d) (newline)))(define cfunc (curry func '(1 2))) ; 'apply' is for dealing with things where you; don't know the length ahead of time. When you do, just pass the arguments.

##### Share on other sites
Quote:
 Original post by ZahlmanI think that's a little more complicated than necessary :)*** Source Snippet Removed ***

The . makes a difference:
(define (curry1 fun arglst)  (lambda x (apply fun (append arglst x))))(define (curry2 fun . arglst)  (lambda x (apply fun (append arglst x)))); Usage(define curried1 (curry1 func 1 2 3))(define curried2 (curry2 func '(1 2 3)))

I personally prefer the former.

And, begin is not necessary: there is an implicit one in defines and lambdas.

##### Share on other sites
Ah, of course. (Wasn't there another syntax like (define name (lambda x y (stuff)))?)

##### Share on other sites
Quote:
 Original post by ZahlmanAh, of course. (Wasn't there another syntax like (define name (lambda x y (stuff)))?)

(define (func a b c)  body)==(define func  (lambda (a b c)     body))

Also, the argument list (in both define and lambda is in some way a pattern, a syntactic shortcut for a "destructuring bind":
...== (define (func . args)  (let ((a (car args))        (b (cadr args))        (c (caddr args)))     body))==(define func  (lambda args    (let ((a (car args))          (b (cadr args))          (c (caddr args)))      body)))

##### Share on other sites
Quote:
 Original post by ZahlmanI think that's a little more complicated than necessary :)*** Source Snippet Removed ***

Do'h! You are, of course, correct. If I remove the . I can simplify the code to this:
(define (curry fun args)  (lambda x (apply fun (append args x))))(define (func a b c d)  (display a) (newline)  (display b) (newline)  (display c) (newline)  (display d) (newline))(define cfunc (curry func '(1 2)))

• 10
• 19
• 14
• 19
• 15
• ### Similar Content

• I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
• By Pixeye
I wrote an extension for unity inspector that allows to group/fold variables.
Available on github  , cheers!

• I'M interested in programming tools (For animation, UI, etc). Can anyone suggest me the resources where I can start learning or which technologies I need achive it.

Thanks,
Rakshit
• By Paszq
Group photo of some of the characters and creatures currently living in Arpago
• By Paszq
A bunch of chests full of sweet loot :)