[Haskell-beginners] how to undertand the function join (*)

Frerich Raabe raabe at froglogic.com
Thu Nov 30 13:47:37 UTC 2017


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 - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing


More information about the Beginners mailing list