Function from symbol?

Started by jeremyc, February 21, 2007, 12:02:35 PM

Previous topic - Next topic

jeremyc

How can I:



(define (greet who) (print "Hello " who))
(set 'lst '((name "John" who) (age 10)))
((lst 0 2) (lst 0 1))


In other words, reference a dynamic function inside a quoted list. The real reason for this is a process I created for parsing fixed width files:



(define (special-conversion-function str) (do-something str))
(set 'layout '((name 10) (dob 6 special-conversion-function) (country 2)))
(import-data "file.txt" layout)


Now, the import-data function recognizes that dob requires a special conversion by the inclusion of another parameter in the fixed width format specification. I want import-data to then call the function "special-conversion-function" but I am failing to make it work.



Thanks,



Jeremy

Lutz

#1
not sure what you mean, but you can pass functions as parameters like this:


> (define (do-func foo arg) (foo arg))
(lambda (foo arg) (foo arg))
> (do-func upper-case "hello")
"HELLO"
>


and you could pass user defined functions the same way.



Or when your function is inside a list as a symbol:


> (define (do-func foo arg) (set 'func (eval (first foo))) (func arg))
(lambda (foo arg) (set 'func (eval (first foo))) (func arg))
> (do-func '(upper-case lower-case) "hello")
"HELLO"
>


Lutz

jeremyc

#2
Quote from: "Lutz"not sure what you mean, but


Hm, my code was incorrect above. I simplified:



(set 'lst '((name 20 trim) (age 5 int)))
(print ((lst 0 2) "JEREMY           ") "n")

;; Desired output: JEREMYn


Right now the output is:


invalid function in function print : ((lst 0 2) "JEREMY           ")

Lutz

#3
one more level to peel off :)


>(set 'lst '((name 20 trim) (age 5 int)))  
(print ((eval (lst 0 2)) "JEREMY           ") "n")
JEREMY
"n"
>


Lutz