how to undertand the function join (*)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

how to undertand the function join (*)

Ray-2

Hello Marcus,

what does join (*) do?

 

(*) :: Num a => a -> a -> a

join (*) :: Num a => a -> a


when we feed a number to join (*),for instance;

 λ> : join (*) 3

 9

it seems thata join (*) become a square function.

what does join do to  (*) to make that happen?


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

Re: how to undertand the function join (*)

Frerich Raabe
On 2017-11-30 14:18, Ray wrote:

> what does JOIN (*) do?
>
> (*) :: Num a => a -> a -> a
>
> join (*) :: Num a => a -> a
>
> when we feed a number to join (*),for instance;
>
>  λ> : join (*) 3
>
>  9
>
> it seems thata JOIN (*) become a square function.
>
> what does JOIN do to  (*) to make that happen?

You can figure it out by using equational reasoning and evaluating the
expression manually:

   join (*) 3

Function application is left-associative, so this is equivalent to

   (join (*)) 3

The 'join' function is defined as
(http://hackage.haskell.org/package/base-4.10.0.0/docs/src/GHC.Base.html#join)

   join x = x >>= id

So we can replace 'join (*)' in our above expression with

   ((*) >>= id) 3

(>>=) for functions is defined as

   f >>= k = \ r -> k (f r) r

So with that at hand, we get

   (\r -> id ((*) r) r) 3

'id x' is just 'x', so we get

   (\r -> (*) r r) 3

With infix syntax, this can be written as

   (\r -> r * r) 3

If you now apply that function, you get

   3 * 3

Which gives your result '9'.

--
Frerich Raabe - [hidden email]
www.froglogic.com - Multi-Platform GUI Testing
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners