Page 1 of 1

define-macro and curry

Posted: Thu Nov 12, 2009 12:55 pm
by Fritz
I`m trying to make function curry-all, and my macro works, but... looks too ugly to be correct.

Code: Select all

(define-macro (curry-all f)
  (letex (f1 f lst (map string (args)))
    (fn (z) (eval-string (append "(" (name 'f1) " " (join 'lst " ") " " (name 'z) ")")))))

((curry-all + 1 2 3 4) 5)

> 15
I think, there should be some easier way to ask LISP to expand args, right?

Re: define-macro and curry

Posted: Thu Nov 12, 2009 4:22 pm
by Lutz
You don't need string operations and 'eval-string'. Compose and manipulate your lambda expressions directly using list functions:

Code: Select all

(define (curry-all f)
	(append (lambda (z)) (list (cons f (append (args) '(z))))))

((curry-all + 1 2 3 4) 5) => 15
here is another solution using expand, but its slower:

Code: Select all

(define (curry-all f)
	(letex (body (cons f (append (args) '(z))))
		(lambda (z) body)))

Re: define-macro and curry

Posted: Thu Nov 12, 2009 7:09 pm
by Fritz
Thanx, lambdas cleared for me a bit. I was surprised by timing: 0,031 ms for eval-string method and 0,0015 ms for the cons ones.

Btw, why is there 40E3BF in the expression?

Code: Select all

(define (curry-all f)
  (append (lambda (z)) (list (cons f (append (args) '(z))))))

(curry-all + 1)
=> (lambda (z) (+ <40E3BF> 1 z))

Re: define-macro and curry

Posted: Thu Nov 12, 2009 7:18 pm
by Lutz
Btw, why is there 40E3BF in the expression?
that is the way an evaluated built-in function symbol gets displayed (the internal machine hex-adddress). If you would quote it, you wouldn't see it:

Code: Select all

(define (curry-all f)
  (append (lambda (z)) (list (cons f (append (args) '(z))))))

(curry-all '+ 1)

=> (lambda (z) (+ 1 z))
passing it unquoted will be a tiny bit faster.

ps:

Code: Select all

(format "%x" (last (dump +)))
will return the same value.