-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stuff.elm
85 lines (63 loc) · 1.41 KB
/
Stuff.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
74
75
76
77
78
79
80
81
82
83
84
85
module Stuff exposing (..)
map : (a -> b) -> List a -> List b
map fn list =
case list of
[] ->
[]
x :: xs ->
fn x :: map fn xs
repeat : Int -> a -> List a
repeat num a =
if num <= 0 then
[]
else
a :: repeat (num - 1) a
take : Int -> List a -> List a
take num list =
if num <= 0 then
[]
else
case list of
[] ->
[]
x :: xs ->
x :: take (num - 1) xs
reverse : List a -> List a
reverse list =
case list of
[] ->
[]
x :: xs ->
reverse xs ++ [ x ]
zip : List a -> List b -> List ( a, b )
zip uno dos =
case ( uno, dos ) of
( _, [] ) ->
[]
( [], _ ) ->
[]
( x :: xs, y :: ys ) ->
[ ( x, y ) ] ++ zip xs ys
filter : (a -> Bool) -> List a -> List a
filter fn list =
case list of
[] ->
[]
x :: xs ->
if fn x then
x :: filter fn xs
else
filter fn xs
sort : List Int -> List Int
sort list =
case list of
[] ->
[]
x :: xs ->
let
smaller =
sort (filter (\n -> n < x) xs)
greater =
sort (filter (\n -> n > x) xs)
in
smaller ++ [ x ] ++ greater