newLISP Fan Club

Forum => newLISP in the real world => Topic started by: ssqq on June 18, 2014, 11:30:18 AM

Title: How to design a function like Clojure 'iterate'
Post by: ssqq on June 18, 2014, 11:30:18 AM
Following code could make a infinite sequence:
   (define (f)
    (begin
        (println (inc cnt))
        (push (last f) f -1)
        (if (> (length f) 3) (pop f 1))))


But it is not lazy list:


(first (f)) --> not 1

How to design a infinite sequence just like iterate and take in Clojure.



(take 10 (iterate inc 5))
Title: Re: How to design a function like Clojure 'iterate'
Post by: rickyboy on June 18, 2014, 01:18:14 PM
QuoteHow to design a infinite sequence just like iterate and take in Clojure.



(take 10 (iterate inc 5))

Here you go. :)


> (sequence 5 14)
(5 6 7 8 9 10 11 12 13 14)

^^ This is just a way of saying, "Why do you want to do that?"  newLISP is inherently eager, and doing such a thing in the most straight-forward way is the best.  Unless, we are not privy to something ...
Title: Re: How to design a function like Clojure 'iterate'
Post by: ssqq on June 19, 2014, 03:01:01 AM
richyboy, yes, you are right. I think so.
Title: Re: How to design a function like Clojure 'iterate'
Post by: cormullion on June 19, 2014, 08:42:58 AM
some previous discussion (//http)
Title: Re: How to design a function like Clojure 'iterate'
Post by: TedWalther on June 19, 2014, 05:51:36 PM
I would do it using a context to store internal state, each call to the context/function would update the state and return it.  Look at the implementations of gensym on here for ideas.