Nested matches don't reconstruct the original variable properly
Opened this issue · 0 comments
developedby commented
Reproducing the behavior
I was writing a program with nested matches on the same variable and noticed that the interaction count was a bit high
def reverse(xs):
fold xs with acc=[]:
case List/Cons:
return xs.tail(List/Cons(xs.head, acc))
case List/Nil:
return acc
def split(list, len):
switch len:
case 0:
return ([], list)
case _:
match list:
case List/Cons:
(left, right) = split(list.tail, len-1)
return (List/Cons(list.head, left), right)
case List/Nil:
return ([], [])
def range(start, end):
switch start < end:
case 0:
return []
case _:
return List/Cons(start, range(start + 1, end))
def merge_sort(list, len):
match list:
case List/Cons:
match list.tail:
case List/Cons:
new_len = len / 2
(left, right) = split(list, new_len)
return merge(merge_sort(left, new_len), merge_sort(right, len - new_len))
case List/Nil:
return [list.head]
case List/Nil:
return []
def merge(left, right):
match left:
case List/Cons:
match right:
case List/Cons:
if (left.head <= right.head):
return List/Cons(left.head, merge(left.tail, (right)))
else:
return List/Cons(right.head, merge(left, right.tail))
case List/Nil:
return left
case List/Nil:
return right
def main():
len = 100
list = reverse(range(1, len))
sorted = merge_sort(list, len)
return *
In merge_sort
, changing (left, right) = split(list, new_len)
to (left, right) = split(List/Cons(list.head, list.tail), new_len)
reduces the number of interactions significantly, so it seems to not be reconstructing the list
variable correctly across nested matches.
System Settings
- Bend: commit a0aa467
Additional context
No response