[Haskell-beginners] LYAH example

Francesco Ariis fa-ml at ariis.it
Wed Mar 22 12:30:33 UTC 2017


On Wed, Mar 22, 2017 at 12:44:23PM +0100, sasa bogicevic wrote:
> Hi All,
> Can someone clarify the example I got from LYAH book. This let statement
> is kinda confusing to me : 
> 
> applyLog :: (a, String) -> (a -> (b, String)) -> (b, String)
> applyLog (x, log) f = let (y, newLog) = f x in (y, log ++ newLog) 

Hello Sasa,
    let's rewrite `applyLog`:

    applyLog :: (a, String) -> (a -> (b, String)) -> (b, String)
    applyLog (x, log) f =
                              -- f      :: a -> (b, String)
        let (y, newLog) = f x -- y      :: b
                              -- newLog :: String
        in (y, log ++ newLog) -- (b, String)

f applied to x doesn't produce just `y`, but `y` and `newLog` (in
a Tuple). It is perfectly ok to specify a pattern:

    let (y, newLog) = f x -- legal

    let xyz = f x -- legal too. The first form saves you a `fst`/`snd`

Is it clearer now?


More information about the Beginners mailing list