Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
haskell挂断(随机、非安全)_Haskell - Fatal编程技术网

haskell挂断(随机、非安全)

haskell挂断(随机、非安全),haskell,Haskell,我在haskell GHCI上写遗传算法 当我输入子项[“+12”,“*34”]GHCI写入”并挂断:( 所有函数都是单独工作的,只有在同时使用函数(交叉、rnd3、gNt工作规范)和随机数时才会出现错误 代码: -----随机输入----------------------------------- rnd3 :: Int->IO Int rnd3 x= do oldState <- getStdGen let (result,newState) = randomR (1

我在haskell GHCI上写遗传算法

当我输入
子项[“+12”,“*34”]
GHCI写入
并挂断:( 所有函数都是单独工作的,只有在同时使用函数(交叉、rnd3、gNt工作规范)和随机数时才会出现错误

代码:

-----随机输入-----------------------------------

rnd3 :: Int->IO Int
rnd3 x= do 
  oldState <- getStdGen
  let (result,newState) = randomR (1,x) oldState
  setStdGen newState
  return (result)
gNt :: String->Int->String
gNt l n = unwords ( gNtWords n (words l) )


opX :: [String] -> [String]
opX (fst:x) = helper 1 x []
     where
    helper 0 l ans = reverse ans
    helper n (fst:x) ans = if ( fst == "+" || fst == "-" || fst == "*" || fst == "/" ) then helper (n+1) x (fst:ans)
                        else helper (n-1) x (fst:ans)

opY :: [String] -> [String]
opY (fst:x) = helper 1 x []
     where
    helper 0 l ans = l
    helper n (fst:x) ans = if ( fst == "+" || fst == "-" || fst == "*" || fst == "/" ) then helper (n+1) x (fst:ans)
                        else helper (n-1) x (fst:ans)


gNtWords:: Int -> [String] -> [String]

gNtWords n [] = []
gNtWords 0  l = l
gNtWords 1 l = (opX l)
gNtWords 2 l = (opY l)
gNtWords n l = if( n < (length (opX l) + 2 ) ) then  gNtWords (n - 2 )  (opX l) 
            else  gNtWords (n - (length (opX l)) - 1 ) (opY l)
-- ( gNtWords (n + nodeCount (opX l) ) (opY l) ) )
rNt :: String->String->Int->String
rNt expTree newExp n = unwords (rNtWord n (words expTree) (words newExp) )

rNtWord:: Int -> [String] -> [String] -> [String]

rNtWord n l newExp = replacer n l newExp 
                            where 
replacer 0 l newExp  = newExp       
replacer 1 (l) newExp  = (   (++) [head l]  (  (++) newExp (opY l)  )   )
replacer 2 (l) newExp = (  (++) [head l] ( (++) (opX l) newExp )  )
replacer n l newExp = if( n < (length (opX l) + 2 ) ) then (++) [head l]  ((++) (replacer (n - 2)(opX l) newExp ) (opY l))
                            else (++) [head l] (  (++) (opX l) (replacer(n-(length(opX l))-1)(opY l) newExp ))
crossover :: String->String->String

crossover exp1 exp2 = let rnd1 = ( unsafePerformIO (rnd3 (nCount exp1) ) ) -1
                        ;rnd2 = (unsafePerformIO (rnd3 (nCount exp2) )) - 1
                        in  rNt exp1 (gNt exp2 rnd2) rnd1
norm :: Int -> Float

norm size =  let sizeF = fromIntegral (size) 
                ;rnd1 = ( fromIntegral ( unsafePerformIO (rnd3 2000 ) ) )/1000 - 1
                ;rnd2 = ( fromIntegral ( unsafePerformIO (rnd3 2000 ) ) )/1000 - 1
                ;s = rnd1*rnd1 +rnd2*rnd2
                in  ( cos (2.0 * 3.14 * rnd1 ) * sqrt( -2.0*log(rnd2) ))
----------正态分布---------------------------------------

rnd3 :: Int->IO Int
rnd3 x= do 
  oldState <- getStdGen
  let (result,newState) = randomR (1,x) oldState
  setStdGen newState
  return (result)
gNt :: String->Int->String
gNt l n = unwords ( gNtWords n (words l) )


opX :: [String] -> [String]
opX (fst:x) = helper 1 x []
     where
    helper 0 l ans = reverse ans
    helper n (fst:x) ans = if ( fst == "+" || fst == "-" || fst == "*" || fst == "/" ) then helper (n+1) x (fst:ans)
                        else helper (n-1) x (fst:ans)

opY :: [String] -> [String]
opY (fst:x) = helper 1 x []
     where
    helper 0 l ans = l
    helper n (fst:x) ans = if ( fst == "+" || fst == "-" || fst == "*" || fst == "/" ) then helper (n+1) x (fst:ans)
                        else helper (n-1) x (fst:ans)


gNtWords:: Int -> [String] -> [String]

gNtWords n [] = []
gNtWords 0  l = l
gNtWords 1 l = (opX l)
gNtWords 2 l = (opY l)
gNtWords n l = if( n < (length (opX l) + 2 ) ) then  gNtWords (n - 2 )  (opX l) 
            else  gNtWords (n - (length (opX l)) - 1 ) (opY l)
-- ( gNtWords (n + nodeCount (opX l) ) (opY l) ) )
rNt :: String->String->Int->String
rNt expTree newExp n = unwords (rNtWord n (words expTree) (words newExp) )

rNtWord:: Int -> [String] -> [String] -> [String]

rNtWord n l newExp = replacer n l newExp 
                            where 
replacer 0 l newExp  = newExp       
replacer 1 (l) newExp  = (   (++) [head l]  (  (++) newExp (opY l)  )   )
replacer 2 (l) newExp = (  (++) [head l] ( (++) (opX l) newExp )  )
replacer n l newExp = if( n < (length (opX l) + 2 ) ) then (++) [head l]  ((++) (replacer (n - 2)(opX l) newExp ) (opY l))
                            else (++) [head l] (  (++) (opX l) (replacer(n-(length(opX l))-1)(opY l) newExp ))
crossover :: String->String->String

crossover exp1 exp2 = let rnd1 = ( unsafePerformIO (rnd3 (nCount exp1) ) ) -1
                        ;rnd2 = (unsafePerformIO (rnd3 (nCount exp2) )) - 1
                        in  rNt exp1 (gNt exp2 rnd2) rnd1
norm :: Int -> Float

norm size =  let sizeF = fromIntegral (size) 
                ;rnd1 = ( fromIntegral ( unsafePerformIO (rnd3 2000 ) ) )/1000 - 1
                ;rnd2 = ( fromIntegral ( unsafePerformIO (rnd3 2000 ) ) )/1000 - 1
                ;s = rnd1*rnd1 +rnd2*rnd2
                in  ( cos (2.0 * 3.14 * rnd1 ) * sqrt( -2.0*log(rnd2) ))
------------------------生孩子--------------

rnd3 :: Int->IO Int
rnd3 x= do 
  oldState <- getStdGen
  let (result,newState) = randomR (1,x) oldState
  setStdGen newState
  return (result)
gNt :: String->Int->String
gNt l n = unwords ( gNtWords n (words l) )


opX :: [String] -> [String]
opX (fst:x) = helper 1 x []
     where
    helper 0 l ans = reverse ans
    helper n (fst:x) ans = if ( fst == "+" || fst == "-" || fst == "*" || fst == "/" ) then helper (n+1) x (fst:ans)
                        else helper (n-1) x (fst:ans)

opY :: [String] -> [String]
opY (fst:x) = helper 1 x []
     where
    helper 0 l ans = l
    helper n (fst:x) ans = if ( fst == "+" || fst == "-" || fst == "*" || fst == "/" ) then helper (n+1) x (fst:ans)
                        else helper (n-1) x (fst:ans)


gNtWords:: Int -> [String] -> [String]

gNtWords n [] = []
gNtWords 0  l = l
gNtWords 1 l = (opX l)
gNtWords 2 l = (opY l)
gNtWords n l = if( n < (length (opX l) + 2 ) ) then  gNtWords (n - 2 )  (opX l) 
            else  gNtWords (n - (length (opX l)) - 1 ) (opY l)
-- ( gNtWords (n + nodeCount (opX l) ) (opY l) ) )
rNt :: String->String->Int->String
rNt expTree newExp n = unwords (rNtWord n (words expTree) (words newExp) )

