Verified Commit 74f7f0b3 authored by Camil Staps's avatar Camil Staps 🚀

Avoid stack overflow in ggenString

parent 2968e7e1
Pipeline #28114 passed with stage
in 48 seconds
......@@ -54,7 +54,7 @@ genState :: GenState
* @param A list of random numbers (e.g. aStream)
* @result An inifinite list of strings
*/
ggenString :: Int Real Int Int RandomStream -> [String]
ggenString :: !Int !Real !Int !Int !RandomStream -> [String]
derive ggen Int, Bool, Real, Char, String
derive ggen UNIT, PAIR, EITHER, CONS of gcd, OBJECT of gtd, FIELD of d, RECORD of grd
......
......@@ -194,17 +194,18 @@ where
Nothing -> f s
Just limit -> take limit $ f s
ggenString :: Int Real Int Int RandomStream -> [String]
ggenString :: !Int !Real !Int !Int !RandomStream -> [String]
ggenString maxlen factor minchar maxchar stream = rndStrings stream
where
rndStrings [len:rnd]
# len = toInt ((randIntToReal len) ^ factor * (fromInt maxlen - 0.5))
# (chars,rnd) = seqList (repeatn len genElem) rnd
# (chars,rnd) = genElems [] len rnd
string = {c \\ c<-chars}
= [string:rndStrings rnd]
where
genElem :: RandomStream -> .(Char, RandomStream)
genElem [r:rnd] = (toChar (minchar+((abs r) rem (maxchar+1-minchar))), rnd)
genElems :: ![Char] !Int !RandomStream -> (![Char], !RandomStream)
genElems cs 0 rnd = (cs,rnd)
genElems cs n [r:rnd] = genElems [toChar (minchar+((abs r) rem (maxchar+1-minchar))):cs] (n-1) rnd
randIntToReal :: Int -> Real
randIntToReal x = (toReal x + if (x >= 0) 0.0 4294967296.0) / 4294967295.0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment