LYAH example

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

LYAH example

sasa bogicevic
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)

I know that f applied to x should produce y and we append log with newLog but when reading  ... f x in (y, ...  I just don't see how f x becomes y in the let statement.
Seems more readable if we could write ... = (f x, log ++ newLog)

Thanks, Sasa

{
        name: Bogicevic Sasa
        phone: +381606006200
}



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: LYAH example

Francesco Ariis
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?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: LYAH example

sasa bogicevic
Ahhhh I see, thank you very much for the response!

Have a nice day,
Sasa

{
        name: Bogicevic Sasa
        phone: +381606006200
}



> On Mar 22, 2017, at 13:30, Francesco Ariis <[hidden email]> wrote:
>
> 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?
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Loading...