Repository navigation
Expand file tree
/
Copy pathsorting.hs
More file actions
165 lines (109 loc) · 2.53 KB
/
Copy pathsorting.hs
File metadata and controls
165 lines (109 loc) · 2.53 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
:{
let divide [] = []
divide (x:xs) = [x] : divide xs
:}
let altDivide = map (:[])
altDivide [1,5,2,65,3,6]
m $ altDivide [1,5,2,65,3,6]
mergetilDone $ divide [1,5,2,65,3,6]
mergetilDone $ divide [1,5,2,65,3,7,6]
let asdf = mergetilDone $ mergetilDone $ divide [1,5,2,65,3,7,6]
asdf
merge (head asdf) (head $ tail asdf)
mergetilDone asdf
-- mergesort
:{
let m [x] = x
m y@(x:xs) = m (mergetilDone y)
:}
:{
let mergetilDone [] = []
mergetilDone [x] = [x]
mergetilDone (x:y:xs) = merge x y : mergetilDone xs
:}
m
:{
let merge x [] = x
merge [] y = y
merge (x:xs) (y:ys)
| x < y = x : merge xs (y:ys)
| otherwise = y : merge (x:xs) ys
:}
merge [1,3,4,8] [2,5,7]
merge [3] []
merge [] []
-- this is a degenerate selection sort which fails when the initial conditions are such that the last element is the greatest
:{
let selectionSort xs@(x:xs')
| (mxL xs) == (last xs) = xs
| otherwise = m :selectionSort rst
where m = minL xs
rst = noSmall xs
:}
selectionSort [1,3,2,5,4]
selectionSort [1,3,2,5,4,6]
selectionSort [1..4]
:{
let last [x] = x
last (x:xs) = last xs
:}
-- get rid of smallest element in list
let noSmall y@(x:xs) = filter (/= minL y) y
3 /= 1
noSmall [1..5]
noSmall [3,1,5]
filter (<3) [1..5]
:{
let qs [] = []
qs (x:xs) = left ++ [x] ++ right
where left = qs $ filter (<x) xs
right = qs $ filter (>x) xs
:}
qs [1,4,3,2]
maxL [1,4,3,2]
:{
let maxL [x,y] = max x y
maxL (x:y:xs)
| x > y = maxL (x:xs)
| otherwise = maxL (y:xs)
:}
let infinity = (read "Infinity")::Double
infinity > 10e1000
3e10
:{
let mxL [x] = x
mxL (x:xs) = max x $ mxL xs
:}
:{
let minL [x] = x
minL (x:xs) = min x $ minL xs
:}
:t mxL
mxL [333,4,31,1,33]
:{
let maxmin x y f
| f x y = x
| otherwise = y
:}
let max' x y = maxmin x y (>)
let min' x y = maxmin x y (<)
max' 3 4
foldl (max') (-infinity) [1,4,23,2,493,24,(-39293)]
Possibility of having parameterized definitions in haskell?
Is there a way to compactly write multiple definitions in haskell via case, without having to repeat, other than the input parameters, the exact same syntax?
Example of defining binary max and min functions. Can we compress
max' x y
| x > y = x
| otherwise = y
min' x y
| x < y = x
| otherwise = y
into something like
(max',min') x y
| x (>,<) y = x
| otherwise = y
maxmin x y f
| f x y = x
| otherwise = y
max' x y = maxmin x y (>)
min' x y = maxmin x y (<)