Jack Brennen on Tue, 08 Jul 2014 08:19:50 +0200


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

Re: concat([])


I don't necessarily disagree, just pointing out the ambiguity in the
usage.  I just looked through my own directory of GP programs (of my
own authorship) and found that out of 107 usages of concat(),
I almost exclusively use the two-argument form -- concat(A,B) --
and that if I expect a string return value, I seem to
explicitly use Str() on at least one of the two arguments rather than
depending on implicit type recognition.

I suppose that's indicative of my own long experience with GP and
how to get predictable results, although I've never really thought
a lot about it (until now).

Note that concat([],[]) returns [] as expected, and that
concat("","") returns "" as expected.


On 7/7/2014 5:14 PM, Max Alekseyev wrote:
Hi Jack,

I believe the general purpose of concat() is to concatenate vectors,
while strings are viewed as a special type of vectors (of characters).
From this perspective, the default type of concat result is 'vector',
unless it is specifically asked to concatenate strings (in which case
result type is 'string').
That's why I think concat([]) should not be much different from

? concat([[]])
%3 = []

Regards,
Max


On Mon, Jul 7, 2014 at 7:23 PM, Jack Brennen <jfb@brennen.net> wrote:
Or should it be "" ?

"ab" <- concat(["a","b"])
"a"  <- concat(["a"])
""   <- concat([])

I guess it's ambiguous, so it is rejected?

Note that concat() has some serious weirdnesses
when you mix strings with other types...
This is version 2.5.5 behavior:

? concat(["",1,x,y])
%1 = "1xy"
? concat([1,x,y,""])
%2 = "[1, x, y]"
? concat([[],x,y,""])
%3 = "[x, y]"
? concat([[],x,y,[]])
%4 = [x, y]
? concat(["",x,y,[]])
%5 = "xy[]"

It seems as if it progresses using non-string-based concatenation
until it finds a string, at which point it converts the result
so far to a string and then continues?





On 7/7/2014 3:34 PM, Max Alekseyev wrote:

? concat([])
    ***   at top-level: concat([])
    ***                 ^----------
    *** concat: domain error in concat: vector = []
    ***   Break loop: type 'break' to go back to GP prompt


Should it simply return [] ?

Regards,
Max