|
|
@@ -7,41 +7,35 @@ fn cons<A>(x: A, xs: List<A>) -> List<A>
|
|
|
|
|
|
fn is_empty<A>(xs: List<A>) -> Bool = xs == []
|
|
|
|
|
|
-# We definitely want to make the following functions generic, but this is not yet possible.
|
|
|
-
|
|
|
-fn generate(n: Scalar, f: Fn[() -> Scalar]) -> List<Scalar> =
|
|
|
+fn generate<A>(n: Scalar, f: Fn[() -> A]) -> List<A> =
|
|
|
if n == 0
|
|
|
then []
|
|
|
else cons(f(), generate(n - 1, f))
|
|
|
|
|
|
-fn map(f: Fn[(Scalar) -> Scalar], xs: List<Scalar>) -> List<Scalar> =
|
|
|
- if len(xs) == 0
|
|
|
+fn map<A, B>(f: Fn[(A) -> B], xs: List<A>) -> List<B> =
|
|
|
+ if is_empty(xs)
|
|
|
then []
|
|
|
else cons(f(head(xs)), map(f, tail(xs)))
|
|
|
|
|
|
-fn cons_end(xs: List<Scalar>, x: Scalar) -> List<Scalar> =
|
|
|
+fn cons_end<A>(xs: List<A>, x: A) -> List<A> =
|
|
|
if is_empty(xs)
|
|
|
then [x]
|
|
|
else cons(head(xs), cons_end(tail(xs), x))
|
|
|
|
|
|
-fn reverse(xs: List<Scalar>) -> List<Scalar> =
|
|
|
- if len(xs) == 0
|
|
|
+fn reverse<A>(xs: List<A>) -> List<A> =
|
|
|
+ if is_empty(xs)
|
|
|
then []
|
|
|
else cons_end(reverse(tail(xs)), head(xs))
|
|
|
|
|
|
fn sequence(n: Scalar) -> List<Scalar> =
|
|
|
- if n == 1
|
|
|
- then [0]
|
|
|
+ if n == 0
|
|
|
+ then []
|
|
|
else cons_end(sequence(n - 1), n - 1)
|
|
|
|
|
|
-fn foldl(f: Fn[(Scalar, Scalar) -> Scalar], acc: Scalar, xs: List<Scalar>) -> Scalar =
|
|
|
- if len(xs) == 1
|
|
|
- then f(acc, head(xs))
|
|
|
+fn foldl<A, B>(f: Fn[(A, B) -> A], acc: A, xs: List<B>) -> A =
|
|
|
+ if is_empty(xs)
|
|
|
+ then acc
|
|
|
else foldl(f, f(acc, head(xs)), tail(xs))
|
|
|
|
|
|
-fn const_5() -> Scalar = 5
|
|
|
-
|
|
|
-fn inc(x: Scalar) -> Scalar = x + 1
|
|
|
-
|
|
|
-fn add(x: Scalar, y: Scalar) -> Scalar = x + y
|
|
|
-fn mul(x: Scalar, y: Scalar) -> Scalar = x * y
|
|
|
+fn add(x, y) = x + y # TODO
|
|
|
+fn sum<A>(xs: List<A>) -> A = foldl(add, 0, xs)
|