[project @ 2003-07-31 10:48:50 by panne]
[ghc-base.git] / Text / ParserCombinators / Parsec / examples / tiger / merge.tig
1 let 
2
3  type any = {any : int}
4  var buffer := getchar()
5
6  function readint(any: any) : int =
7  let var i := 0
8      function isdigit(s : string) : int = 
9           ord(buffer)>=ord("0") & ord(buffer)<=ord("9")
10      function skipto() =
11        while buffer=" " | buffer="\n"
12          do buffer := getchar()
13   in skipto();
14      any.any := isdigit(buffer);
15      while isdigit(buffer)
16        do (i := i*10+ord(buffer)-ord("0"); buffer := getchar());
17      i
18  end
19
20  type list = {first: int, rest: list}
21
22  function readlist() : list =
23     let var any := any{any=0}
24         var i := readint(any)
25      in if any.any
26          then list{first=i,rest=readlist()}
27          else nil
28     end
29
30  function merge(a: list, b: list) : list =
31    if a=nil then b
32    else if b=nil then a
33    else if a.first < b.first 
34       then list{first=a.first,rest=merge(a.rest,b)}
35       else list{first=b.first,rest=merge(a,b.rest)}
36
37  function printint(i: int) =
38   let function f(i:int) = if i>0 
39              then (f(i/10); print(chr(i-i/10*10+ord("0"))))
40    in if i<0 then (print("-"); f(-i))
41       else if i>0 then f(i)
42       else print("0")
43   end
44
45  function printlist(l: list) =
46    if l=nil then print("\n")
47    else (printint(l.first); print(" "); printlist(l.rest))
48
49    var list1 := readlist()
50    var list2 := (buffer:=getchar(); readlist())
51
52
53   /* BODY OF MAIN PROGRAM */
54  in printlist(merge(list1,list2))
55 end
56