Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132944 views
License: OTHER
1
map :: (a -> b) -> [a] -> [b]
2
map f [] = []
3
map f (x:xs) = f x : map f xs
4
----------
5
6
zipWith :: (a->b->c) -> [a]->[b]->[c]
7
zipWith z (a:as) (b:bs)
8
= z a b : zipWith z as bs
9
zipWith _ _ _ = []
10
----------
11
12
zip :: [a] -> [b] -> [(a,b)]
13
zip = zipWith (,)
14
----------
15
16
unzip :: [(a,b)] -> ([a],[b])
17
unzip = foldr (\(a,b) ~(as,bs) -> (a:as,b:bs)) ([],[])
18
----------
19
20
foldl :: (a -> b -> a) -> a -> [b] -> a
21
foldl f z [] = z
22
foldl f z (x:xs) = foldl f (f z x) xs
23
----------
24
25
foldr :: (a -> b -> b) -> b -> [a] -> b
26
foldr f z [] = z
27
foldr f z (x:xs) = f x (foldr f z xs)
28
----------
29
30
take :: Int -> [a] -> [a]
31
take n _ | n <= 0 = []
32
take _ [] = []
33
take n (x:xs) = x : take (n-1) xs
34
----------
35
36
splitAt :: Int -> [a] -> ([a],[a])
37
splitAt n xs = (take n xs, drop n xs)
38
----------
39