-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Association list example, which is not working for some reason.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |