creating a function from a sexp

27 views
Skip to first unread message

Sunil S Nandihalli

unread,
Oct 27, 2010, 9:15:51 AM10/27/10
to clo...@googlegroups.com
Hello everybody,
 
I would like to create a function from an arbitrary s-expression for example if I have something like 

(def x 10)
(let [y 20]
  (create-fn-from-sexp '(+ x y z)))

I would like create-fn-from-sexp to return a function which is a function of the third argument z .. since both x and y are both bound in this lexical scope . Is it possible to write such a function?  If the create-fn-from-sexp worked correctly the above code would be equivalent to 


(def x 10)
(let [y 20]
 (fn [z] (+ x y z))

Is it possible to write such a function?

thanks 
Sunil.

Ulises

unread,
Oct 27, 2010, 9:24:12 AM10/27/10
to clo...@googlegroups.com
> Is it possible to write such a function?

Perhaps you're looking for partial?

user> (def x 10)
#'user/x
user> (let [y 5] (partial + x y))
#<core$partial$fn__3680 clojure.core$partial$fn__3680@beebcd>
user> ((let [y 5] (partial + x y)) 1)
16
user>

U

Sunil S Nandihalli

unread,
Oct 27, 2010, 9:34:08 AM10/27/10
to clo...@googlegroups.com
not really the problem is that the s-expression is passed I don't know it ahead of time during compilation .. it is known at run-time...

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

nicola...@gmail.com

unread,
Oct 27, 2010, 9:47:22 AM10/27/10
to clo...@googlegroups.com
You need to use both eval and a macro to get then environment.
I think &env contains the lexical environment in any macro call now.
(I don't know in each form though).
So you need to walk the term, looking for free variables, close under
free variables by creating a function and then call eval.

That's probably quite some work to write.

Altogether, it looks like at least a step too far. (Especially some
kind of problem may arise: what of (+ x y ((fn [z] z) 3))?)
But I know sometimes it is necessary to go a few steps further than we should.

What is the use case?

Best,

Nicolas.

Sunil S Nandihalli

unread,
Oct 27, 2010, 10:38:33 AM10/27/10
to clo...@googlegroups.com
Hi Nicolas,
 Thanks for you response. 
 This is an experimental work. Knowing that there was clojuratica to link to mathematica .. I was hoping that we can generate the code to solve PDE .. This is an experimental thing .. But been having a lot of trouble in getting the basic stuff setup .. being able to do what I described above would be crucial to achieve this.
 and between .. I didn't find any other examples of the usage of &env and &form apart from their presence in the clojure/core.clj. It would be nice to have some description of the usage of these things.
Thanks again,
Sunil.


--

Sunil S Nandihalli

unread,
Oct 27, 2010, 10:46:37 AM10/27/10
to clo...@googlegroups.com
actually i am able to find a few examples ... from a google group thread...
http://groups.google.com/group/clojure/browse_thread/thread/d710c290b67951a3/b827d46389110f26?lnk=gst&q=clojure+macro+%26env#b827d46389110f26

just thought of sharing it again in this context..
Sunil
Reply all
Reply to author
Forward
0 new messages