diff --git a/stdlib/Makefile b/stdlib/Makefile index 546f2af54..5d4766162 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -18,6 +18,8 @@ $(BDIR)/Fun.o: $(BDIR)/Ref.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)/Array.o: $(BDIR)/List.o diff --git a/stdlib/Queue.lama b/stdlib/Queue.lama new file mode 100644 index 000000000..f02d1fa7b --- /dev/null +++ b/stdlib/Queue.lama @@ -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 +}