-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-factorial.hth
More file actions
28 lines (21 loc) · 806 Bytes
/
fix-factorial.hth
File metadata and controls
28 lines (21 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# This defines a fixed-point combinator, showing that Haskenthetical doesn't
# need `letrec` to do recursion. (`def` can be used recursively, but that's not
# necessary for this example.)
#
# I can't claim to understand it. It's based on the example at
# http://rosettacode.org/wiki/Y_combinator#Elm, which I also can't claim to
# understand, even though I wrote it myself.
(def (: id (-> $a $a)) (λ x x))
(type (Mu $a $b) (Roll (-> (Mu $a $b) (-> $a $b))))
(def (: unroll (» -> (Mu $a $b) (Mu $a $b) $a $b))
(elim-Mu id))
(def (: fix (-> (» -> (-> $a $b) $a $b)
(-> $a $b)))
(λ f (let ((g (λ r (f (λ v (unroll r r v))))))
(g (Roll g)))))
(def (: fac (-> Float Float))
(fix (λ (f n)
(if~ n 0
1
(* n (f (- n 1)))))))
(fac 5)