10 Corecursion ぱらぱらめくる『The Haskell Road to Logic, Maths and Programming』

ones = 1 : ones -- [1,1,1,1,1,...]
nats = 0: map (+1) nats -- [0,1,2,...]
nats !! 10^100-2 -- 999999999999999999999...98
odds = 1 : map (+2) odds

iterate :: (a -> a) -> a -> [a]
iterage f x = x : iterate f (f x)

theOnes = iterate id 1
theNats = iterate (+1) 0
theOdds = iterate (+2) 1

theNats1 = 0 : zipWith (+) ones theNats1

theFibs = 0 : 1 : zipWith (+) theFibs (tail the Fibs)
  • 乱数列
module COR
where
import System.Random (mkStdGen,randomRs)


randomInts :: Int -> Int -> [Int]
randomInts bound seed =
   tail (randomRs (0,bound) (mkStdGen seed))
let rr = randomInts 12424141 21442
rr !! 0
rr !! 1