[Openmcl-devel] deftype list-of (recursive definition)
Stas Boukarev
stassats at gmail.com
Thu Jul 26 12:40:14 PDT 2012
Taoufik Dachraoui <dachraoui.taoufik at gmail.com> writes:
> Hi
>
> Thank you for the response
>
> 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
> ?
>
> Kind regards
> Taoufik
>
> On Thu, Jul 26, 2012 at 7:56 PM, Pascal J. Bourguignon <
> pjb at informatimago.com> wrote:
>
>> Taoufik Dachraoui <dachraoui.taoufik at gmail.com> writes:
>>
>> > Hi
>> >
>> > I would like to define a recursive type as follows:
>> >
>> > (deftype list-of (type)
>> > `(or null (cons ,type (list-of ,type))))
>> >
>> > This does not work (stack overflow)
>> >
>> > How can define such types?
>>
>>
>> cl-user> (deftype list-of (type)
>> (let ((funame (gensym (prin1-to-string `(list-of ,type)))))
>> (compile funame `(lambda (x)
>> (declare (ftype (function (t) t) ,funame))
>> (or (null x)
>> (and (consp x)
>> (typep (car x) ',type)
>> (,funame (cdr x))))))
>> `(satisfies ,funame)))
>>
>> list-of
>> cl-user> (typep '("one" "two" "three") '(list-of string))
>> t
>> cl-user> (typep '("one" 3 "three") '(list-of string))
>> nil
>> cl-user>
Please, don't use this. It won't help the implementation to infer any
types, and will only slow things down. If you want to check types of
elements in a list, define a function list-of-type-p and use it directly
or with ASSERT.
--
With best regards, Stas.
More information about the Openmcl-devel
mailing list