<div dir="ltr">Hi,<br><br>The following piece of code runs just fine, if I say:<br><br><span style="font-family: courier new,monospace;">instance Random RGB where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> random = color</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> randomR = colorR</span><br style="font-family: courier new,monospace;">
<br>instead of:<br><br><span style="font-family: courier new,monospace;">instance Random RGB where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> random = color2</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> randomR = colorR<br><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">When I use </span>random = color2</span><span style="font-family: courier new,monospace;"><span style="font-family: arial,helvetica,sans-serif;"> I encounter a stack space overflow:<br>
<br><span style="font-family: courier new,monospace;">cetin@linux-d312:~/lab/test/colors> ./var2 +RTS -K30000000</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Stack space overflow: current size 30000000 bytes.</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Use `+RTS -Ksize' to increase it.</span><br style="font-family: arial,helvetica,sans-serif;"></span></span>
<br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">I think I</span>'m doing something wrong with the definition of colorR.<br><br>Can anyone explain me what's wrong?<br>
<br><span style="font-family: courier new,monospace;">import GHC.Word</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import Data.Word</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import System.Random</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import System.Random.Mersenne.Pure64</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">type RGB = (Int,Int,Int)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">instance Bounded RGB where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> minBound = minRGB</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> maxBound = maxRGB</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">minRGB = (0 ,0 ,0 )</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">maxRGB = (255,255,255)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">instance Random RGB where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> random = color2</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> randomR = colorR</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">color2 :: RandomGen g $B"M(B g $B"*(B (RGB,g)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">color2 = colorR (minRGB,maxRGB)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">color :: RandomGen g $B"M(B g $B"*(B (RGB,g)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">color s0 = ((r,g,b),s3)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (r,s1) = q s0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (g,s2) = q s1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (b,s3) = q s2</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> q = randomR (0,255)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace; color: rgb(255, 0, 0);">colorR :: RandomGen g $B"M(B (RGB,RGB) $B"*(B g $B"*(B (RGB,g)</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);">
<span style="font-family: courier new,monospace; color: rgb(255, 0, 0);">colorR ((a,b,c),(x,y,z)) s0 = ((r,g,b),s3)</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);"><span style="font-family: courier new,monospace; color: rgb(255, 0, 0);"> where</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);">
<span style="font-family: courier new,monospace; color: rgb(255, 0, 0);"> (r,s1) = q (a,x) s0</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);"><span style="font-family: courier new,monospace; color: rgb(255, 0, 0);"> (g,s2) = q (b,y) s1</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);">
<span style="font-family: courier new,monospace; color: rgb(255, 0, 0);"> (b,s3) = q (c,z) s2</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);"><span style="font-family: courier new,monospace; color: rgb(255, 0, 0);"> q = randomR</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">main :: IO ()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">main = do</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> mt $B"+(B newPureMT</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> let cs = randoms mt :: [RGB]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print cs</span><br><br>------------------------------------------<br><br>This one also just works fine:<br><br><span style="font-family: courier new,monospace;">import Data.Word</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import System.Random.Mersenne</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">type RGB = (Word8,Word8,Word8)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">instance MTRandom RGB where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> random m = do</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> r $B"+(B random m :: IO Word8</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> g $B"+(B random m :: IO Word8</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> b $B"+(B random m :: IO Word8</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return (r,g,b)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">main :: IO ()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">main = do</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> g $B"+(B newMTGen Nothing</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> cs $B"+(B randoms g :: IO [RGB]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print cs<br><font face="arial,helvetica,sans-serif"><br>but I really need the range constraints colorR might provide me and would greatly appreciate any help to understand/solve the issue.<br>
<br>Best Regards,<br>Cetin<br><br>P/s:<br><span style="font-family: courier new,monospace;">uname -a</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Linux linux-d312 2.6.25.16-0.1-default #1 SMP 2008-08-21 00:34:25 +0200 x86_64 x86_64 x86_64 GNU/Linux</span><br>
</font></span></div>