Create a function with a function

Q&A's, tips, howto's
Post Reply
cameyo
Posts: 147
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Create a function with a function

Post by cameyo »

Function to create a function with name and parameters:

Code: Select all

(define (make-add name val)
  (let (f nil)
    (setq f (string "(define (" name " x) (+ " val " x))"))
    (setq name (eval-string f))
  name))
Creating a function

Code: Select all

(make-add "sum-10" 10)
out: (lambda (x) (+ 10 x))
Using the created function

Code: Select all

(sum-10 3)
out: 13
Do you know of another way (instead of using strings) to create a function with another function?

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: Create a function with a function

Post by newBert »

Maybe this way, but it's not quite the same:

Code: Select all

(define (make-adder x)
  (letex (x y)
    (fn (z) (+ y z))))
    
(setq add2 (make-adder 2))
(println (add2 4))
> 6
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

cameyo
Posts: 147
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Re: Create a function with a function

Post by cameyo »

Thank you, but it doesn't works. The symbol y is not binded.
I am looking for the most suitable/fastest method of generating functions automatically (passing name of function and parameters).
Last edited by cameyo on Tue Apr 27, 2021 7:26 pm, edited 1 time in total.

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: Create a function with a function

Post by newBert »

Oh my apologies! I reversed x and y (in `letex`) when transcribing... :/

Code: Select all

(define (make-adder x)
  (letex (y x)
    (fn (z) (+ y z))))
    
(setq add2 (make-adder 2))
(println (add2 4))
; 6
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

cameyo
Posts: 147
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Re: Create a function with a function

Post by cameyo »

It works.
But I would also need to pass the function name as a parameter, for example:

Code: Select all

(make-adder "add10" 10)
Thanks again for the help

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: Create a function with a function

Post by newBert »

So, maybe like this (probably improvable):

Code: Select all

(define-macro (make-adder) (local (name val) (bind (args) true) (set (expand name 'name) (expand (lambda (x) (+ val x)) 'val))))

(make-adder (name 'add10) (val 10))
(println (add10 3))
; 13
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: Create a function with a function

Post by newBert »

Sorry, I could not indent the previous code, because it caused an "internal server error" (?!)
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

cameyo
Posts: 147
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Re: Create a function with a function

Post by cameyo »

Thanks again.
I'll test both methods for speed and simplicity.

pda
Posts: 19
Joined: Fri Sep 25, 2020 10:42 am

Re: Create a function with a function

Post by pda »

cameyo wrote:
Tue Mar 30, 2021 2:07 pm
Function to create a function with name and parameters:

Code: Select all

(define (make-add name val)
  (let (f nil)
    (setq f (string "(define (" name " x) (+ " val " x))"))
    (setq name (eval-string f))
  name))
Creating a function

Code: Select all

(make-add "sum-10" 10)
out: (lambda (x) (+ 10 x))
Using the created function

Code: Select all

(sum-10 3)
out: 13
Do you know of another way (instead of using strings) to create a function with another function?
your example of a made-add function to create a sum-10 function:

Code: Select all

(define (make-add n v)
  (evaluate (list 'define (list n 'x) (list '+ v 'x))))
and using it:

Code: Select all

(make-add 'sum-10 10)
out: (lambda (x) (+ 10 x))
(sum-10 3)
out: 13
a more general way to create any funcion with name n having parameters a and a body b with minor checking of parameters and function name:

Code: Select all

(define (mkf n a b)
    (evaluate (and (symbol? n) (list 'define (if (apply and (map symbol? (cons n a))) (cons n a) n) b))))
and using it:

Code: Select all

(mkf 'sum-x-y '(x y) '(+ x y))
out: (lambda (x y) (+ x y))
sum-x-y
out: (lambda (x y) (+ x y))
(sum-x-y 4 5)
out: 9
the function expects name and parameters to be symbols and defaults to a symbol definition if you pass bad parameters, that is paremeters which are not symbols, in this case is defines a symbol with the name passed and the evaluated body as value (or nil if evaluation fails). If the name of the function to be defined is not a symbols it returns nil and doesn't define anything:

Code: Select all

x
out: nil
y
out: nil
(mkf 'sum-x-y-with-bad-parameters '(x y 3) '(+ x y))
out: ERR
sum-x-y-with-bad-parameters
out: nil
(define x 3)
out: 3
(define y 34)
out: 34
(mkf 'sum-x-y-with-bad-parameters '(x y 3) '(+ x y))
out: 37
sum-x-y-with-bad-parameters
out: 37
(mkf 4 '(x y 3) '(+ x y))
out: nil
(mkf nil '(x y) '(+ x y))
out: nil
Take this as an example about how to face the question, of course it needs more checking to be robust and correct (variable capture...).

Note: in this post "evaluate" means "eval", because phpBB does not allow me to type it even in code section, so assume this code as the very begining:

Code: Select all

(define evaluate eval)

cameyo
Posts: 147
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Re: Create a function with a function

Post by cameyo »

Thanks pda

Post Reply