开发者

How to make an empty sequence in common lisp?

A function takes a sequence as the parameter. In the function, I want to make an empty sequence of the same type of the parameter. Then I'll store something and return it such that the return type is the same as the parameter. But

(make-sequence (type-of parameter) 0) will cause error if parameter is any list or vector of some length.

My current solution is: 1. use an empty list to store things, let's call it temp-list 2. (make-sequence (type-of pa开发者_JAVA百科rameter) (length temp-list)) 3. copy the elements, then return

Other better solutions?


returning an empty sequence of the same type

(subseq sequence 0 0)


It looks like it's failing because type-of a list is CONS, not LIST, and type-of a vector has its size, which you're trying to override.

You could convert the type-of by some simple rules, like:

(cond ((eq x 'cons) 'list)
      ((consp x) (car x))))

But PUSH doesn't really work on vectors, so I'm not sure what you'd want to do there, anyway: you'll need a list for the consing, and then to convert, anyway, right?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