Lisp Anonymous Function Local Variable - function

How do I assign anonymous functions to local variables in either cl, emacs lisp or clojure?
I've tried the following with no success.
(let ((y (lambda (x) (* x x)) )) (y 2))
and
((lambda (x) 10) (lambda (y) (* y y)))

In CL, you could use flet or labels.
(defun do-stuff (n)
(flet ((double (x) (* 2 x)))
(double n)))
(do-stuff 123) ;; = 246
As Chris points out, since double is not recursive, we should use flet, as the difference between the two is that labels can handle recursive functions.
Check out docs for info on labels, or this question for the difference between labels and flet.

Related

where does racket lambda argument come from?

I asked a similar question before, I just want to make sure I understand the idea, in the -lambda(x)- line 4, what is the x, where it is coming from?
(define (cached-assoc xs n)
(letrec ([memo (make-vector n #f)]
[acc 0]
[f (lambda(x)
(let ([ans (vector-assoc x memo)])
(if ans
(cdr ans)
(let ([new-ans (assoc x xs)])
(begin
(vector-set! memo acc (cons x new-ans))
(set! acc (if (= (+ acc 1)) 0 (+ acc 1)))
new-ans)))))])
f))
Your cached-assoc procedure returns f, a function which has x as an unbound parameter. It doesn’t have a value yet, the value will be whatever you pass to it when you finally invoke it:
((cached-assoc xs n) 10)
In the above example x will be bound to 10, and xs and n will be whatever you defined for them before calling cached-assoc. I believe the source of the confusion is the fact that cached-assoc is returning a lambda, once you understand this it'll be clear.

"Adding" two functions together in Scheme

I am going through a practice exam for my programming languages course. One of the problems states:
Define a function named function+ that “adds” two functions together and returns this composition. For example:
((function+ cube double) 3)
should evaluate to 216, assuming reasonable implementations of the functions cube and double.
I am not sure how to approach this problem. I believe you are supposed to use the functionality of lambdas, but I am not entirely sure.
If you need a procedure which allows you two compose to unary procedures (procedure with only 1 parameter), you'll smack yourself in the head after seeing how simple the implementation is
(define (function+ f g)
(λ (x) (f (g x))))
(define (cube x)
(* x x x))
(define (double x)
(+ x x))
((function+ cube double) 3)
;=> 216
Basically if you need to do that you just do (x (y args ...)) so if you need to have a procedure that takes two arguments proc1 and proc2 returns a lambda that takes any number of arguments. You just use apply to give proc1 arguments as a list and pass the result to proc2. It would look something like this:
(define (compose-two proc2 proc1)
(lambda args
...))
The general compose is slightly more complicated as it takes any number of arguments:
#!r6rs
(import (rnrs))
(define my-compose
(let* ((apply-1
(lambda (proc value)
(proc value)))
(gen
(lambda (procs)
(let ((initial (car procs))
(additional (cdr procs)))
(lambda args
(fold-left apply-1
(apply initial args)
additional))))))
(lambda procs
(cond ((null? procs) values)
((null? (cdr procs)) (car procs))
(else (gen (reverse procs)))))))
(define (add1 x) (+ x 1))
((my-compose) 1) ;==> 1
((my-compose +) 1 2 3) ; ==> 6
((my-compose sqrt add1 +) 9 15) ; ==> 5

define a form as function name?

I'd like to know what this code means in Scheme:
(define ((K x) y) x)
(define (((S x) y) z)
((x z) (y z)))
The whole file is here.
Is this legal Scheme? Is (K x) a parametrized function, something like generic functions in Java? I looked up the MIT Scheme reference, there seems to be nothing mentioned for definition of this kind.
Trying it in MIT Scheme works
(define ((K x) y) x)
;Value: k
((k 3) 4)
;Value: 3
Apparently, these are the definitions for K and S combinators from a combinatorial logic SKI calculus.
We can define the same function explicitly,
(define k (lambda (x) (lambda (y) x)))
;Value: k
((k 3) 4)
;Value: 3
Apparently, MIT-Scheme does that for us, just as in case of regular definitions like (define (fun foo) bar) being translated to (define fun (lambda (foo) bar)).
The S combinator would be defined explicitly as
(define S (lambda (x) (lambda (y) (lambda (z)
((x z) (y z))))))
(define ((add a) b) (+ a b))
;Value: add
(define (add1 a) (+ a 1))
;Value: add1
(((s add) add1) 3)
;Value: 7
This is how currying languages (like e.g. Haskell) work, where every function is a function of one argument. Haskell is very close to the combinatorial logic in that respect, there's no parentheses used at all, and we can write the same definitions simply as
_K x y = x
_S x y z = x z (y z)
So that _S (+) (1+) 3 produces 7.
It's called Curried Function Shorthand and described here.

how to return function in elisp

This is related to this question: elisp functions as parameters and as return value
(defun avg-damp (n)
'(lambda(x) (/ n 2.0)))
Either
(funcall (avg-damp 6) 10)
or
((avg-damp 6) 10)
They gave errors of Symbol's value as variable is void: n and eval: Invalid function: (avg-damp 6) respectively.
The reason the first form does not work is that n is bound dynamically, not lexically:
(defun avg-damp (n)
(lexical-let ((n n))
(lambda(x) (/ x n))))
(funcall (avg-damp 3) 12)
==> 4
The reason the second form does not work is that Emacs Lisp is, like Common Lisp, a "lisp-2", not a "lisp-1"

Calling a Scheme function using its name from a list

Is it possible to call a Scheme function using only the function name that is available say as a string in a list?
Example
(define (somefunc x y)
(+ (* 2 (expt x 2)) (* 3 y) 1))
(define func-names (list "somefunc"))
And then call the somefunc with (car func-names).
In many Scheme implementations, you can use the eval function:
((eval (string->symbol (car func-names))) arg1 arg2 ...)
You generally don't really want to do that, however. If possible, put the functions themselves into the list and call them:
(define funcs (list somefunc ...))
;; Then:
((car funcs) arg1 arg2 ...)
Addendum
As the commenters have pointed out, if you actually want to map strings to functions, you need to do that manually. Since a function is an object like any other, you can simply build a dictionary for this purpose, such as an association list or a hash table. For example:
(define (f1 x y)
(+ (* 2 (expt x 2)) (* 3 y) 1))
(define (f2 x y)
(+ (* x y) 1))
(define named-functions
(list (cons "one" f1)
(cons "two" f2)
(cons "three" (lambda (x y) (/ (f1 x y) (f2 x y))))
(cons "plus" +)))
(define (name->function name)
(let ((p (assoc name named-functions)))
(if p
(cdr p)
(error "Function not found"))))
;; Use it like this:
((name->function "three") 4 5)