Re: trampoline for mutual recursion

2008-11-28 Thread André Thieme
On 28 Nov., 23:29, Meikel Brandmeyer <[EMAIL PROTECTED]> wrote: > Hi André, > > Am 28.11.2008 um 22:56 schrieb André Thieme: > > >> Maybe I have a wrong understanding of "closure", but > >> as far as I understand, every function definition in > >> Clojure - be it defn or #() - finally translates t

Re: trampoline for mutual recursion

2008-11-28 Thread Meikel Brandmeyer
Hi André, Am 28.11.2008 um 22:56 schrieb André Thieme: Maybe I have a wrong understanding of "closure", but as far as I understand, every function definition in Clojure - be it defn or #() - finally translates to (fn ...). And fn creates a closure. I tooled myself up and looked up the definiti

Re: trampoline for mutual recursion

2008-11-28 Thread André Thieme
On 27 Nov., 13:06, Meikel Brandmeyer <[EMAIL PROTECTED]> wrote: Hello. > Maybe I have a wrong understanding of "closure", but > as far as I understand, every function definition in > Clojure - be it defn or #() - finally translates to (fn ...). > And fn creates a closure. Well, from my experien

Re: trampoline for mutual recursion

2008-11-27 Thread lpetit
On 27 nov, 18:09, Rich Hickey <[EMAIL PROTECTED]> wrote: > > > (defn trampoline [f] > > >   (let [ret (f)] > > >      (cond f > > >        (fn? ret) (recur ret) > > >        (instance? org.clojure.lang.NoBoingWrapper f) (.returnInstance > > > f) > > >        else ret))) > I considered that, and it

Re: trampoline for mutual recursion

2008-11-27 Thread jim
Rich, I'm currently working on some code where I'm implementing state machines where each state is a function. To move to another state, the current state returns the function of the next state and trampoline runs the machine through to completion. My next step is to have each state return a va

Re: trampoline for mutual recursion

