Will recursively-called variable be free or bound? - scope

I am trying to have a better understanding about free and bound variables. Here is an example code:
(define (what-kind-of-var? guess x)
(< (abs (- (square guess) x))
0.001))
I see that bound variables here would be guess and x, and free variables <, abs, -, and square. What if I called what-kind-of-var? recursively? Would it be a bound variable because it is binding itself?
Thanks!

It would, under dynamic binding, but Scheme has lexical scoping.
But actually it is neither. "Free" or "bound" comes from lambda calculus. what-kind-of-var? is a top-level variable, naming that lambda expression,
(define what-kind-of-var?
(lambda (guess x) ;; this
(< (abs (- (square guess) x))
0.001)))
but in lambda calculus expressions cannot be named. The only way to call it recursively in lambda calculus is to use Y combinator:
((Y (lambda (what-kind-of-var?) ;; outer
(lambda (guess x) ;; inner
(if (< (abs (- (square guess) x))
0.001)
guess
(what-kind-of-var? (+ 1 guess) x)))))
4 25)
and now of course what-kind-of-var? is bound inside that new lambda expression under Y. It's free in the nested, inner lambda, but bound in the outer one.

guess and x are parameters. They're eventually bound (to respective arguments) when the function is applied.
<, abs, -, are actually bound to procedures in the initial environment. So they're not free variables.
square would be the free variable, subject to the fact that what-kind-of-var? is not defined in its scope. (note that sqr is bound in the initial environment).
what-kind-of-var? is also not unbound, even if it calls itself recursively (assuming recursion is implemented properly in the language). (define (f param) body) can be seen as (define f (lambda (param) body)

Related

SICP recursive let definition

For
(let ((fact
(lambda (n)
(if (= n 1)
1
(* n (fact (- n 1)))))))
(fact 10))
Scheme gives an error, i wanted to confirm if my reasoning is correct,
Since (let ((var1 arg1)) body) is equivalent to evaluating the body in an environment where var1 is bound to arg1.
Above, when we are trying to calculate arg1 before the bind call, we do not find the reference as that is exactly what we are trying to bind for it to be available
But then why does this not throw
(let ((fact (lambda (n)
(if (= n 1) 1 (* n (fact (- n 1)))))))
(display 10))
as we are trying to make the binding there as well.
Also why doesn't this throw then
(define factorial
(lambda (n)
(if (= n 0)
1
(* n (factorial (- n 1))))))
(factorial 10)
The reason is that the "variable fact is not bound" error occurs only when we actually try to look up the value of "fact". Consider
(let ((fact (lambda (n) (if (= n 0)
1
(* n (fact (- n 1)))))))
(fact 0))
This does not cause an error because we do not enter the else clause and therefore do not attempt to use the value of the unbound variable fact. However, trying the same thing with 1 does give us the error.
We do not run into an error when using define because define and let have different rules. In a let binding, you create a new lexical scope, and the binding of fact in the let binding does not "leak out" from the let to the outside world. Since the right side of a let assignment must only refer to bindings from the outside world, the right side cannot refer to the binding fact.
However, in a define statement, the whole point is to introduce a new binding in the current scope (not create a new scope). In a define binding, the right side of the assignment must refer only to variables from the outside world - but this potentially includes the variable being defined, as long as accessing it is hidden behind a lambda and hence done lazily.
In fact, consider the following:
(define is-even
(lambda (n) (or (= n 0)
(is-odd (- n 1)))))
(define is-odd
(lambda (n) (and (not (= n 0))
(is-even (- n 1)))))
(display (is-odd 55))
In this case, by the time we actually call is-even, is-odd is defined in the same scope as is-even and hence we can look up its value. Similarly, when we call is-odd, is-even is already defined and hence we have a binding for its value.
Note that the following does not work:
(define is-even
(lambda (n) (or (= n 0)
(is-odd (- n 1)))))
(display (let ((is-odd (lambda (n) (and (not (= n 0))
(is-even (- n 1))))))
(is-odd 55)))
This is because the is-odd binding here is not in the same scope as is-even. It is defined in a more local binding than is-even is. The is-odd being referred to in the is-even definition cannot be the same is-odd as created in the let statement, because this is-odd is a local binding and thus cannot be referred to in the wider scope that is-even is defined in.
If we were using the old-fashioned LISP notion of "dynamic scope", also known as "call-by-name semantics", the latter example would work perfectly fine. However, Scheme does not support dynamic scope (which is definitely for the best in my view).

Implement a self-reference/pointer in a pure/functional language (Elm/Haskell)

Abstract Problem:
I'd like to implement a self-reference / pointer in Elm.
Specific Problem:
I'm writing a toy LISP interpreter in Elm inspired by mal.
I'm attempting to implement something like letrec to support recursive and mutually-recursive bindings (the "self reference" and "pointers" I'm mentioning above).
Here's some example code:
(letrec
([count (lambda (items)
(if (empty? items)
0
(+ 1 (count (cdr items)))
)
)
])
(count (quote 1 2 3))
)
;=>3
Note how the body of the lambda refers to the binding count. In other words, the function needs a reference to itself.
Deeper Background:
When a lambda is defined, we need to create a function closure which consists of three components:
The function body (the expression to be evaluated when the function is called).
A list of function arguments (local variables that will be bound upon calling).
A closure (the values of all non-local variables that may be referenced within the body of the function).
From the wikipedia article:
Closures are typically implemented with [...] a representation of the function's lexical environment (i.e., the set of available variables) at the time when the closure was created. The referencing environment binds the non-local names to the corresponding variables in the lexical environment at the time the closure is created, additionally extending their lifetime to at least as long as the lifetime of the closure itself. When the closure is entered at a later time, possibly with a different lexical environment, the function is executed with its non-local variables referring to the ones captured by the closure, not the current environment.
Based on the above lisp code, in creating the lambda, we create a closure whose count variable must be bound to the lambda, thereby creating an infinite/circular/self-reference. This problem gets further complicated by mutually-recursive definitions which must be supported by letrec as well.
Elm, being a pure functional language, does not support imperative modification of state. Therefore, I believe that it is impossible to represent self-referencing values in Elm. Can you provide some guidance on alternatives to implementing letrec in Elm?
Research and Attempts
Mal in Elm
Jos von Bakel has already implemented mal in Elm. See his notes here and the environment implementation here. He's gone to great lengths to manually build a pointer system with its own internal GC mechanism. While this works, this seems like massive amounts of struggle. I'm craving a pure functional implementation.
Mal in Haskell
The mal implementation in Haskell (see code here) uses Data.IORef to emulate pointers. This also seems like hack to me.
Y-Combinator/Fixed Points
It seems possible that the Y-Combinator can be used to implement these self references. There seems to be a Y* Combinator that works for mutually recursive functions as well. It seems logical to me that there must also exist a Z* combinator (equivalent to Y* but supports the eager evaluation model of Elm). Should I transform all of my letrec instances so that each binding is wrapped around a Z*?
The Y-Combinator is new to me and my intuitive mind simply does not understand it so I'm not sure if the above solution will work.
Conclusion
Thank you very much for reading! I have been unable to sleep well for days as I struggle with this problem.
Thank You!
-Advait
In Haskell, this is fairly straightforward thanks to lazy evaluation. Because Elm is strict, to use the technique below, you would need to introduce laziness explicitly, which would be more or less equivalent to adding a pointer indirection layer of the sort you mentioned in your question.
Anyway, the Haskell answer might be useful to someone, so here goes...
Fundamentally, a self-referencing Haskell value is easily constructed by introducing a recursive binding, such as:
let mylist = [1,2] ++ mylist in mylist
The same principle can be used in writing an interpreter to construct self-referencing values.
Given the following simple S-expression language for constructing potentially recursive / self-referencing data structures with integer atoms:
data Expr = Atom Int | Var String | Cons Expr Expr | LetRec [String] [Expr] Expr
we can write an interpreter to evaluate it to the following type, which doesn't use IORefs or ad hoc pointers or anything weird like that:
data Value = AtomV Int | ConsV Value Value deriving (Show)
One such interpreter is:
type Context = [(String,Value)]
interp :: Context -> Expr -> Value
interp _ (Atom x) = AtomV x
interp ctx (Var v) = fromJust (lookup v ctx)
interp ctx (Cons ca cd) = ConsV (interp ctx ca) (interp ctx cd)
interp ctx (LetRec vs es e)
= let ctx' = zip vs (map (interp ctx') es) ++ ctx
in interp ctx' e
This is effectively a computation in a reader monad, but I've written it explicitly because a Reader version would require using the MonadFix instance either explicitly or via the RecursiveDo syntax and so would obscure the details.
The key bit of code is the case for LetRec. Note that a new context is constructed by introducing a set of potentially mutually recursive bindings. Because evaluation is lazy, the values themselves can be computed with the expression interp ctx' es using the newly created ctx' of which they are part, tying the recursive knot.
We can use our interpreter to create a self-referencing value like so:
car :: Value -> Value
car (ConsV ca _cd) = ca
cdr :: Value -> Value
cdr (ConsV _ca cd) = cd
main = do
let v = interp [] $ LetRec ["ones"] [Cons (Atom 1) (Var "ones")] (Var "ones")
print $ car $ v
print $ car . cdr $ v
print $ car . cdr . cdr $ v
print $ car . cdr . cdr . cdr . cdr . cdr . cdr . cdr . cdr . cdr . cdr $ v
Here's the full code, also showing an alternative interp' using the Reader monad with recursive-do notation:
{-# LANGUAGE RecursiveDo #-}
{-# OPTIONS_GHC -Wall #-}
module SelfRef where
import Control.Monad.Reader
import Data.Maybe
data Expr = Atom Int | Var String | Cons Expr Expr | LetRec [String] [Expr] Expr
data Value = AtomV Int | ConsV Value Value deriving (Show)
type Context = [(String,Value)]
interp :: Context -> Expr -> Value
interp _ (Atom x) = AtomV x
interp ctx (Var v) = fromJust (lookup v ctx)
interp ctx (Cons ca cd) = ConsV (interp ctx ca) (interp ctx cd)
interp ctx (LetRec vs es e)
= let ctx' = zip vs (map (interp ctx') es) ++ ctx
in interp ctx' e
interp' :: Expr -> Reader Context Value
interp' (Atom x) = pure $ AtomV x
interp' (Var v) = asks (fromJust . lookup v)
interp' (Cons ca cd) = ConsV <$> interp' ca <*> interp' cd
interp' (LetRec vs es e)
= mdo let go = local (zip vs vals ++)
vals <- go $ traverse interp' es
go $ interp' e
car :: Value -> Value
car (ConsV ca _cd) = ca
cdr :: Value -> Value
cdr (ConsV _ca cd) = cd
main = do
let u = interp [] $ LetRec ["ones"] [Cons (Atom 1) (Var "ones")] (Var "ones")
let v = runReader (interp' $ LetRec ["ones"] [Cons (Atom 1) (Var "ones")] (Var "ones")) []
print $ car . cdr . cdr . cdr . cdr . cdr . cdr . cdr . cdr . cdr . cdr $ u
print $ car . cdr . cdr . cdr . cdr . cdr . cdr . cdr . cdr . cdr . cdr $ v
A binding construct in which the expressions can see the bindings doesn't require any exotic self-reference mechanisms.
How it works is that an environment is created for the variables, and then the values are assigned to them. The initializing expressions are evaluated in the environment in which those variables are already visible. Thus if those expressions happen to be lambda expressions, then they capture that environment, and that's how the functions can refer to each other.
An interpreter does this by extending the environment with the new variables, and then using the extended environment for evaluating the assignments. Similarly, a compiler extends the compile-time lexical environment, and then compiles the assignments under that environment, so the running code will store values into the correct frame locations. If you have working lexical closures, the correct behavior of functions being able to mutually recurse just pops out.
Note that if the assignments are performed in left to right order, and one of the lambdas happens to be dispatched during initialization, and then happens to make a forward call to one of lambdas through a not-yet-assigned variable, that will be a problem; e.g.
(letrec
([alpha (lambda () (omega)]
[beta (alpha)] ;; problem: alpha calls omega, not yet stored in variable.
[omega (lambda ())])
...)
Note that in the R7RS Scheme Report, P16-17, letrec is in fact documented as working like this. All the variables are bound, and then they are assigned the values. If the evaluation of an init expression refers to the same variable that is being initialized, or to later variables not yet initialized, R7RS says that it is an error. The document also specifies a restriction regarding the use of continuations captured in the initializing expressions.
The U combinator
I am late to the party here, but I got interested and spent some time working out how to do this in a Lisp-family language, specifically Racket, and thought perhaps other people might be interested.
I suspect that there is lots of information about this out there, but it's seriously hard to search for anything which looks like '*-combinator' now (even now I am starting a set of companies called 'Integration by parts' and so on).
You can, as you say, do this with the Y combinator, but I didn't want to do that because Y is something I find I can understand for a few hours at a time and then I have to work it all out again. But it turns out that you can use something much simpler: the U combinator. It seems to be even harder to search for this than Y, but here is a quote about it:
In the theory of programming languages, the U combinator, U, is the mathematical function that applies its argument to its argument; that is U(f) = f(f), or equivalently, U = λ f . f(f).
Self-application permits the simulation of recursion in the λ-calculus, which means that the U combinator enables universal computation. (The U combinator is actually more primitive than the more well-known fixed-point Y combinator.)
The expression U(U), read U of U, is the smallest non-terminating program, [...].
(Text from here, which unfortunately is not a site all about the U combinator other than this quote.)
Prerequisites
All of the following code samples are in Racket. The macros are certainly Racket-specific. To make the macros work you will need syntax-parse via:
(require (for-syntax syntax/parse))
However note that my use of syntax-parse is naïve in the extreme: I'm really just an unfrozen CL caveman pretending to understand Racket's macro system.
Also note I have not ruthlessly turned everything into λ: there are lets in this code, use of multiple values including let-values, (define (f ...) ...) and so on.
Two versions of U
The first version of U is the obvious one:
(define (U f)
(f f))
But this will run into some problems with an applicative-order language, which Racket is by default. To avoid that we can make the assumption that (f f) is going to be a function, and wrap that form in another function to delay its evaluation until it's needed: this is the standard trick that you have to do for Y in an applicative-order language as well. I'm only going to use the applicative-order U when I have to, so I'll give it a different name:
(define (U/ao f)
(λ args (apply (f f) args)))
Note also that I'm allowing more than one argument rather than doing the pure-λ-calculus thing.
Using U to construct a recursive functions
To do this we do a similar trick that you do with Y: write a function which, if given a function as argument which deals with the recursive cases, will return a recursive function. And obviously I'll use the Fibonacci function as the canonical recursive function.
So, consider this thing:
(define fibber
(λ (f)
(λ (n)
(if (<= n 2)
1
(+ ((U f) (- n 1))
((U f) (- n 2)))))))
This is a function which, given another function, U of which computes smaller Fibonacci numbers, will return a function which will compute the Fibonacci number for n.
In other words, U of this function is the Fibonacci function!
And we can test this:
> (define fibonacci (U fibber))
> (fibonacci 10)
55
So that's very nice.
Wrapping U in a macro
So, to hide all this the first thing to do is to remove the explicit calls to U in the recursion. We can lift them out of the inner function completely:
(define fibber/broken
(λ (f)
(let ([fib (U f)])
(λ (n)
(if (<= n 2)
1
(+ (fib (- n 1))
(fib (- n 2))))))))
Don't try to compute U of this: it will recurse endlessly because (U fibber/broken) -> (fibber/broken fibber/broken) and this involves computing (U fibber/broken), and we're doomed.
Instead we can use U/ao:
(define fibber
(λ (f)
(let ([fib (U/ao f)])
(λ (n)
(if (<= n 2)
1
(+ (fib (- n 1))
(fib (- n 2))))))))
And this is all fine ((U fibber) 10) is 55 (and terminates!).
And this is really all you need to be able to write the macro:
(define-syntax (with-recursive-binding stx)
(syntax-parse stx
[(_ (name:id value:expr) form ...+)
#'(let ([name (U (λ (f)
(let ([name (U/ao f)])
value)))])
form ...)]))
And this works fine:
(with-recursive-binding (fib (λ (n)
(if (<= n 2)
1
(+ (fib (- n 1))
(fib (- n 2))))))
(fib 10))
A caveat on bindings
One fairly obvious thing here is that there are two bindings constructed by this macro: the outer one, and an inner one of the same name. And these are not bound to the same function in the sense of eq?:
(with-recursive-binding (ts (λ (it)
(eq? ts it)))
(ts ts))
is #f. This matters only in a language where bindings can be mutated: a language with assignment in other words. Both the outer and inner bindings, unless they have been mutated, are to functions which are identical as functions: they compute the same values for all values of their arguments. In fact, it's hard to see what purpose eq? would serve in a language without assignment.
This caveat will apply below as well.
Two versions of U for many functions
The obvious generalization of U, U*, to many functions is that U*(f1, ..., fn) is the tuple (f1(f1, ..., fn), f2(f1, ..., fn), ...). And a nice way of expressing that in Racket is to use multiple values:
(define (U* . fs)
(apply values (map (λ (f)
(apply f fs))
fs)))
And we need the applicative-order one as well:
(define (U*/ao . fs)
(apply values (map (λ (f)
(λ args (apply (apply f fs) args)))
fs)))
Note that U* is a true generalization of U: (U f) and (U* f) are the same.
Using U* to construct mutually-recursive functions
I'll work with a trivial pair of functions:
an object is a numeric tree if it is a cons and its car and cdr are numeric objects;
an objct is a numeric object if it is a number, or if it is a numeric tree.
So we can define 'maker' functions (with an '-er' convention: a function which makes an x is an xer, or, if x has hyphens in it, an x-er) which will make suitable functions:
(define numeric-tree-er
(λ (nter noer)
(λ (o)
(let-values ([(nt? no?) (U* nter noer)])
(and (cons? o)
(no? (car o))
(no? (cdr o)))))))
(define numeric-object-er
(λ (nter noer)
(λ (o)
(let-values ([(nt? no?) (U* nter noer)])
(cond
[(number? o) #t]
[(cons? o) (nt? o)]
[else #f])))))
Note that for both of these I've raised the call to U* a little, simply to make the call to the appropriate value of U* less opaque.
And this works:
(define-values (numeric-tree? numeric-object?)
(U* numeric-tree-er numeric-object-er))
And now:
> (numeric-tree? 1)
#f
> (numeric-object? 1)
#t
> (numeric-tree? '(1 . 2))
#t
> (numeric-tree? '(1 2 . (3 4)))
#f
Wrapping U* in a macro
The same problem as previously happens when we raise the inner call to U* with the same result: we need to use U*/ao. In addition the macro becomes significantly more hairy and I'm moderately surprised that I got it right so easily. It's not conceptually hard: it's just not obvious to me that the pattern-matching works.
(define-syntax (with-recursive-bindings stx)
(syntax-parse stx
[(_ ((name:id value:expr) ...) form ...+)
#:fail-when (check-duplicate-identifier (syntax->list #'(name ...)))
"duplicate variable name"
(with-syntax ([(argname ...) (generate-temporaries #'(name ...))])
#'(let-values
([(name ...) (U* (λ (argname ...)
(let-values ([(name ...)
(U*/ao argname ...)])
value)) ...)])
form ...))]))
And now, in a shower of sparks, we can write:
(with-recursive-bindings ((numeric-tree?
(λ (o)
(and (cons? o)
(numeric-object? (car o))
(numeric-object? (cdr o)))))
(numeric-object?
(λ (o)
(cond [(number? o) #t]
[(cons? o) (numeric-tree? o)]
[else #f]))))
(numeric-tree? '(1 2 3 (4 (5 . 6) . 7) . 8)))
and get #t.
As I said, I am sure there are well-known better ways to do this, but I thought this was interesting enough not to lose.

Racket struct error: given value instantiates a different structure type with the same name

I'm pretty familiar with Racket, and many in the Scheme and Lisp family, but I have no idea what is up with this error, or what is causing it:
network-biases: contract violation;
given value instantiates a different structure type with the same name
expected: network?
given: (network ...) <-- I omitted this because its useless.
Heres the function where the error is (I have a gist of the rest):
(define (update-mini-batch net mini-batch eta)
(define nabla-b (map (lambda (b)
(apply grid (shape b))) (network-biases net)))
(define nabla-w (map (lambda (w)
(apply grid (shape w))) (network-weights net)))
(define-values (nabla-b-new nabla-w-new)
(foldl (lambda (lst bw)
(define x (first lst))
(define y (second lst))
(define-values (nabla-b nabla-w) bw)
(define-values (delta-nabla-b delta-nabla-w) (backprop net x y))
(define nabla-b-new (+/ nabla-b delta-nabla-b))
(define nabla-w-new (+/ nabla-w delta-nabla-w))
(values nabla-b-new nabla-w-new)) (values nabla-b nabla-w) mini-batch))
(struct-copy network net
[biases (map (lambda (b nb)
(- b (* nb (/ eta (length mini-batch)))))
(network-biases net) nabla-b-new)]
[weights (map (lambda (w nw)
(- w (* nw (/ eta (length mini-batch)))))
(network-weights net) nabla-w-new)]))
I couldn't get an MCVE that actually threw an error, so I don't have one to give.
The distilled basics of what I'm trying to do in the above function is this:
Calculate new values for a structure's properties, and create a new structure with those new properties.
- Thanks!!
Structures in Racket are generative. This means that each time
(struct network (num-layers sizes biases weights) #:transparent)
is run, a new type of structure is created. These are all named network.
The error message you see is usually due to evaluating the structure definition twice (and it is a bit confusing since the two types have the same name).
I can't see anywhere in your code that could lead to (struct network ...) being run twice. Are you using DrRacket or an alternative environment that doesn't reset namespace?
If I open "nn.rkt" and run it, will I see the error?

Nested FLET block inside LET block (and vice-versa)

Is it considered idiomatic or non-idiomatic to have a LET block nested inside aFLET/LABELS block ?
Again, I may be coming at this all wrong, but I'm trying to mimic the generic where block in Haskell (so I have a defun and I want to write code that uses certain temporary bindings for values and functions.
In case these are non-idiomatic (after all, I shouldn't expect to transfer over usage from one language to another), what's the right way to do this ?
E.g. something like (stupid example follows ...)
(defun f (x) (
(let* ((x 4)
(y (1+ x))
(flet ((g (x) (+ 2 x)))
(g y))))
You want to know if it's a difference of preference between:
(defun f (x)
(let* ((x 4) (y (1+ x)))
(flet ((g (x) (+ 2 x)))
(g y))))
and
(defun f (x)
(flet ((g (x) (+ 2 x)))
(let* ((x 4) (y (1+ x)))
(g y))))
?
It really doesn't matter which order you put flet/labels and let/let* in this case. It will produce the same result and your CL implementation might optimize your code such that the result would be the same anyway.
In a LISP-1 you would have put it in the same let and then the question would be if you should put the lambda first or last. Seems like taste to me.
The only case where there is a difference is when you are making calculations that are free variables in your function. Like this:
(defun f (x)
(let ((y (1+ x)))
(flet ((g (x) (+ 2 x y))) ; y is free, made in the let*
(g x))))
(f 5) ; ==> 13
Switching order is now impossible without moving logic since the function uses a free variable. You could put the let inside the definition of g like this:
(defun f (x)
(flet ((g (z) ; renamed to not shadow original x
(let* ((y (1+ x)))
(+ 2 z y)))
(g x))))
But imagine you used it with mapcar, reduce or recursion. Then it would have done the calculation for every iteration instead of once before the call. These are the cases that really matter.

Currying for map in Scheme

I understand Haskell syntax.
I want to curry (partially apply) a function called "play-note", and pass one argument "manually," and the second with a map.
If I could write it in Haskell, I'd say:
playNote :: Time -> Instrument -> Int -> Int -> Int -> Int -> Music -- made up, but just to give you an idea
notes = [46, 47, 35, 74]
loopGo = True
loop time = do mapM_ (playNote' time) notes
if loopGo then (loop (time+(second/2))) else return () -- time element removed
playNote' time pitch = playNote time drums pitch 80 11025 9 -- ignore extra args
In Scheme, here's the best I've got:
(define notes '(46 47 35 74))
(define *loop-go* #t)
(define play-note-prime
(lambda (time2)
(lambda (pitch)
(play-note time2 drums pitch 80 11025 9)))) ; drums is another variable
(define loop
(lambda (time)
(map (play-note-prime time) notes)
(if *loop-go*
(callback (+ time (/ *second* 2)) 'loop (+ time (/ second 2)))))) ; time element is more sophisticated here
The Scheme version "compiles," but doesn't do what I expect it to (curry the 1st arg, then the 2nd). Help? Thanks!
Edit:
The essence of my problem is not being able to define a function which takes two arguments, in a way that the following code produces the right result:
map ({some function} {some value; argument 1}) {some list; each element will be an argument 2}
To answer my own question:
The function needs to be defined as taking arguments "piecemeal": in other words, it's not really currying. I was trying that above, but wasn't getting it right.
A function which is partially applied needs to be described as a lambda or chain of lambdas, each accepting exactly the number of arguments it'll be passed (in descending order of when they will be recieved).
A working example:
(define nums '(3 3 1 2))
(define f
(lambda (num1)
(lambda (num2)
(expt num1 num2))))
(define ans (map (f 5) nums))
(print ans)
Defining f as accepting all arguments will not work:
(define f
(lambda (num1 num2)
(expt num1 num2))) ; Can't be curried
You're right -- one way to do it in scheme is to manually create lambdas. This would of course be a pain to implement in a general, correct way, i.e. checking numbers of arguments (don't want to have too many!).
Here's how Clojure (another LISP dialect) does it:
http://clojuredocs.org/clojure_core/clojure.core/partial
Is the source ugly? Maybe. Does it work? Sure.
Unless I'm missing something, wouldn't cut or cute from srfi-26 provide what you need? Then its just a question of whether your scheme implementation provides it (I think that most do)

Resources