> < ^ Date: Mon, 09 Jan 1995 12:01:00 +0100 (WET)
> < ^ From: Martin Schoenert <martin.schoenert@math.rwth-aachen.de >
< ^ Subject: Re: strange problem with string concatenation
Jacob Hirbawi wrote in his e-mail message of 1995/01/07

I ran into this strange behavior of string concatenation :

f:=x->Concatenation(List(x,String));
a:=UnorderedTuples([1..3],2);
b:=List(a,f);

# I want b to equal ["11","12",...,"33"]; instead I get :
gap> b;
[ [ '1', '1' ], [ '1', '2' ], [ '1', '3' ], [ '2', '2' ], [ '2', '3' ],
  [ '3', '3' ] ]

# but single elements look right!
gap> b[1];
"11"

...

but it would be nicer if the built-in function behaves predictably.

Except for the printed appearance, there is no difference between the
list [ '1', '1' ] and "11". Both are lists containing two characters
each. "11" is just a shorthand for the more conventional representation
[ '1', '1' ]. This in particular implies that the list b you get
*is equal* to [ "11", "12", ..., "33" ] (in the sense of GAP's '='),
even though they look different.

Now because the shorthand is nicer, GAP tries to print it instead of the
conventational list representation, whenever it is resonably easy to do
so. But in your example, GAP doesn't bother check whether each element
of the large list is in fact a string. On the other hand if you print
the elements individualy, then GAP checks them, and notices that they
are strings.

If you want to force GAP to recognize that the elements are strings, use
the following trick.

gap> f := x -> Concatenation(List(x,String));
gap> a := UnorderedTuples( [1..3], 2 );;
gap> b := List( a, f );;
ap> ignore := List( b, IsString );;

In fact you can avoid the conversion from number to string by using
the list [ '1', '2', '3' ] instead of [1..3] in the first place.

gap> b := UnorderedTuples( "123", 2 );;
gap> ignore := List( b, IsString );
gap> b;
[ "11", "12", "13", "22", "23", "33" ]

Martin.

PS. The above rules also imply that the empty list [] and the empty
string "" are equal. This is a bit ackward in the context of 'Print',
where 'Print( [] )' should print '[]', but 'Print( "" )' should print
nothing (because 'Print' drops the "" around strings).

-- .- .-. - .. -.  .-.. --- ...- . ...  .- -. -. .. -.- .-
Martin Sch"onert,   Martin.Schoenert@Math.RWTH-Aachen.DE,   +49 241 804551
Lehrstuhl D f"ur Mathematik, Templergraben 64, RWTH, 52056 Aachen, Germany

> < [top]