(*
** Course: Concepts of Programming Languages (BU CAS CS 320)
** Semester: Summer I, 2009
** Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu)
*)

//
// Author: Hongwei Xi (hwxi AT cs DOT bu DOT edu)
// Time: May 21, 2009
//

(* ****** ****** *)

#include "BUCASCS320.hats"

(* ****** ****** *)

#define :: list0_cons
#define cons list0_cons
#define nil list0_nil

(* ****** ****** *)

extern fun{elt:t@ype} list0_length (xs: list0 elt): int

implement{elt}
  list0_length (xs) = loop (xs, 0) where {
  fun loop (xs: list0 elt, res: int): int =
    case+ xs of
    | cons (_, xs1) => loop (xs1, res + 1)
    | nil () => res
} // end of [list0_length]

(* ****** ****** *)

extern fun{elt:t@ype}
  list0_append (xs: list0 elt, ys: list0 elt): list0 elt

implement{elt} // functional append
  list0_append (xs, ys) = append (xs, ys) where {
  fun append (xs: list0 elt, ys: list0 elt): list0 elt =
  case+ xs of
  | list0_cons (x, xs) => list0_cons (x, append (xs, ys))
  | list0_nil () => ys
} // end of [list0_append]

(* ****** ****** *)

extern fun{a,b:t@ype}
  list0_zip (xs: list0 a, ys: list0 b): list0 @(a, b)

implement{a,b}
  list0_zip (xs, ys) = case (xs, ys) of
  | (list0_cons (x, xs), list0_cons (y, ys)) =>
     list0_cons ((x, y), list0_zip (xs, ys))
  | (list0_nil _, _) => list0_nil ()
  | (_, list0_nil _) => list0_nil ()
// end of [list0_zip]

(* ****** ****** *)

typedef charlst = list0 char

extern fun
string_append (s1: string, s2: string): string

implement string_append (s1, s2) = let
  val cs1 = string_explode (s1)
  val cs2 = string_explode (s2)
  val cs = list0_append<char> (cs1, cs2)
in
  string_implode (cs)
end // end of [string_append]

(* ****** ****** *)

(* end of [code-2009-05-21.dats] *)