Bill Allombert on Fri, 02 Feb 2024 10:45:30 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: How to perform concat()s without errors on empty vectors or conversions on single-element vectors??


On Fri, Feb 02, 2024 at 04:34:29PM +1100, Joe Slater wrote:
> I want to convert a vector whose members may be vectors into one long
> vector. This can be done using concat(), but I have two problems:
> 1) it fails when the vector is empty, rather than returning the empty
> vector; and

The issue is that there is not enough information to know that is the neutral
element of your monoid, it could be [], []~, "" etc.

> 2) when the vector has a single element it returns that element rather than
> a vector.

Same issue.

The fix is to put the neutral element as first element of the vector.

?  concat([[],1,2])
%1 = [1,2]
?  concat([[]~,1,2])
%2 = [1,2]~
?  concat(["",1,2])
%3 = "12"

> I.e., I input:
> (16:29) > v=[[1,2],[1,[2,3]],[[1,2],[3,4]],[1],[]];

You should avoid inhomogenous lists like [1,[2,3]],
it is usually safer if all the elements have the same type: [[1],[2,3]]

Cheers,
Bill