vstte10_search_list.mlw 633 Bytes
Newer Older
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
(* VSTTE'10 competition 
   Problem 3: searching a linked list *)

{
  use export list.List
  use export list.Length
  use export option.Option

(*
  logic nth (n : int) (l : list 'a) = match l with
   | Nil -> None
   | Cons x r -> if n = 0 then Some x else nth (n - 1) r
  end
*)
}

let rec search i l = 
  match l with
  | Nil -> i
  | Cons x r -> if x = 0 then i else search (i+1) r
  end

let search_list l =
  { }
  search 0 l
  { (0 <= result < length l (* nth result l = 0*)) or 
    (result = length l) }


(*
Local Variables: 
compile-command: "unset LANG; make -C ../.. examples/programs/vstte10_search_list.gui"
End: 
*)