> > Lazy-seq's are often handy in Clojure to subvert the stack limit imposed
> > by the the JVM, but it's not quite the same problem that TCO solves.
> 
> Having recently converted some Scheme that leaned heavily on the presence
> of TCO, I'm curious as to what situations you think could not be solved
> with lazy-seqs?
> 
> David

As I understand it, indirect tail calls are the case where TCO is
most difficult to emulate in languages without goto.
Of course, we have trampolines, and
(as demonstrated in Nicolas's example) there's a correspondence
between lazy-seqs and trampolines.
So, I don't mean to claim that there's anything you *can't* do with lazy-seq.
Although I do wonder how to translate cps-code to use lazy-seqs
(as opposed to trampolines).

(apologies in advance for this silly example)

(defn [f k x]
  (if (time-to-return? x)
    (k x)
    (g (fn [x*] (k (do-stuff-to x*))) 
       x)))

(defn [g k x]
  (if (time-to-return? x)
    (k x)
    (f (fn [x*] (k (do-other-stuff-to x*)))
       x)))

so with TCO,
assuming k doesn't exhaust memory,
f and g will operate in constant space,
and the eventual call to k will also unwind in constant space. 

cheers,
Andy



 

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to