-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflatten.robin
37 lines (28 loc) · 1.05 KB
/
flatten.robin
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
;'<<SPEC'
-> Tests for functionality "Evaluate Robin Expression (with List)"
`flatten` evaluates its first argument to obtain a list, then evaluates
to the list obtained by interpolating all elements into a single list.
By interpolating we mean that, if some element is itself a list, the
individual elements of that list will be present, in the same order, in
the corresponding position, in the resulting list, and that this process
is applied recursively to any elements in sublists which are themselves
sublists.
| (flatten ())
= ()
| (flatten (list 1 2 3))
= (1 2 3)
| (flatten (list 1 (list 2 3 4) 5))
= (1 2 3 4 5)
| (flatten (list 1 (list 2 3 (list 4 4 4)) 5))
= (1 2 3 4 4 4 5)
| (flatten (list 1 () 5))
= (1 5)
'<<SPEC'
(define flatten (fun (li)
(bind flatten-r (fun (self li acc)
(if (empty? li)
acc
(if (list? (head li))
(self self (tail li) (self self (head li) acc))
(self self (tail li) (prepend (head li) acc)))))
(reverse (flatten-r flatten-r li ())))))