view demo/list.ur @ 2283:4afaab523213

Initialize invalidation to NULL!
author Ziv Scully <ziv@mit.edu>
date Thu, 12 Nov 2015 10:06:07 -0500
parents 669ac5e9a69e
children
line wrap: on
line source
datatype list t = Nil | Cons of t * list t

fun length [t] (ls : list t) =
    let
        fun length' (ls : list t) (acc : int) =
            case ls of
                Nil => acc
              | Cons (_, ls') => length' ls' (acc + 1)
    in
        length' ls 0
    end

fun rev [t] (ls : list t) = 
    let
        fun rev' (ls : list t) (acc : list t) =
            case ls of
                Nil => acc
              | Cons (x, ls') => rev' ls' (Cons (x, acc))
    in
        rev' ls Nil
    end