Page 1 of 1

Create a function with a function

Posted: Tue Mar 30, 2021 2:07 pm
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?

Re: Create a function with a function

Posted: Mon Apr 26, 2021 9:02 am
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

Re: Create a function with a function

Posted: Tue Apr 27, 2021 11:37 am
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).

Re: Create a function with a function

Posted: Tue Apr 27, 2021 5:15 pm
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

Re: Create a function with a function

Posted: Tue Apr 27, 2021 7:35 pm
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

Re: Create a function with a function

Posted: Thu Apr 29, 2021 1:31 pm
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

Re: Create a function with a function

Posted: Thu Apr 29, 2021 1:35 pm
by newBert
Sorry, I could not indent the previous code, because it caused an "internal server error" (?!)

Re: Create a function with a function

Posted: Tue May 04, 2021 5:24 pm
by cameyo
Thanks again.
I'll test both methods for speed and simplicity.

Re: Create a function with a function

Posted: Sun Dec 19, 2021 10:35 pm
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)

Re: Create a function with a function

Posted: Fri Jan 14, 2022 2:47 pm
by cameyo
Thanks pda