[Haskell-cafe] Random/StdGen/read: there is something unclear (or misunderstood!)

Zara me_zara at dea.spamcon.org
Tue Mar 13 04:59:29 EDT 2007


Sorry if this question is too basic, but I am trying to learn Haskell
and from tiem to time I get stuck. (And forgive me, I haev a heavy
procedural background, so functional programming is hard for me)

I am trying to use 'read' to create a random generator, applying it on
some text. As the Report, says:

#   In addition, 'read' may be used to map an arbitrary string (not
#  necessarily one produced by 'show') onto a value of type 'StdGen'.
# In general, the 'read' instance of 'StdGen' has the following
#  properties: 
#   
#   * It guarantees to succeed on any string. 
#   
#   * It guarantees to consume only a finite portion of the string. 
#   
#   * Different argument strings are likely to result in different
#      results.

But it does not succeed on "any string". Code follows

\begin{code}
module Pepe where

import Random

pepin :: StdGen
pepin = read "cosita"

pepe :: StdGen
pepe = read "cositaLinda"

\end{code}

If I evaluate pepin, results are:

Hugs> pepin
38273 1 :: StdGen
(360 reductions, 554 cells)

But if I evaluate pepe, all changes:

Hugs> pepe

Program error: Prelude.read: no parse

(380 reductions, 694 cells)

Am I misunderstanding something, or is StdGen -> read buggy?

As you can see, I am using Hugs, version Sep 2006 to test.

TIA, best regards,

Zara



More information about the Haskell-Cafe mailing list