[Haskell-cafe] Markov Text Generator & Randomness

Ben Gamari ben at smart-cactus.org
Thu Jul 24 13:55:42 UTC 2014


Charles-Pierre Astolfi <cpa at crans.org> writes:

>>     "The old man looked from his glass across the square, then over at the waiters."
> You're embarrassingly right! But then, "those the" definitely never
> appears, altough it does in my generated text.
>
>> Otherwise my cursory look turned up no bugs.
> Unfortunately there is :(
>

Ahh yes, looking a bit more closely now I have a few points:

  1. In `draw`: The first argument is an action which will return a new
     word. Instead of passing `rword :: RVar Word`, you presumably rather want to
     pass `word :: Word`. This is likely the cause of your bug.
     
  2. In `draw`: Instead of `weightedSample` which produces a random shuffling of
     the entire list,  you really just want to draw a single word. This
     is a categorical distribution; use `Data.Random.Categorical.fromList`
     to construct the distribution and `R.rvar` to draw a variate. Note
     that you may only want to avoid doing the former more than once as
     construction of the distribution requires sorting and normalizing.

  3. `map (\(x,y)->(y,x))` is just `map swap` where `swap` is provided
     by `Data.Tuple`.

My quick rework of your code can be found here [1].

Cheers,

- Ben


[1] http://lpaste.net/108025
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 472 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140724/e1b556b6/attachment.sig>


More information about the Haskell-Cafe mailing list