Skip to content

Instantly share code, notes, and snippets.

@pkulev
Created October 13, 2014 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkulev/6da4a43000a82c8bf022 to your computer and use it in GitHub Desktop.
Save pkulev/6da4a43000a82c8bf022 to your computer and use it in GitHub Desktop.
% 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