(defstruct patch color xcor ycor)
 
 (defun agent-set-list (agent-set)
- agent-set)
+ (cond
+  ((eql agent-set :turtles) *turtles*)
+  ((eql agent-set :patches) *patches*)
+  ((and (listp agent-set) (eql :agent-set (car agent-set))) (cdr agent-set))
+  (t (error "Doesn't seem to be an agent-set: ~A" agent-set))))
 
   each time it's used, so changes depending on the state of the engine.
 
   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patches"
- *patches*)
+ :patches)
 
 (defun turtles ()
  "TURTLES => ALL-TURTLES
   each time it's used, so changes depending on the state of the engine.
 
   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles"
- *turtles*)
+ :turtles)
 
 (defun ask (agent-set fn)
  "ASK AGENT-SET FN => RESULT
    :while agent
    :do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
 
+(defun count (agent-set)
+ "COUNT AGENT-SET => N
+
+ARGUMENTS AND VALUES:
+
+  AGENT-SET: a NetLogo agentset
+  N: a number
+
+DESCRIPTION:
+
+  COUNT is equivalent to count in NetLogo.  Returns N, the number of
+  agents in AGENT-SET.
+
+  See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#count"
+ (coerce (length (agent-set-list agent-set)) 'double-float))
+
 (defun of (fn agent-set)
  "OF FN AGENT-SET => RESULT
 
 
 
 (defpackage #:clnl-nvm
  (:use :common-lisp)
- (:shadow #:random)
+ (:shadow #:random #:count)
  (:export #:export-world #:create-world #:current-state
   ; API as used by transpiled NetLogo programs
   #:agent-value
   #:ask
+  #:count
   #:create-turtles
   #:die
   #:of
 
 (defprim :clear-all ())
 (defprim :crt (:number))
 (defprim :color ())
-(defprim :count ())
+(defprim :count (:agentset))
 (defprim :die ())
 (defprim :display ())
 (defprim :with (:reporter-block))
 
 (defsimpleprim :+ :reporter cl:+)
 (defsimpleprim :* :reporter cl:*)
 (defsimpleprim :/ :reporter cl:/)
-(defprim :any? :reporter (lambda (agentset) `(> (length ,agentset) 0)))
+(defprim :any? :reporter (lambda (agentset) `(> (clnl-nvm:count ,agentset) 0)))
 (defsimpleprim :ask :command clnl-nvm:ask)
 (defagentvalueprim :color)
+(defsimpleprim :count :reporter clnl-nvm:count)
 (defsimpleprim :crt :command clnl-nvm:create-turtles)
 (defsimpleprim :die :command clnl-nvm:die)
 (defsimpleprim :fd :command clnl-nvm:forward)
 
 
 (defsimplecommandtest "setxy 1" "crt 10 ask turtles [ setxy random-xcor random-ycor ]"
  "B02FD5B864A129AED5254A68C499607F7F6EA236")
+
+(defsimplereportertest "count 1" "count turtles" "0"
+ "E1DE30F072D785E0D0B59F28B0F7853E3D3E0D8B")
+
+(defreportertestwithsetup "count 2" "crt 10" "count turtles" "10"
+ "A925E39EC022967568D238D31F70F0A375024A89")
+
+(defsimplereportertest "count 3" "count patches" "9"
+ "E1DE30F072D785E0D0B59F28B0F7853E3D3E0D8B")