Advertisement
banovski

Ninety-Nine Haskell Problems: #7

Feb 22nd, 2025
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.39 KB | Source Code | 0 0
  1. -- Problem 7: flatten a nested list
  2.  
  3. import Data.List (subsequences)
  4.  
  5. -- Haskell lists won't do because they're homogeneous, hence the new
  6. -- type:
  7.  
  8. data NestedList a = Elem a
  9.   | List [NestedList a]
  10.   deriving Show
  11.  
  12. variants :: [NestedList Integer]
  13. variants = Elem 1 : map List (subsequences
  14.   [ Elem 1
  15.   , List []
  16.   , List [Elem 2]
  17.   , List [List []]
  18.   , List [List [Elem 3]]
  19.   ])
  20.  
  21. flatten :: NestedList a -> [a]
  22. flatten (List []) = []
  23. flatten (Elem x) = [x]
  24. flatten (List (x:xs)) = flatten x ++ go xs
  25.   where
  26.     go [] = []
  27.     go [y] = flatten y
  28.     go (y:ys) = flatten y ++ go ys
  29.  
  30. formatter :: Show a => NestedList a -> [Char]
  31. formatter x = show x ++ " -> " ++ show (flatten x)
  32.  
  33. main :: IO ()
  34. main = mapM_ (putStrLn . formatter) variants
  35.  
  36. -- Elem 1 -> [1]
  37. -- List [] -> []
  38. -- List [Elem 1] -> [1]
  39. -- List [List []] -> []
  40. -- List [Elem 1,List []] -> [1]
  41. -- List [List [Elem 2]] -> [2]
  42. -- List [Elem 1,List [Elem 2]] -> [1,2]
  43. -- List [List [],List [Elem 2]] -> [2]
  44. -- List [Elem 1,List [],List [Elem 2]] -> [1,2]
  45. -- List [List [List []]] -> []
  46. -- List [Elem 1,List [List []]] -> [1]
  47. -- List [List [],List [List []]] -> []
  48. -- List [Elem 1,List [],List [List []]] -> [1]
  49. -- List [List [Elem 2],List [List []]] -> [2]
  50. -- List [Elem 1,List [Elem 2],List [List []]] -> [1,2]
  51. -- List [List [],List [Elem 2],List [List []]] -> [2]
  52. -- List [Elem 1,List [],List [Elem 2],List [List []]] -> [1,2]
  53. -- List [List [List [Elem 3]]] -> [3]
  54. -- List [Elem 1,List [List [Elem 3]]] -> [1,3]
  55. -- List [List [],List [List [Elem 3]]] -> [3]
  56. -- List [Elem 1,List [],List [List [Elem 3]]] -> [1,3]
  57. -- List [List [Elem 2],List [List [Elem 3]]] -> [2,3]
  58. -- List [Elem 1,List [Elem 2],List [List [Elem 3]]] -> [1,2,3]
  59. -- List [List [],List [Elem 2],List [List [Elem 3]]] -> [2,3]
  60. -- List [Elem 1,List [],List [Elem 2],List [List [Elem 3]]] -> [1,2,3]
  61. -- List [List [List []],List [List [Elem 3]]] -> [3]
  62. -- List [Elem 1,List [List []],List [List [Elem 3]]] -> [1,3]
  63. -- List [List [],List [List []],List [List [Elem 3]]] -> [3]
  64. -- List [Elem 1,List [],List [List []],List [List [Elem 3]]] -> [1,3]
  65. -- List [List [Elem 2],List [List []],List [List [Elem 3]]] -> [2,3]
  66. -- List [Elem 1,List [Elem 2],List [List []],List [List [Elem 3]]] -> [1,2,3]
  67. -- List [List [],List [Elem 2],List [List []],List [List [Elem 3]]] -> [2,3]
  68. -- List [Elem 1,List [],List [Elem 2],List [List []],List [List [Elem 3]]] -> [1,2,3]
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement