Dynamic function creation
Posted: Sun Nov 23, 2008 12:12 am
Let me know if you've seen this sort of thing in another language. Let's say you have the following functions:
Wouldn't it be cool if you could eliminate the need for having multiple function definitions in certain situations? You could do something like this:
Then you would call it like this:
And get the output:
This is admittedly a very very stupid example, but I'm sure this could turn out to be really useful in some situations where otherwise you'd have multiple functions essentially doing the same thing, they could be called "newLISP function templates", or something. :-)
Code: Select all
(define (foo a b c)
(string "error: " a b c)
)
(define (bar a b c)
(string "info: " a b c)
)
Code: Select all
(define (log[var] a b c)
(string var ": " a b c)
)
Code: Select all
(logerr "jazziffy" mylist)
You might even add some conditional syntactic sugar to it like so:err: jazzify (1 2 3) nil
Code: Select all
(define (log[-?var] a b c)
(string var ": " a b c)
)
; call it
(log-err "jazzify) ; => err: jazzify nil nil
(log-info "jazzify") ; => info: jazzify nil nil
(log- "jazzify") ; => nil: jazzify nil nil
(log "jazzify") ; => nil: jazzify nil nil