#Skip to menu

Guile(scheme): Queue

Published:

Guile has a beautiful queue implementation. The queue is a cons of a list and the last pair of the list. Get the code on Savannah (module/ice-9/q.scm) (local cache). Queue documentation.

As an example of how to use this data structure, we’ll implement the moving average algorithm. (mavg.scm)

(use-modules (ice-9 q) (srfi srfi-1))

(define (avg n xs)
  (/ (fold + 0 xs) n))

(define (ma subn xs)
  (define q (make-q))
  (set-car! q (take xs subn))
  (sync-q! q)
  (let l ((xs (drop xs subn)) (avgs (make-q)))
    (enq! avgs (avg subn (car q)))
    (if (null? xs)
        (car avgs)
        (begin
          (q-pop! q)
          (enq! q (car xs))
          (l (drop xs 1) avgs)))))