wiki:Functions/Repeat

repeat

In a strict languages, like Scheme we need the whole Stream ADT to be defined first.

(define-syntax stream-cons
  (syntax-rules ()
    ((_ x xs)
     (cons x (delay xs)))))

(define (stream-cdr xs)
  (force (cdr xs)))

(define (stream-take n xs)
  (if (<= n 0)
      '()
      (cons (car xs) (stream-take (- n 1) (stream-cdr xs)))))

And then

(define (repeat x)
  (stream-cons x (repeat x)))

(define (replicate n x)
  (stream-take n (repeat x)))

(define (iterate f x)
  (stream-cons x (iterate f (f x))))

While in Haskell every expression is an implicit Promise or so called thunk.

repeat :: a -> [a]
repeat x  =  xs where xs = x : xs
replicate :: Int -> a -> [a]
replicate n x  =  take n (repeat x)
iterate :: (a -> a) -> a -> [a]
iterate f x = x : iterate f (f x)
Last modified 2 years ago Last modified on Nov 15, 2018, 11:34:56 AM
Note: See TracWiki for help on using the wiki.