lys-lang / lys

⚜︎ A language that compiles to WebAssembly
https://lys-lang.dev
Apache License 2.0
420 stars 8 forks source link

Function references #61

Closed menduz closed 4 years ago

menduz commented 4 years ago

Function as argument/values are now accepted.


enum Tree {
  Nil
  Node(name: string)
  Cons(left: Tree, right: Tree)
}

// this is now legal
fun walk(
  enter: fun(Tree) -> Tree,
  leave: fun(Tree) -> Tree,
  term: Tree
): Tree = {
  val e = enter(term)
  leave(match e {
    case cons is Cons(left, right) -> {
      val newLeft = walk(enter, leave, left)
      val newRight = walk(enter, leave, right)
      if (newLeft != left || newRight != right) {
        Cons(newLeft, newRight)
      } else {
        cons
      }
    }
    else -> e
  })
}

fun identity(x: Tree): Tree = x