tags:

views:

58

answers:

2

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 parameter) (length temp-list)) 3. copy the elements, then return

Other better solutions?

+1  A: 

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?

Ken
Oh, yeah, PUSH working with vectores is not like that with lists. Thanks. But there are still other functions to store things in vector. The Rainer's answer satisfy the need.
yehnan
+5  A: 

returning an empty sequence of the same type

(subseq sequence 0 0)
Rainer Joswig