## How to design a function like Clojure 'iterate'

Q&A's, tips, howto's

### How to design a function like Clojure 'iterate'

Following code could make a infinite sequence:
Code: Select all
`    (define (f)    (begin        (println (inc cnt))        (push (last f) f -1)        (if (> (length f) 3) (pop f 1))))`

But it is not lazy list:

Code: Select all
`(first (f)) --> not 1`

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

(take 10 (iterate inc 5))
ssqq

Posts: 88
Joined: Sun May 04, 2014 12:49 pm

### Re: How to design a function like Clojure 'iterate'

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

(take 10 (iterate inc 5))

Here you go. :)

Code: Select all
`> (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 ...
(λx. x x) (λx. x x)
rickyboy

Posts: 584
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

### Re: How to design a function like Clojure 'iterate'

richyboy, yes, you are right. I think so.
ssqq

Posts: 88
Joined: Sun May 04, 2014 12:49 pm

### Re: How to design a function like Clojure 'iterate'

cormullion

Posts: 2037
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W

### Re: How to design a function like Clojure 'iterate'

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.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther

Posts: 605
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Return to newLISP in the real world

### Who is online

Users browsing this forum: No registered users and 1 guest