1
votes
wrap [1,2,3] should output [[1],[2],[3]]
wrap [[1],[2],[3]] should output [ [[1]], [[2]], [[3]] ]

my implementation is:

wrap [] = []
wrap (x:xs) = [x] :  [wrap xs]

and haskell output an error: Occurs check: cannot construct the infinite type: t ~ [t]

Expected type: [t] -> [t] Actual type: [t] -> [[t]]

1
wrap (x:xs) = [x] : wrap xs?zerkms
^, but also just wrap = map pure.Ry-
@zerkms, that causing Couldn't match type ‘a’ with ‘[a]’ erroranru
@anru it totally works for me ¯\_(ツ)_/¯ zerkms
@zerkms, my bad, I had typed wrap :: [a] -> [a]anru

1 Answers

4
votes

[wrap xs] wraps the entire result of wrap xs. You don't want to wrap the entire result. You only want to wrap each individual element of the remainder of the list and that is exactly what wrap xs already does. Thus, wrap is:

wrap :: [a] -> [[a]]
wrap [] = []
wrap (x:xs) = [x] : wrap xs

The error is telling you that you are trying to use a [t] where GHC expects a t. This is exactly what we said above, that you are wrapping something too many times.