原文
trait Functor[A]:
fun map[B](self, f: A -> B) -> Self[B];
impl List[A]:
fun fold[A, B](l: List[A], z: B, f: (B, A) -> B) -> B
match l:
Cons(h, t) => List::fold(t, f(z, h), f)
Nil => z
fun sum(l: List[i32]) -> i32
List::fold(l, 0, (a, b) => a + b)
impl Functor[A] for List[A]:
fun map[B](self, f: A -> B) -> List[B]
List::fold(self, Nil[B], (t, h) => Cons(f(h), t))
fun fmap[A, B, F: Functor](f: A -> B, x: F[A]) -> F[B]
x.map(f)
fun main() -> IO[Unit]
let l = Cons(1, Cons(2, Cons(3, Nil)))
let l2 = fmap(x => x * 2, l)
print(int_to_str(List::sum(l2)))