newLISP Fan Club

Forum => newLISP in the real world => Topic started by: Fritz on November 12, 2009, 04:55:08 AM

Title: define-macro and curry
Post by: Fritz on November 12, 2009, 04:55:08 AM
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?
Title: Re: define-macro and curry
Post by: Lutz on November 12, 2009, 08:22:22 AM
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)))
Title: Re: define-macro and curry
Post by: Fritz on November 12, 2009, 11:09:39 AM
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))
Title: Re: define-macro and curry
Post by: Lutz on November 12, 2009, 11:18:11 AM
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.