2 ms·
Personally, I think this problem shows why sane type systems are useful. In Haskell data NestedList a = Single a | Nested [NestedList a] -- Or equival
by wz1000 10y ago
Personally, I think this problem shows why sane type systems are useful.
In Haskell
data NestedList a = Single a | Nested [NestedList a]
-- Or equivalently, type NestedList = Free []
flatten :: NestedList a -> [a]
-- This is pretty much the only reasonable definition of flatten the compiler will accept given this type
flatten (Single a) = [a]
flatten (Nested xs) = concatMap flatten xs
-- or given the Free variation of NestedList
-- flatten = retract
- dllthomas 10y agoNot necessarily. See my bash solution in another comment :-D