Bend/examples/queue.bend

23 lines
460 B
Plaintext
Raw Normal View History

# A cool trick involving unscoped lambdas
# Implements constant-time queues with just lambdas
2024-04-11 20:17:46 +03:00
2024-05-15 22:26:16 +03:00
# Qnew : Queue a
2024-04-11 20:17:46 +03:00
Qnew = λx x
2024-05-15 22:26:16 +03:00
# Qadd : a -> Queue a -> Queue a
2024-04-11 20:17:46 +03:00
Qadd = λx λq λk (q λc (c x k))
2024-05-15 22:26:16 +03:00
# Qrem : Queue a -> Pair a (Queue a)
2024-04-11 20:17:46 +03:00
Qrem = λq (q $k λx λxs λp(p x λ$k xs))
2024-05-15 22:26:16 +03:00
# Output: [1, 2, 3]
2024-04-11 20:17:46 +03:00
main =
let q = Qnew
let q = (Qadd 1 q)
let q = (Qadd 2 q)
let q = (Qadd 3 q)
((Qrem q) λv0 λq
((Qrem q) λv1 λq
((Qrem q) λv2 λq
[1, 2, 3])))