From: jim Date: Sat, 21 Mar 2015 08:48:34 +0000 (-0400) Subject: add some stuff X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=fdca7e1fb19e83509ce56851768d8ce827fafbed add some stuff --- diff --git a/exercises/assignment5_answers.mdwn b/exercises/assignment5_answers.mdwn index 2b29695a..b591330b 100644 --- a/exercises/assignment5_answers.mdwn +++ b/exercises/assignment5_answers.mdwn @@ -546,11 +546,6 @@ any type `α`, as long as your function is of type `α -> α` and you have a bas It's especially useful for you to implement a version of a System F encoding `pred`, starting with one of the (untyped) versions available in [[assignment3 answers]]. - ------ -`sysf_bool`, `sysf_true`, `sysf_false`, -`sysf_nat`, `sysf_zero`, `sysf_iszero`, `sysf_succ`, and `sysf_pred`. - OCAML ANSWERS: type ('a) sysf_bool = 'a -> 'a -> 'a @@ -559,13 +554,29 @@ any type `α`, as long as your function is of type `α -> α` and you have a bas type ('a) sysf_nat = ('a -> 'a) -> 'a -> 'a let sysf_zero : ('a) sysf_nat = fun s z -> z + let sysf_iszero (n : 'a sysf_nat) : 'b sysf_bool = n (fun _ -> sysf_false) sysf_true (* Annoyingly, though, if you just say sysf_iszero sysf_zero, you'll get an answer that isn't fully polymorphic. This is explained in the comments linked below. The way to get a polymorphic result is to say instead `fun next -> sysf_iszero sysf_zero next`. *) + let sysf_succ (n : 'a sysf_nat) : 'a sysf_nat = fun s z -> s (n s z) (* Again, to use this you'll want to call `fun next -> sysf_succ sysf_zero next` *) - let sysf_pred (n : 'a sysf_nat) : 'a sysf_nat = (* LEFT UNDONE *) + + let sysf_pred (n : 'a sysf_nat) : 'a sysf_nat = (* NOT DONE *) + + Consider the following list type, specified using OCaml or Haskell datatypes: