ML reference type and mutable data


(* checking if two lists are equal *)
let rec list_equals  = function
    ([],[]) -> true
  |([],_) -> false
  |(_,[]) -> false
  |(x::xs,y::ys) -> if x = y then list_equals (xs, ys) else false;;

list_equals ([],[]);;

list_equals ([7], []);;

list_equals ([7;2], [7]);;

list_equals ([7;2], [7;2]);;

(* Pattern matching cannot check for equality of variables *)
(* THIS IS NOT GOING TO WORK: *)
let rec list_equals  = function
    ([],[]) -> true
  |([],_) -> false
  |(_,[]) -> false
  |(x::xs,x::ys) -> list_equals (xs, ys)
  | _ -> false;;

CSci 4651 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.