Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
Loops while循环中的Bug_Loops_Haskell - Fatal编程技术网

Loops while循环中的Bug

Loops while循环中的Bug,loops,haskell,Loops,Haskell,当我在下面写的循环时,我无法找出玩具中的bug是什么。它对一个输入有效,但对其他输入挂起。下面是代码-while循环接受向量、向量上的谓词和向量上的转换函数,并返回另一个向量: import Data.Vector.Unboxed as U while :: Vector Int -> (Vector Int -> Bool) -> (Vector Int -> Vector Int) -> Vector Int while v p f = go 0 v

当我在下面写的循环时,我无法找出玩具
中的bug是什么。它对一个输入有效,但对其他输入挂起。下面是代码-while循环接受向量、向量上的谓词和向量上的转换函数,并返回另一个向量:

import Data.Vector.Unboxed as U

while :: Vector Int -> (Vector Int -> Bool) -> (Vector Int -> Vector Int) -> Vector Int
while v p f = go 0 v
      where go n x = if (p x) then go (n+1) (f x)
                              else x

test :: Vector Int -> Vector Int
test a = while a (\x -> (x!0) < 2) (\x -> U.map (+1) a)

main = print $ test (U.fromList [0])
我得到一个带有结果的终止(
ghci
下面的输出):

我觉得我一定错过了什么。我仔细查看了函数,但不知道我做错了什么。看起来谓词的执行次数不能超过两次

顺便说一句,它可以与其他类型一起使用,如
Int

   while :: Int -> (Int -> Bool) -> (Int -> Int) -> Int
    while i p f = go 0 i
          where go n x = if (p x) then go (n+1) (f x)
                                  else x

    test :: Int ->  Int
    test a = while a (\x -> x < 2) (\x -> x+1)

    main = print $ test 0
GHC版本:7.6.1


Vector版本:0.10.0.1

aha,很高兴看到它这么简单!
test a = while a (\x -> (x!0) < 1) (\x -> U.map (+1) a)
   while :: Int -> (Int -> Bool) -> (Int -> Int) -> Int
    while i p f = go 0 i
          where go n x = if (p x) then go (n+1) (f x)
                                  else x

    test :: Int ->  Int
    test a = while a (\x -> x < 2) (\x -> x+1)

    main = print $ test 0
λ: main
2
test a = while a (\x -> (x!0) < 1) (\x -> U.map (+1) a)
test a = while a (\x -> (x!0) < 1) (\x -> U.map (+1) x)
test a = while a (\x -> (x!0) < 1) (U.map (+1))
while v p f = go v
      where go x = if p x then go (f x) else x