English 中文(简体)
永远从警卫返回
原标题:always return true from guard
  • 时间:2012-01-12 15:11:16
  •  标签:
  • haskell

I m trying to figure out a solution for Problem 27 of 99 Haskell questions.
Here s how I want it to be:

  group :: (Eq a) => [Int] -> [[a]] -> [[[[a]]]]
  group []     _  = []
  group (i:is) xs 
    | sum (i:is) /= length xs = error "invalid arguments"
    | otherwise               = ...

联系的一个例子:

group [2,2,5] ["aldo","beat","carla","david","evi","flip","gary","hugo","ida"]
[[["aldo","beat"],["carla","david"],["evi","flip","gary","hugo","ida"]],...] (altogether 756 solutions)

因此,我想首先检查一下Int名单的总和是否与上述长处清单相同。 我认为,无论这两种价值观是否平等,都永远不印刷“无效的论点”。 我也试图这样做:

group (i:is) xs 
     | (sum (i:is) == length xs) = ...
     | otherwise                 = error "invalid arguments"

still doesn t work
Any ideas?

<>UPDATES 我的无能为力。 职能中可收回的部分:

 group (i:is) xs 
     | (sum (i:is) == length xs) = filter (/= []) $ concatGroups (combinations i xs) (group is xs)
     | otherwise                 = error ("invalid arguments: " ++ show (sum(i:is)) ++ "/=" ++ show(length xs))

如你所知,<代码>组为xs,减少数额,但缩短时间,因此在休补时总是会提出申诉。 我认为,我只想去掉这一警卫,希望使用者永远不会这样做。

最佳回答

如果你把你的法典改成黑帮的话,扩大黑帮。

group :: (Eq a) => [Int] -> [[a]] -> [[[[a]]]]
group []     _  = []
group (i:is) xs 
  | sum (i:is) /= length xs = error ("invalid arguments to group: sum "
                                     ++ show (i:is) ++ " /= " ++ show (length xs))
  | otherwise               = ...

这将有助于你追踪<条码>......部分的错误。

问题回答

暂无回答




相关问题
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?

热门标签