-
Notifications
You must be signed in to change notification settings - Fork 1
/
Preface.elm
73 lines (58 loc) · 1.95 KB
/
Preface.elm
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
72
73
module Preface where
isEmpty : [a] -> Bool
isEmpty xs = case xs of
[] -> True
(_::_) -> False
repeat : a -> Int -> [a]
repeat x n = if | n <= 0 -> []
| otherwise -> x::repeat x (n-1)
replicate : Int -> a -> [a]
replicate n x = repeat x n
isEven : Int -> Bool
isEven n = n `mod` 2 == 0
isOdd : Int -> Bool
isOdd = not . isEven
takeWhile : (a -> Bool) -> [a] -> [a]
takeWhile p xs = case xs of
[] -> []
(h::t) -> if | p h -> h :: takeWhile p t
| otherwise -> []
dropWhile : (a -> Bool) -> [a] -> [a]
dropWhile p xs = case xs of
[] -> []
(h::t) -> if | p h -> dropWhile p t
| otherwise -> xs
init : [a] -> [a]
init xs = case xs of
[] -> []
[x] -> []
(h::t) -> h :: init t
tail : [a] -> [a]
tail xs = case xs of
[] -> []
(h::t) -> t
(#) : [a] -> Int -> Maybe a
xs # n = case xs of
[] -> Nothing
(h::t) -> if | n <= 0 -> Nothing
| n == 1 -> Just h
| otherwise -> t # (n-1)
iterate : (a -> a) -> a -> Int -> [a]
iterate f x n = if | n <= 0 -> []
| n == 1 -> [x]
| otherwise -> x :: iterate f (f x) (n-1)
cycle : [a] -> Int -> [a]
cycle xs n = if | n <= 0 -> []
| n == 1 -> xs
| otherwise -> xs ++ cycle xs (n-1)
find : (a -> Bool) -> [a] -> Maybe a
find p xs = case xs of
[] -> Nothing
h::t -> if | p h -> Just h
| otherwise -> find p t
intercalate : [a] -> [[a]] -> [a]
intercalate xs xss = xss |> intersperse xs |> concat
unfoldr : (b -> Maybe (a, b)) -> b -> [a]
unfoldr f b = case f b of
Just (a, b') -> a :: unfoldr f b'
Nothing -> []