git @ Cat's Eye Technologies Castile / master eg / linkedlist-ops.castile
master

Tree @master (Download .tar.gz)

linkedlist-ops.castile @masterraw · history · blame

struct list {
  value: string;
  next: list|void;
}

fun empty() {
  return null as list|void
}

fun cons(v: string, l: list|void) {
  make list(value:v, next:l) as list|void
}

equal_list : list|void, list|void -> boolean
fun equal_list(a: list|void, b: list|void) {
  typecase a is void {
    typecase b is void {
      return true
    }
  }
  typecase a is list {
    typecase b is list {
      return a.value == b.value and equal_list(a.next, b.next)
    }
  }
  return false
}

length : list|void -> integer
fun length(l: list|void) {
  typecase l is void { return 0 }
  typecase l is list { return 1 + length(l.next) }
}

main = fun() {
  l1 = cons("first", cons("second", cons("third", empty())));
  l2 = cons("first", cons("second", cons("third", empty())));
  l3 = cons("first", cons("second", empty()));

  print(str(length(l1 as list|void)));

  if (equal_list(l1, l2) and not equal_list(l2, l3)) {
    print("Yep, story checks out")
  }
}