Flatten a list: Difference between revisions

Content added Content deleted
(Add Refal)
m (Move FutureBasic out of BASIC group)
Line 554: Line 554:
Sleep</syntaxhighlight>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre>

==={{header|FutureBasic}}===
Definitely old school.
<syntaxhighlight lang="futurebasic">
local fn FlattenList( list as Str255 ) as Str255
long i
Str255 flatStr, commaStr
flatStr = ""
for i = 1 to len$(list)
if ( instr$( 0, "[] ,", mid$( list, i, 1 ) ) === 0 )
flatStr += commaStr + mid$( list, i, 1 )
commaStr = ", "
end if
next
end fn = flatStr

window 1, @"Flatten a list", ( 0, 0, 350, 150 )

print "["; fn FlattenList( "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" ); "]"

HandleEvents</syntaxhighlight>
{{output}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre>

Modern and a little outside the box.
<syntaxhighlight lang="futurebasic">
void local fn FlattenAList
CFStringRef listStr = @"[[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]"
CFArrayRef listArr = fn StringComponentsSeparatedByCharactersInSet( listStr, fn CharacterSetWithCharactersInString( @"\"[ ]," ) )
CFMutableArrayRef mutArr = fn MutableArrayWithArray( listArr )
MutableArrayRemoveObject( mutArr, @"" )
CFStringRef flatStr = fn ArrayComponentsJoinedByString( mutArr, @", " )
printf @"[%@]", flatStr
end fn

fn FlattenAList

HandleEvents
</syntaxhighlight>
{{output}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre>
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre>


Line 1,908: Line 1,866:
println[flatten[a]]
println[flatten[a]]
</syntaxhighlight>
</syntaxhighlight>

=={{header|FutureBasic}}==
Definitely old school.
<syntaxhighlight lang="futurebasic">
local fn FlattenList( list as Str255 ) as Str255
long i
Str255 flatStr, commaStr
flatStr = ""
for i = 1 to len$(list)
if ( instr$( 0, "[] ,", mid$( list, i, 1 ) ) === 0 )
flatStr += commaStr + mid$( list, i, 1 )
commaStr = ", "
end if
next
end fn = flatStr

window 1, @"Flatten a list", ( 0, 0, 350, 150 )

print "["; fn FlattenList( "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" ); "]"

HandleEvents</syntaxhighlight>
{{output}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre>

Modern and a little outside the box.
<syntaxhighlight lang="futurebasic">
void local fn FlattenAList
CFStringRef listStr = @"[[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]"
CFArrayRef listArr = fn StringComponentsSeparatedByCharactersInSet( listStr, fn CharacterSetWithCharactersInString( @"\"[ ]," ) )
CFMutableArrayRef mutArr = fn MutableArrayWithArray( listArr )
MutableArrayRemoveObject( mutArr, @"" )
CFStringRef flatStr = fn ArrayComponentsJoinedByString( mutArr, @", " )
printf @"[%@]", flatStr
end fn

fn FlattenAList

HandleEvents
</syntaxhighlight>
{{output}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre>


=={{header|GAP}}==
=={{header|GAP}}==