Created
October 13, 2014 08:39
-
-
Save pkulev/6da4a43000a82c8bf022 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% if head is nil then answer | |
% if head is integer then append it to answer and flatten tail | |
% if head is list then flatten head and then flatten tail | |
declare | |
fun {FlattenList L} | |
local FlatLoop in | |
fun {FlatLoop L1 Acc} | |
case L1 | |
of nil then Acc | |
[] (H|T1)|T then | |
{FlatLoop T {Append Acc {FlatLoop H|T1 nil}}} | |
[] H|T then {FlatLoop T {Append Acc [H]}} | |
end | |
end | |
{FlatLoop L nil} | |
end | |
end | |
{Browse {FlattenList [1 2 3]}} | |
{Browse {FlattenList [[1] [2]]}} | |
{Browse {FlattenList [1 2 [3] 4]}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment