Question

I have the code

(define alternate
(letrec ([f (lambda (x) (cons x (lambda () (f (+ x 1)))))])
(lambda () (f 1))))

The result is 1,2,3.. How i could change it to take 1,2,1,2,1,2..

I tried cons inside the f but didn't work. Any ideas?

Was it helpful?

Solution 3

Here's a way to do it as a small modification of your existing code:

(define alternate
  (letrec ([f (lambda (x) (cons x (lambda () (f (if (= x 1) 2 1)))))])
    (lambda () (f 1))))

OTHER TIPS

You might also find generators useful: docs

Welcome to DrRacket, version 5.3.3.5 [3m].
Language: racket [custom].
> (require racket/generator)
> (define g (generator () (let LOOP () (yield 1) (yield 2) (LOOP))))
> (g)
1
> (g)
2
> (g)
1
> (g)
2

UPDATE:

Even better, use an infinite-generator:

Welcome to DrRacket, version 5.3.3.5 [3m].
Language: racket [custom].
> (require racket/generator)
> (define g (infinite-generator (yield 1) (yield 2)))
> (g)
1
> (g)
2
> (g)
1
> (g)
2

This is straightforward to implement using streams:

(define (alternate)
  (stream-map (lambda (x)
                (if (even? x) 1 2))
              (in-naturals)))

The trick here is that a stream is built using stream-cons, which basically does what you're implementing by hand: it creates a list where its elements are "promises" that get evaluated only when needed.

stream-cons produces a lazy stream for which stream-first forces the evaluation of first-expr to produce the first element of the stream, and stream-rest forces the evaluation of rest-expr to produce a stream for the rest of the returned stream.

This shows how alternate returns an infinite stream of elements of the form 1 2 1 2 1 2 ...

(define alt (alternate))

(stream-ref alt 0)
=> 1
(stream-ref alt 1)
=> 2
(stream-ref alt 2)
=> 1
(stream-ref alt 3)
=> 2

Alternatively, if you need a list of n elements of the sequence use this procedure, which by the way should be part of Racket in the first place:

(define (stream-take s n)
  (if (zero? n)
      '()
      (cons (stream-first s)
            (stream-take (stream-rest s) (sub1 n)))))

Now it works as expected:

(define alt (alternate))

(stream-take alt 0)
=> '()
(stream-take alt 1)
=> '(1)
(stream-take alt 2)
=> '(1 2)
(stream-take alt 3)
=> '(1 2 1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top