rNtWord:: Int -> [String] -> [String] -> [String]

rNtWord n l newExp = replacer n l newExp 
                            where 
replacer 0 l newExp  = newExp       
replacer 1 (l) newExp  = (   (++) [head l]  (  (++) newExp (opY l)  )   )
replacer 2 (l) newExp = (  (++) [head l] ( (++) (opX l) newExp )  )
replacer n l newExp = if( n < (length (opX l) + 2 ) ) then (++) [head l]  ((++) (replacer (n - 2)(opX l) newExp ) (opY l))
                            else (++) [head l] (  (++) (opX l) (replacer(n-(length(opX l))-1)(opY l) newExp ))
crossover :: String->String->String

crossover exp1 exp2 = let rnd1 = ( unsafePerformIO (rnd3 (nCount exp1) ) ) -1
                        ;rnd2 = (unsafePerformIO (rnd3 (nCount exp2) )) - 1
                        in  rNt exp1 (gNt exp2 rnd2) rnd1
norm :: Int -> Float

norm size =  let sizeF = fromIntegral (size) 
                ;rnd1 = ( fromIntegral ( unsafePerformIO (rnd3 2000 ) ) )/1000 - 1
                ;rnd2 = ( fromIntegral ( unsafePerformIO (rnd3 2000 ) ) )/1000 - 1
                ;s = rnd1*rnd1 +rnd2*rnd2
                in  ( cos (2.0 * 3.14 * rnd1 ) * sqrt( -2.0*log(rnd2) ))
