[Openmcl-devel] deftype list-of (recursive definition)

Joshua TAYLOR joshuaaaron at gmail.com
Thu Jul 26 12:24:30 PDT 2012


On Thu, Jul 26, 2012 at 3:08 PM, Taoufik Dachraoui
<dachraoui.taoufik at gmail.com> wrote:
> I did not like the compile so I came up with the following:
>
> ? (let ((xtype nil))
> (defun mytype (e)
>   (or (null e)
>       (and (consp e)
>            (typep (car e) `,xtype)
>            (mytype (cdr e)))))
> (deftype list-of (type)
>   (setf xtype type)
>   '(satisfies mytype)))
> LIST-OF
> ? (typep '(2 3) '(list-of integer))
> T
> ? (typep '(x y) '(list-of symbol))
> T
> ? (typep '(x 3) '(list-of symbol))
> NIL
> ?

The problem is, though, that that doesn't work:

CL-USER> (let ((xtype nil))
(defun mytype (e)
  (or (null e)	
      (and (consp e)
           (typep (car e) `,xtype)
           (mytype (cdr	e)))))
(deftype list-of (type)
  (setf xtype type)
  '(satisfies mytype)))
LIST-OF
CL-USER> (defun int-test ()
					 (typep '(2 3) '(list-of integer)))
INT-TEST
CL-USER> (int-test)
T
CL-USER> (defun symbol-test ()
					 (typep '(x y) '(list-of symbol)))
SYMBOL-TEST
CL-USER> (symbol-test)
T
CL-USER> (int-test)
NIL ; no longer T !

Depending on when the value of xtype is changed, type checks don't
always have check what you're intending them to check.

//JT

-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/



More information about the Openmcl-devel mailing list