-
Notifications
You must be signed in to change notification settings - Fork 9
/
sort.ast
37 lines (33 loc) · 1021 Bytes
/
sort.ast
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
------------------------------------------------------------------
-- sort.ast
--
-- defines a parameterized sort function over a list. it uses
-- a user defined order predicate on elements of the list to
-- perform the sort. the underlying sort algorithm is the
-- Quicksort.
--
-- Example:
-- sort((lambda with (x,y) do return true if x<y else false),
-- [10,5,110,50]).
--
-- (c) University of Rhode Island
------------------------------------------------------------------
------------------------------------------------------------------
function sort
------------------------------------------------------------------
with (_,[]) do
return [].
with (_,[a]) do
return [a].
with (p,[pivot|rest]) do
let less=[].
let more=[].
for e in rest do
if p(e,pivot) do
let less = less + [e].
else
let more = more + [e].
end
end
return sort(p,less) + [pivot] + sort(p,more).
end