Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

typep confusion

39 views
Skip to first unread message

David Richards

unread,
Jun 29, 2022, 9:36:06 AM6/29/22
to
Hello all,
I am expecting the following to return false (NIL). What am I not understanding? I get the same result in SBCL, CLISP and Allegro-lisp so clearly it's me!
(typep #("Hello" "world") '(vector number))
T

Thanks!
Dave

Alan Bawden

unread,
Jun 29, 2022, 9:59:34 AM6/29/22
to
Because (UPGRADED-ARRAY-ELEMENT-TYPE 'NUMBER) is T, and so (VECTOR NUMBER)
is really just the same as (VECTOR T). If any of those implementations
supported an array type that could _only_ contain numbers (and _not_
strings), then you would get a different answer in that implementation.

- Alan

David Richards

unread,
Jun 29, 2022, 11:02:18 AM6/29/22
to
Thank you Alan for the answer! So if I wanted to make certain that an array holds only strings I'd have to use something like
(every 'stringp #("Hello" "World"))
right?
Thanks!
Dave

Tom Russ

unread,
Jun 29, 2022, 6:55:16 PM6/29/22
to
Yes.

Madhu

unread,
Jun 29, 2022, 9:39:26 PM6/29/22
to
* Tom Russ <50ba9190-a8a2-4036-94c6-b802174e9e60n @googlegroups.com> :
Wrote on Wed, 29 Jun 2022 15:55:09 -0700 (PDT):
> On Wednesday, June 29, 2022 at 8:02:18 AM UTC-7, ??? wrote:
>> On Wednesday, June 29, 2022 at 9:59:34 AM UTC-4, Alan Bawden wrote:
>> > David Richards ??? writes:
>> > I am expecting the following to return false (NIL). What am I not
>> > understanding? I get the same result in SBCL, CLISP and Allegro-lisp so
>> > clearly it's me!
>> > (typep #("Hello" "world") '(vector number))
>> > T
>> > Because (UPGRADED-ARRAY-ELEMENT-TYPE 'NUMBER) is T, and so (VECTOR NUMBER)
>> > is really just the same as (VECTOR T). If any of those implementations
>> > supported an array type that could _only_ contain numbers (and _not_
>> > strings), then you would get a different answer in that implementation.
>> Thank you Alan for the answer! So if I wanted to make certain that
>> an array holds only strings I'd have to use something like
>> (every 'stringp #("Hello" "World"))
>> right?
>> Thanks!
>Yes.

You can even turn that predicate expression into a type with SATISFIES,
but you need to make it a named function first

(defun all-strings-p (seq)
(every 'stringp seq))

(deftype string-vec ()
`(and vector (satisfies all-strings-p)))

(typep #("Hello World") 'string-vec) ;;=> T
0 new messages