Added queue to stdlib

This commit is contained in:
Dmitry Boulytchev 2025-01-31 00:02:40 +03:00
parent 4cb462c2a8
commit ddb45a496f
2 changed files with 28 additions and 0 deletions

View file

@ -18,6 +18,8 @@ $(BDIR)/Fun.o: $(BDIR)/Ref.o
$(BDIR)/Data.o: $(BDIR)/Ref.o $(BDIR)/Collection.o $(BDIR)/Data.o: $(BDIR)/Ref.o $(BDIR)/Collection.o
$(BDIR)/Queue.o: $(BDIR)/List.o
$(BDIR)/Collection.o: $(BDIR)/List.o $(BDIR)/Ref.o $(BDIR)/Collection.o: $(BDIR)/List.o $(BDIR)/Ref.o
$(BDIR)/Array.o: $(BDIR)/List.o $(BDIR)/Array.o: $(BDIR)/List.o

26
stdlib/Queue.lama Normal file
View file

@ -0,0 +1,26 @@
import List;
fun emptyQueue () {
[{}, {}]
}
fun isEmptyQueue (q) {
case q of
[{}, {}] -> true
| _ -> false
esac
}
fun enqueue ([pop, push], x) {
[pop, x : push]
}
fun dequeue ([pop, push]) {
case pop of
x : pop -> [[pop, push], x]
| _ -> case push of
{} -> failure ("dequeueing from empty queue\n")
| _ -> dequeue ([reverse (push), {}])
esac
esac
}