如果父项中的rnd不是随机数(例如rnd=1或rnd=2),则所有操作均有效

children:: [String] -> String
children pop = crossover (parent pop) (parent pop)
或如果在这里
(父流行音乐)
(++)(父流行音乐)(父流行音乐)
所有作品

children:: [String] -> String
children pop = crossover (parent pop) (parent pop)

在什么错误中?在我看来,或者我不知何故不这样做随机数(在其他函数中它们正常工作)对不起我的英语(和haskell))

没有理由在这里使用
unsafePerformIO
,或者几乎从来都没有。只要假装它不存在。它不会让你的代码变得更好,更容易阅读或编写,而且通常会产生代码不工作的副作用。有一些基本的功能,特别是monad。我强烈建议你选择一个,然后仔细检查整个过程(第一个很好)

作为旁注,您对
rnd3
的定义与
rnd3 x=randomRIO(1,x)


要记住的一个关键点是,你不能逃避
IO
do
块中的最后一个语句必须具有
ioa

我的决定:首先,我写了基于Int移动的素数生成器

myRandom :: Int -> Int -> Int

myRandom gen range= let a = 6364136223846793005
                    ;c = 1442695040888963407
                    ;m = 2^32--2^64
                in   mod(mod (a*gen + c) (2^16)) range
32\64度中的两个被除为零,结果如何

Received casual Int是生成器的以下入口值。然后我了解到,通过我们隐藏成功StdGen可以存储,而不是在违抗函数中存储Int

rand :: StdGen->Int->(Int,StdGen)
rand gen range= randomR (0,range-1)  gen  

第一代使用mkStdGen获取(使用unsafePerformIO从我的rnd3中麻木..抱歉)

这不是
unsafePerformIO
的合法使用。如果您的下一个问题是“什么是可接受的使用?”“那么答案很简单:不要使用
unsafePerformIO
。如何才能以不同的方式接收临时邮件?我很长时间以来都在忍受接收随机数的痛苦,我不知道如果不使用转换,我怎么能做到这一点,不同的是,我不会使用它。这是我在哈斯克尔的第一个节目,放纵一下。谢谢你的回答。一些替代方法:你可以使用
MonadRandom
,你可以创建一个无限的随机列表并使用该列表中的值,你可以在IO monad中运行所有东西,或者你可以传递StdGen而不是使用全局(可变)StdGen。@你可以在IO monad中编写代码,您不必退出IO即可进行计算。Haskell中IO单子的目的是迫使您分离纯代码和非纯代码。写下你可以使用的函数,当你需要生成一个随机数时,让该函数执行IO,并将其保留在类型签名中。使用do符号将有助于使这一过程变得简单和优雅。如果您想要一种更简单的生成随机数的方法,请查看MonadRandom软件包。@bheklillr谢谢,I tru
index::[String]->IO Int->String------index(x:xs)1=x------index(x:xs)n=index xs(n-1)
您可以在这个简单的示例中演示如何返回列表的第n个元素