summaryrefslogtreecommitdiff
path: root/list.squig
diff options
context:
space:
mode:
Diffstat (limited to 'list.squig')
-rw-r--r--list.squig62
1 files changed, 28 insertions, 34 deletions
diff --git a/list.squig b/list.squig
index ea0d333..b08dafd 100644
--- a/list.squig
+++ b/list.squig
@@ -1,11 +1,13 @@
-new_list := ??{
- front := nil
- back := nil
+list_new := ??{
+ x := {
+ front := nil
+ back := nil
+ }
}
list_push_front := ??(list, item){
list {
- if front == nil {
+ if front == nil then {
front = {
value := item
next := nil
@@ -24,7 +26,7 @@ list_push_front := ??(list, item){
list_push_back := ??(list, item){
list {
- if back == nil {
+ if back == nil then {
front = {
value := item
next := nil
@@ -44,67 +46,59 @@ list_push_back := ??(list, item){
list_pop_front := ??(list){
x := nil
list {
- if front == nil {
+ if front == nil then {
throw_error("Call to 'list_pop_front' on empty list")
- }
+ } else {}
front {
x = value
front = next
}
- if front == nil {
+ if front == nil then {
back = nil
- }
+ } else {}
}
}
list_pop_back := ??(list){
x := nil
list {
- if back == nil {
+ if back == nil then {
throw_error("Call to 'list_pop_back' on empty list")
- }
+ } else {}
back {
x = value
back = prev
}
- if back == nil {
+ if back == nil then {
front = nil
- }
+ } else {}
}
}
list_get := nil
{
get_helper := ??(front, idx){
- if front == nil {
+ if front == nil then {
throw_error("Index past end of list in 'list_get'")
- }
+ } else {}
x := nil
- if idx == 0 {
+ if idx == 0 then {
front {
x = value
}
} else {
front {
- found := nil
- get_helper(next, idx - 1){
- found = x
- }
- x = found
+ x = get_helper(next, idx - 1)
}
}
}
list_get = ??(list, idx){
- if idx < 0 {
+ if idx < 0 then {
throw_error("Negative index in 'list_get'")
- }
+ } else {}
x := nil
list {
- found := nil
- get_helper(front, idx){
- found = x
- }
- x = found
+ x = get_helper(front, idx)
}
}
}
@@ -112,23 +106,23 @@ list_get := nil
list_set := nil
{
set_helper := ??(front, idx, val){
- if front == nil {
+ if front == nil then {
throw_error("Index past end of list in 'list_set'")
- }
- if idx == 0 {
+ } else {}
+ if idx == 0 then {
front {
value = val
}
} else {
front {
- set_helper(next, idx - 1, val){}
+ set_helper(next, idx - 1, val)
}
}
}
list_set = ??(list, idx, val){
- if idx < 0 {
+ if idx < 0 then {
throw_error("Negative index in 'list_set'")
- }
+ } else {}
list {
set_helper(front, idx, val)
}