fun-programming-language

An imperative-style function that flattens a list of integers and adds all of them.

val flatsumimp(xs) =
  let val ys = new() in
  let val s  = new() in
  ys := xs;
  s  := 0;
  while !ys <> nil do (
    if integer(head(!ys))
    then (
         s := !s + head(!ys);
         ys := tail(!ys)
    )
    else ys := append(head(!ys), tail(!ys)) 
  );
  !s;;

Tags: programming languages, functional programming, OCaml.