2008-11-27 Thread Rich Hickey
On Nov 27, 8:37 am, lpetit <[EMAIL PROTECTED]> wrote: > On Nov 27, 1:58 pm, lpetit <[EMAIL PROTECTED]> wrote: > > > and the modification of trampoline along these lines : > > (defn trampoline [f] > > (let [ret (f)] > > (cond f > >(fn? ret) (recur ret) > >(instance? org.clo

Re: trampoline for mutual recursion

2008-11-27 Thread lpetit
On Nov 27, 1:58 pm, lpetit <[EMAIL PROTECTED]> wrote: > and the modification of trampoline along these lines : > (defn trampoline [f] >   (let [ret (f)] >      (cond f >        (fn? ret) (recur ret) >        (instance? org.clojure.lang.NoBoingWrapper f) (.returnInstance > f) >        else ret)))

Re: trampoline for mutual recursion

2008-11-27 Thread lpetit
Thank you for the answer. Could trampoline benefit for being enhanced by allowing an explicit "escaping" for a return value of type function (that could also work for other return types, but would not be necessary for the common case). Something like these : - common usage : as usual user=> (de

Re: trampoline for mutual recursion

2008-11-27 Thread Stephen C. Gilardi
On Nov 27, 2008, at 7:13 AM, Stephen C. Gilardi wrote: > interface "fn" that distinguishes The interface is "clojure.lang.Fn". --Steve --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to

Re: trampoline for mutual recursion

2008-11-27 Thread Stephen C. Gilardi
On Nov 27, 2008, at 5:21 AM, Robert Pfeiffer wrote: > If you want to return sets, keywords, maps, or everything that > implements the fn interface this is also an issue. Does trampolining > on keywords make sense? The svn revision that introduced trampoline also introduced a "marker" interface

Re: trampoline for mutual recursion

2008-11-27 Thread Meikel Brandmeyer
Hi, On 26 Nov., 23:40, André Thieme <[EMAIL PROTECTED]> wrote: > Maybe a simple accumulator could work here: > user> (let [acc (ref 0)] >         (defn make-accumulator [n] >           (dosync (ref-set acc n)) >           (fn [i] >             (dosync (alter acc + i) > #'user/make-accumulator

Re: trampoline for mutual recursion

2008-11-27 Thread Robert Pfeiffer
On 26 Nov., 22:38, "Stephen C. Gilardi" <[EMAIL PROTECTED]> wrote: >    [..] Note that if you want to return a fn as a >    final value, you must wrap it in some data structure and unpack it >    after trampoline returns. If you want to return sets, keywords, maps, or everything that implements

Re: trampoline for mutual recursion

2008-11-26 Thread Mark Volkmann
On Wed, Nov 26, 2008 at 4:40 PM, André Thieme <[EMAIL PROTECTED]> wrote: > > On 26 Nov., 21:26, Chouser <[EMAIL PROTECTED]> wrote: >> On Wed, Nov 26, 2008 at 3:22 PM, Mark Volkmann >> >> <[EMAIL PROTECTED]> wrote: >> >> > I'm curious about this syntax. I thought if #(function-name args) >> > creat

Re: trampoline for mutual recursion

2008-11-26 Thread André Thieme
On 26 Nov., 21:26, Chouser <[EMAIL PROTECTED]> wrote: > On Wed, Nov 26, 2008 at 3:22 PM, Mark Volkmann > > <[EMAIL PROTECTED]> wrote: > > > I'm curious about this syntax. I thought if #(function-name args) > > creates a closure then I can put one in a variable and execute it > > laterI entered t

Re: trampoline for mutual recursion

2008-11-26 Thread Stephen C. Gilardi
On Nov 26, 2008, at 4:32 PM, lpetit wrote: > I've maybe missed something, but will this work if one wants to make > the final return value of the tail call a closure ? Along the same lines of this being a manual way to do TCO, that issue will need to be handled manually as well. Here's what (d

Re: trampoline for mutual recursion

2008-11-26 Thread lpetit
On 25 nov, 15:05, Rich Hickey <[EMAIL PROTECTED]> wrote: > To convert to a trampoline, simply return closures over your tail > calls, rather than direct calls. This is as simple as prepending # I've maybe missed something, but will this work if one wants to make the final return value of the tai

Re: trampoline for mutual recursion

2008-11-26 Thread Stephen C. Gilardi
On Nov 26, 2008, at 3:22 PM, Mark Volkmann wrote: > I entered this in a REPL. > > (def myClosure #(prn "Hello")) > > How can I execute the closure in myClosure now? Clojure user=> (def myClosure #(prn "Hello")) #'user/myClosure user=> (myClosure) "Hello" nil user=> --Steve --~--~-~--~

Re: trampoline for mutual recursion

2008-11-26 Thread Chouser
On Wed, Nov 26, 2008 at 3:22 PM, Mark Volkmann <[EMAIL PROTECTED]> wrote: > > I'm curious about this syntax. I thought if #(function-name args) > creates a closure then I can put one in a variable and execute it > laterI entered this in a REPL. > > (def myClosure #(prn "Hello")) > > How can I exec

Re: trampoline for mutual recursion

2008-11-26 Thread Mark Volkmann
On Tue, Nov 25, 2008 at 8:05 AM, Rich Hickey <[EMAIL PROTECTED]> wrote: big snip > To convert to a trampoline, simply return closures over your tail > calls, rather than direct calls. This is as simple as prepending # > > (declare bar) > > (defn foo [n] > (if (pos? n) >#(bar (dec n)) I'm c

Re: trampoline for mutual recursion

2008-11-26 Thread Randall R Schulz
Hi, On Tuesday 25 November 2008 06:05, Rich Hickey wrote: > I've added trampoline to ease the conversion/creation of mutually > recursive algorithms in Clojure. > > ... Clojure's new trampolines for mutually recursive functions has caught the attention of the Scala folks. There's a nice thread

Re: trampoline for mutual recursion

2008-11-26 Thread Chouser
On Wed, Nov 26, 2008 at 1:00 PM, dreish <[EMAIL PROTECTED]> wrote: > > My favorite thing about recur is that the compiler tells you > immediately if you accidentally put it somewhere other than in a tail > position. You don't have to wait for the stack to overflow in actual > use because your test

Re: trampoline for mutual recursion

2008-11-26 Thread dreish
On Nov 26, 11:14 am, Rich Hickey <[EMAIL PROTECTED]> wrote: > There's a very simple reason - such built-in trampolines are > incompatible with interoperability. Anyone can build a trampoline > system into their lang to get tail calls and make sure it is used > consistently within lang - but what d

Re: trampoline for mutual recursion

2008-11-26 Thread Rich Hickey
On Nov 26, 10:28 am, dreish <[EMAIL PROTECTED]> wrote: > Now that you've gone this far, why not do this? > > - class clojure.lang.TailCall contains an AFn > - Compiler checks for a tail call position and instead of calling it, > returns new TailCall(AFn) > - In invoke, while returnvalue instance

Re: trampoline for mutual recursion

2008-11-26 Thread dreish
Now that you've gone this far, why not do this? - class clojure.lang.TailCall contains an AFn - Compiler checks for a tail call position and instead of calling it, returns new TailCall(AFn) - In invoke, while returnvalue instanceof TailCall, returnvalue = returnvalue.fn.invoke(returnvalue) I con

Re: trampoline for mutual recursion

2008-11-25 Thread kib2
Thanks Rich, funny, there was a post on trampoline in the Python community today : http://davywybiral.blogspot.com/2008/11/trampolining-for-recursion.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" gr

Re: trampoline for mutual recursion

2008-11-25 Thread jim
Very nice. Thanks, Rich. I had implemented my own version, but it's much nicer to have it in the language. Jim --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to

Re: trampoline for mutual recursion

2008-11-25 Thread samppi
Yes, this is superb—I was just now trying to figure out a complicated tree zipper that mutual recursion would make needless. Thanks a lot! On Nov 25, 11:22 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Nov 25, 11:07 am, Rich Hickey <[EMAIL PROTECTED]> wrote: > > > > > On Nov 25, 10:42 a

Re: trampoline for mutual recursion

2008-11-25 Thread [EMAIL PROTECTED]
On Nov 25, 11:07 am, Rich Hickey <[EMAIL PROTECTED]> wrote: > On Nov 25, 10:42 am, "Stephen C. Gilardi" <[EMAIL PROTECTED]> wrote: > > > On Nov 25, 2008, at 9:05 AM, Rich Hickey wrote: > > > > I've added trampoline to ease the conversion/creation of mutually > > > recursive algorithms in Clojure

Re: trampoline for mutual recursion

2008-11-25 Thread Rich Hickey
On Nov 25, 10:42 am, "Stephen C. Gilardi" <[EMAIL PROTECTED]> wrote: > On Nov 25, 2008, at 9:05 AM, Rich Hickey wrote: > > > I've added trampoline to ease the conversion/creation of mutually > > recursive algorithms in Clojure. > > Very cool! Thanks for enabling that. > > In looking over the imp

Re: trampoline for mutual recursion

2008-11-25 Thread Stephen C. Gilardi
On Nov 25, 2008, at 9:05 AM, Rich Hickey wrote: > I've added trampoline to ease the conversion/creation of mutually > recursive algorithms in Clojure. Very cool! Thanks for enabling that. In looking over the implementation, I found a small problem: the arg vector precedes the doc string lea

trampoline for mutual recursion

2008-11-25 Thread Rich Hickey
I've added trampoline to ease the conversion/creation of mutually recursive algorithms in Clojure. Trampolines are a well known technique for implementing TCO - instead of actually making a call in the tail, a function returns the function to be called, and a controlling loop calls it, thus allow