W preview
[ap2015.git] / W / examples / sample.w
1 ># sample.w
2 #
3 # sneek preview for the world!
4 #<
5
6 import StdEnv
7
8 typedef Tree a := Tip | Bin a (Tree a) (Tree a)
9
10 # functions are invoked as 'insert(x, y)'
11 func insert : a (Tree a) -> (Tree a)
12 insert(x, Tip) = Bin x Tip Tip
13 insert(x, Bin y l r) | x < y = Bin y insert(x, l) r
14 | otherwise = Bin y l insert(x, r)
15
16 # prefix operators work like normal functional functions
17 op prefix contains : a (Tree a) -> B
18 contains x Tip = False
19 contains x (Bin y l r) | x == y = True
20 | x < y = contains x l
21 | otherwise = contains x r
22
23 op prefix len : [a] -> N
24 len [] = 0
25 len [_:xs] = 1 + (len xs)
26
27 # currently no way to specify binding strenght
28 op infix -- : N N -> B
29 -- x y | x < y = True
30 | otherwise = False
31
32 func max : N N -> N
33 max(x, y) | x -- y = y
34 | y -- x = x
35 | otherwise = y # equals
36