Need help groking the statement: expression1 in term:expression2

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

Need help groking the statement: expression1 in term:expression2

Olumide
Hello List,

I'd appreciate help understanding the second line of following block of
code (from LYH, first line added for completeness),

http://learnyouahaskell.com/input-and-output#randomness
randoms' :: (RandomGen g, Random a) => g -> [a]
randoms' gen = let (value, newGen) = random gen in value:randoms' newGen

The part I'm really struggling with is random gen in value:randoms' newGen

Thanks,

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

Re: Need help groking the statement: expression1 in term:expression2

Quanyang Liu
On Tue, Mar 22 2016 at 18:43:46 +0800, Olumide wrote:
> Hello List,
>
> I'd appreciate help understanding the second line of following block
> of code (from LYH, first line added for completeness),
>
> http://learnyouahaskell.com/input-and-output#randomness
> randoms' :: (RandomGen g, Random a) => g -> [a]
> randoms' gen = let (value, newGen) = random gen in value:randoms' newGen
>

It's equivalent to the following:
     randoms' gen = let (value, newGen) = random gen
                    in value:(randoms' newGen)

> The part I'm really struggling with is random gen in value:randoms' newGen
>
> Thanks,
>
> - Olumide

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

Re: Need help groking the statement: expression1 in term:expression2

Sumit Sahrawat, Maths & Computing,
 IIT (BHU)
In reply to this post by Olumide


On 22-Mar-2016 4:14 pm, "Olumide" <[hidden email]> wrote:
> randoms' :: (RandomGen g, Random a) => g -> [a]
> randoms' gen = let (value, newGen) = random gen in value:randoms' newGen

"random gen" returns a pair, whose first element is a random value, and the second element is a new generator.

The cons (:) operator takes two values, one is an element, and the other is a list. It returns a new list with the provided arguments as head and tail.

Ultimately, randoms' gen returns a list whose first element is a random value, and the rest of the list is the result of calling randoms' on the newly produced generator. Recursively, it generates an infinite lazy list of random elements.

Hope this helps :)

Regards,
  Sumit


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