{- Siin on osa loengus esitatud näiteid laisa väärtustamise kohta. -} -- Arvu tükeldamine numbriteks digits :: Int -> [Int] digits = reverse . map (`mod` 10) . takeWhile (/= 0) . iterate (`div` 10) -- Ruutjuur Newton-Raphson'i meetodil sqrt1 n = within eps (iterate (next n) a0) where eps = 0.00001 a0 = 1.0 next n x = (x + n/x)/2.0 within eps (x1 : x2 : xs) | abs (x1 - x2) <= eps = x2 | otherwise = within eps (x2 : xs) -- Algarvud Erathostenese sõela abil primes = map head (iterate sieve [2..]) where sieve (p:xs) = [ x | x <- xs, x `mod` p /= 0] -- Algarvud tsüklilise listina primes' = 2 : [ n | n <- [3,5..], null (factors n)] factors n = [ i | i <- takeWhile (\ p -> p*p < n) primes', n `mod` i == 0] -- Hammingu jada hamming = 1 : ( map (2*) hamming || map (3*) hamming || map (5*) hamming) where (x:xs) || (y:ys) | x == y = x : (xs || ys) | x < y = x : (xs || (y:ys)) | x > y = y : (ys || (x:xs)) -- Liita listi kõigile elementidele listi minimaalne element -- Kahekordse läbivaatusega versioon incmin2 xs = map (minv+) xs where minv = minimum xs --Ühekordse läbivaatusega versioon incmin1 xs = newlist where (minv,newlist) = onepass xs onepass [a] = (a, [a+minv]) onepass (a:as) = (a `min` b, (a+minv):bs) where (b,bs) = onepass as