English 中文(简体)
详细清单
原标题:debugging list comprehension

I copied the Euler.11 solution given on ( http://www.haskell.org/haskellwiki/Euler_problems/11_to_20#Problem_11), but it fails with an indexing error: " (Array.!): undefined array element". Of course firstly I d like a better error message(!), perhaps even giving the failed index, but failing that I tried to debug it.

这些数据是正确的,其印刷版显示了正确的界限和数据。

So I added a few trace messages, to both the result expression, and the body of the comprehension. I get lots of trace results from the final expression, but none from the body computations. Why?

prods :: Array (Int, Int) Int -> [Int]
-- trace ("xs: " ++ show xs) (product xs)
prods a = [trace ("xs: " ++ show xs) (product xs) | i <- range $ bounds a,
                        s <- senses,
                        let trace1 = check "i: " i,
                        let is = take 4 $ iterate s i,
                        let trace2 = check "is: " is,
                        all (inArray a) is,
                        let xs = map (a!) is]

-- Doit
-- euler = print . maximum . prods . input =<< getContents
euler eData = maximum . prods $ input eData

-- Debugging tracecheck :: String -> a -> a
check msg v | trace (msg ++ (show v)) True = v
最佳回答

首先,使错误信息包括失败指数将需要<代码>。 显示限制在阵列指数中,这些限制可能未经许可。

第二,正如罗马人所说的那样,这些电文不是由于zy评价而印制的。 Bang-patterns (let !trace = 核对“i: “i)可能是绕过它的最方便的方法,但我不知道在名单上的行文中如何做这些工作。

其次,<代码>未界定阵列要素的电文告诉你,阵列的构造是不正确的(有些内容没有界定),因此,你需要缩减构造功能,而不是使用它的功能。

问题回答

Haskell is a lazy language(*). You do not use trace1 and trace2 in your computation, hence they are not evaluated, and nothing gets printed.

For example, if you replace

let is = take 4 $ iterate s i

iii

let is = take 4 $ iterate s trace1

然后将使用<代码>trace1,并导致跟踪信息。

(*) 更确切地说,GHC是今天最常用的Haskell执行工作,它利用zy评价实现Haskell的非限制性杂质。 如果这样做,就永远不会感觉到。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签