Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble explaining Haskell code with where and pattern match

I have hard time parsing how mf m y are assigned values or even why there can be 3 variables on the left side of assignment in where section.

Q: Can anyone explain what happens here in both cases? (that is for empty list and a list with some elements)

-- | A variant of 'foldl' that has no base case,
-- and thus may only be applied to non-empty structures.
--
-- @'foldl1' f = 'List.foldl1' f . 'toList'@
foldl1 :: (a -> a -> a) -> t a -> a
foldl1 f xs = fromMaybe (errorWithoutStackTrace "foldl1: empty structure")
                (foldl mf Nothing xs)
  where
    mf m y = Just (case m of
                     Nothing -> y
                     Just x  -> f x y)

(this is the source code for the foldl1 function).

like image 250
przemo_li Avatar asked Dec 22 '25 14:12

przemo_li


2 Answers

Definitions in where clauses follow the same syntax as global definitions, so mf m y = ... defines a function named mf, which takes parameters named m and y.

like image 179
sepp2k Avatar answered Dec 24 '25 11:12

sepp2k


I have hard time parsing how mf m y are assigned values or even why there can be 3 variables.

You do not define three variables here: you define a variable mf which is a function, and m and y are two arguments of the function mf.

We can make the function more elegant, and thus omit the m and y. mf can be defined as:

mf Nothing = Just . id
mf (Just x) = Just . f x

Mind that we can not simply make mf an outer function, since it uses a function f, with is a parameter of foldl1. So we put it in a where clause:

foldl1 :: (a -> a -> a) -> t a -> a
foldl1 f xs = fromMaybe (errorWithoutStackTrace "foldl1: empty structure")
                (foldl mf Nothing xs)
    where mf Nothing = Just . id
          mf (Just x) = Just . f x
like image 29
Willem Van Onsem Avatar answered Dec 24 '25 10:12

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!