-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpract9.hs
71 lines (50 loc) · 1.76 KB
/
pract9.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
cartesian :: [a] -> [b] -> [(a,b)]
cartesian xs ys = [(x,y) | x<-xs , y <-ys]
iterate' :: (a->a) -> a -> [a]
iterate' f x = x : iterate' f (f x)
nats' :: [Integer]
nats' = iterate' succ 0
divides :: Integer -> Integer -> Bool
divides x y = mod y x == 0
divisors :: Integer -> [Integer]
divisors x = [y | y <- [1..x], mod x y == 0]
isPrime :: Integer -> Bool
isPrime x = divisors x == [1, x]
primes :: [Integer]
primes = [y | y <- [1..], (length (divisors y)) == 2]
eratosthenes :: [Integer] -> [Integer]
--eratosthenes xs = [y | y <- (filter (\x -> divides y x == False) xs)]
eratosthenes (x:xs) = x : eratosthenes (filter (\y -> not (divides x y)) xs)
positive2Tuples :: [(Integer, Integer)]
positive2Tuples = [x | i <- [1..], x <- partitionPos i]
partitionPos :: Integer -> [(Integer, Integer)]
partitionPos n = [(x,y) | x <- [1..n-1], y <- [n-x]]
fact :: Integer -> Integer
fact 1 = 1
fact x = x * (fact (x-1))
facts :: [Integer]
facts = [x | i <- [1..], x <- [fact i]]
fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
fibs :: [Integer]
--fibs = [fib i | i <- [1..]]
fibs = map fib [0..]
foldl' :: (b -> a -> b) -> b -> [a] -> b
foldl' f nv [] = nv
foldl' f nv (x:xs) = foldl' f (f nv x) xs
scanl' :: (b -> a -> b) -> b -> [a] -> [b]
scanl' f nv [] = [nv]
scanl' f nv (x:xs) = nv : scanl' f (f nv x) xs
zip' :: [a] -> [b] -> [(a,b)]
zip' [] ys = []
zip' xs [] = []
zip' (x:xs) (y:ys) = (x,y):zip' xs ys
zipWith' :: [a] -> [b] -> (a -> b -> b) -> [b]
zipWIth' [] ys f = []
zipWith' xs [] f = []
zipWith' (x:xs) (y:ys) f = (f x y) : zipWith' xs ys f
fact' n = (scanl (\ x y -> x * y) 1 [1..n])
facts' :: [Integer]
facts' = map fact' [1..]