-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind.robin
35 lines (26 loc) · 1.01 KB
/
find.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
;'<<SPEC'
-> Tests for functionality "Evaluate Robin Expression (with List)"
`find` evaluates its first argument to obtain a predicate, then evaluates
its second argument to obtain a list. It then evaluates to a list which
is either empty, if no element of the list satisfies the predicate, or
a list which contains exactly one element, which will be the first
element from the list which satisfies the predicate.
| (find (fun (x) (symbol? x)) ())
= ()
| (find (fun (x) (symbol? x)) (list 1 2 3))
= ()
| (find (fun (x) #t) (list 1 2 3))
= (1)
| (find (fun (x) (symbol? x)) (literal (1 two #f 3 () four 5 six)))
= (two)
`find` could be defined in terms of `filter`, but in practice it would
be implemented in a way which need not examine the entire list.
'<<SPEC'
(define find (fun (pred li)
(bind find-r (fun (self pred li)
(if (empty? li)
()
(if (pred (head li))
(list (head li))
(self self pred (tail li)))))
(find-r find-r pred li))))