| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- use core::scalar
- fn len<A>(xs: List<A>) -> Scalar
- fn head<A>(xs: List<A>) -> A
- fn tail<A>(xs: List<A>) -> List<A>
- fn cons<A>(x: A, xs: List<A>) -> List<A>
- fn is_empty<A>(xs: List<A>) -> Bool = len(xs) == 0
- # We definitely want to make the following functions generic, but this is not yet possible.
- fn generate(n: Scalar, f: Fn[() -> Scalar]) -> List<Scalar> =
- 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
- then []
- else cons(f(head(xs)), map(f, tail(xs)))
- fn cons_end(xs: List<Scalar>, x: Scalar) -> List<Scalar> =
- 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
- then []
- else cons_end(reverse(tail(xs)), head(xs))
- fn sequence(n: Scalar) -> List<Scalar> =
- if n == 1
- then [0]
- 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))
- 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
|