Skip to content

Instantly share code, notes, and snippets.

@qzdc00
Created November 25, 2014 18:36
Show Gist options
  • Save qzdc00/5bc979e9088b6ff36d4a to your computer and use it in GitHub Desktop.
Save qzdc00/5bc979e9088b6ff36d4a to your computer and use it in GitHub Desktop.
hw3/WashingtonPLMOOC
(* Coursera Programming Languages, Homework 3, Provided Code *)
exception NoAnswer
datatype pattern = Wildcard
| Variable of string
| UnitP
| ConstP of int
| TupleP of pattern list
| ConstructorP of string * pattern
datatype valu = Const of int
| Unit
| Tuple of valu list
| Constructor of string * valu
fun g f1 f2 p =
let
val r = g f1 f2
in
case p of
Wildcard => f1 ()
| Variable x => f2 x
| TupleP ps => List.foldl (fn (p,i) => (r p) + i) 0 ps
| ConstructorP(_,p) => r p
| _ => 0
end
(**** for the challenge problem only ****)
datatype typ = Anything
| UnitT
| IntT
| TupleT of typ list
| Datatype of string
(**** you can put all your code here ****)
fun only_capitals strl = List.filter (fn str => Char.isUpper(String.sub(str, 0))) strl
fun foldl (f, acc, xs) =
case xs of
[] => acc
| x :: xs => foldl(f, f(acc, x), xs)
fun longest_string1 strl = foldl((fn (max, x) => if String.size max >= String.size x then max else x), "", strl)
fun longest_string2 strl = foldl((fn (max, x) => if String.size max > String.size x then max else x), "", strl)
fun longest_string_helper f strl = foldl((fn (max, x) => if f(String.size x, String.size max) then x else max), "", strl)
val longest_string3 = longest_string_helper (fn (x, max) => x > max)
val longest_string4 = longest_string_helper (fn (x, max) => x >= max)
val longest_capitalized = longest_string3 o only_capitals
val rev_string = String.implode o List.rev o String.explode
fun first_answer f lst =
case lst of
[] => raise NoAnswer
| x :: lst' => case f x of
SOME ans => ans
| NONE => first_answer f lst'
fun all_answers f lst =
let fun aux(f, lst, acc) =
case lst of
[] => acc
| x :: lst' => case f x of
NONE => NONE
| SOME lstn => case acc of
NONE => NONE
| SOME l => aux(f, lst', SOME (l @ lstn))
in
aux(f, lst, SOME [])
end
fun count_wildcards p = g (fn () => 1) (fn x => 0) p
fun count_wild_and_variable_lengths p = g (fn () => 1) (fn x => String.size x) p
fun count_some_var (str, p) = g (fn () => 0) (fn x => if str = x then 1 else 0) p
fun check_pat p =
let fun aux1 p =
case p of
Variable x => [x]
| TupleP ps => List.foldl (fn (v,vs) => vs @ aux1(v)) [] ps
| ConstructorP(_,p) => aux1(p)
| _ => []
fun aux2 strl =
case strl of
[] => true
| s :: strl' => if List.exists (fn x => x = s) strl' then false else aux2 strl'
in
aux2(aux1(p))
end
(*....*)
fun match vp =
case vp of
(_, Wildcard) => SOME []
| (v, Variable s) => SOME [(v, s)]
| (Unit, UnitP) => SOME []
| (Const y, ConstP x) => SOME []
| (Tuple vs, TupleP ps) => if List.length ps = List.length vs
then all_answers (fn (c, d) => match (c, d)) (ListPair.zip (vs, ps))
else NONE
| (Constructor(s2, v), ConstructorP(s1, p)) => if s1 = s2 then match (v, p) else NONE
| _ => NONE
fun first_match v plst =
SOME (first_answer (fn p => match (v, p)) plst)
handle NoAnswer => NONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment