Tuesday, 2 February 2010

common lisp - generators

exploring the very basics of generators in common lisp.
here is a simple counter . procedure make-counter when given number , produces next number above by one as result. each call to f , or g , produces the next value. values of n for f are seperate to those of g .
so can make as many counters as want.


;;attempt at generators in lisp
(defpackage :test
(:use #:cl))

(in-package :test)

(defun make-counter(n)
(lambda ()
(setq n (+ n 1))))

;; tests
(setq f (make-counter 0))

;; this generates next value
(funcall f)

(setq g (make-counter 321))

(funcall g)

No comments:

Post a Comment