Couldn't skip type annotations as that raises ambiguous type error.
Explanation:
Function foldl applies a given function to all container elements going from inside out. When applied to list, it effectively traverses list in a reverse order.
Binary operator foldl is applied to flip (:) and [].
foldl has type signature Foldable t => (b -> a -> b) -> b -> t a -> b, which can be rewritten as ([a] -> a -> [a]) -> [a] -> [a] -> [a].
Binary operator (:) appends a value (first argument) to a list (second argument). It's type signature is a -> [a] -> [a]. It almost looks like the required signature from the 3. point.
Use the flip function in order to switch around (:) first and second argument. This results in a function flip (:) with type signature of [a] -> a -> [a].
Now when this flip (:) is partially applied to foldl, it gives a function foldl (flip (:)) with a type signature [a] -> [a] -> [a]. Giving foldl a initial value of [], gives the final form foldl (flip (:)) [] with a type signature [a] -> [a].
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Haskell solution in exactly 28 characters:
Longer version of the same, but more readable:
Couldn't skip type annotations as that raises ambiguous type error.
Explanation:
foldl
applies a given function to all container elements going from inside out. When applied to list, it effectively traverses list in a reverse order.foldl
is applied toflip (:)
and[]
.foldl
has type signatureFoldable t => (b -> a -> b) -> b -> t a -> b
, which can be rewritten as([a] -> a -> [a]) -> [a] -> [a] -> [a]
.(:)
appends a value (first argument) to a list (second argument). It's type signature isa -> [a] -> [a]
. It almost looks like the required signature from the 3. point.flip
function in order to switch around(:)
first and second argument. This results in a functionflip (:)
with type signature of[a] -> a -> [a]
.flip (:)
is partially applied tofoldl
, it gives a functionfoldl (flip (:))
with a type signature[a] -> [a] -> [a]
. Givingfoldl
a initial value of[]
, gives the final formfoldl (flip (:)) []
with a type signature[a] -> [a]
.