Skip to content

Commit

Permalink
Association list example, which is not working for some reason.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Feb 16, 2022
1 parent 70e0e73 commit 4172d72
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions eg/assoc.castile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
struct assoc {
key: string;
value: string;
next: assoc|void;
} for (singleton, update, lookup, remove)

fun singleton(k: string, v: string) {
make assoc(key:k, value:v, next:null as assoc|void)
}

fun update(k: string, v: string, a: assoc) {
make assoc(key:k, value:v, next:a as assoc|void)
}

lookup : assoc, string -> string|void
fun lookup(a: assoc, k: string) {
if a.key == k {
return a.value as string|void
}
n = a.next
typecase n is void {
return null as string|void
}
typecase n is assoc {
return lookup(n, k) as string|void
}
}

fun main() {
a = update("1", "first", update("2", "second", singleton("3", "third")));
r = lookup(a, "2");
print("um");
typecase r is void { print("NOT FOUND"); }
typecase r is string { print(r); }
print("ya");
}

0 comments on commit 4172d72

Please sign in to comment.