define-macro and curry

Started by Fritz, November 12, 2009, 04:55:08 AM

Previous topic - Next topic

Fritz

I`m trying to make function curry-all, and my macro works, but... looks too ugly to be correct.


(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?

Lutz

#1
You don't need string operations and 'eval-string'. Compose and manipulate your lambda expressions directly using list functions:


(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:


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

Fritz

#2
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?


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

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

Lutz

#3
QuoteBtw, 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:


(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:
(format "%x" (last (dump +)))

will return the same value.