简体   繁体   English

尝试在Haskell中“打印”列表时出错

[英]Error trying to “print” list in Haskell

I have the following problem: given a max(max) apacity, and given a list of values(listOfValues) i need to return a list with values from the listOfValues. 我有以下问题:给定一个max(max)容量,并给定一个值列表(listOfValues),我需要返回一个列表,其中包含listOfValues中的值。 The sum of the elements must be <= max and i need to prioritize the higher values. 元素的总和必须<= max,并且我需要优先考虑较高的值。

Example: typing solvingProblem 103 [15, 20, 5, 45, 34] i must get: [45, 45, 5, 5] 示例:键入SolutionProblem 103 [15,20,5,45,34]我必须得到:[45,45,5,5]

To solve the problem i create the following code: 为了解决这个问题,我创建了以下代码:

solvingProblem max [] = 0
solvingProblem max listOfValues | max == 0 = 0
                                | otherwise = createList max listOfValues []


createList max [] result = -1
createList max listOfValues result | smaller listOfValues > max = -1
                               | higher listOfValues > max = createList max (remove (higher listOfValues) listOfValues) result
                               | otherwise = createList (max - higher listOfValues) listOfValues (insert (higher listOfValues) result)

higher [a] = a
higher (a:b:x) | a > b = higher (a:x)
              | otherwise = higher (b:x)

smaller [a] = a
smaller (a:b:x) | a < b = smaller (a:x)
              | otherwise = smaller (b:x)

remove x [] = []
remove x (h:t) | x == h = remove x t
                | otherwise = h : remove x t

insert x (h:t) = x : h : t

In the two lines where i'll returning "-1" should be the parameter "result", but if i change "-1" to "result" the code don't load on ghci. 在我将返回“ -1”的两行中,应将参数“ result”作为参数,但是如果我将“ -1”更改为“ result”,则代码不会加载到ghci上。

Can someone help me? 有人能帮我吗?

Thank you and sorry for my bad english. 谢谢,抱歉我的英语不好。

If I may begin with a bit of a side note, some of your functions already exist in Haskell (now that I come to think of it you might have written them for an exercise, but just in case it wouldn't be the case, let's discuss that): your higher is maximum , your smaller is minimum and your insert is just (:) , beacause like you write it yourself insert x list = x:list . 如果我可能需要一些旁注,那么您的某些功能已经存在于Haskell中(现在我想到了,您可能是为练习而编写的,但是以防万一,让我们来讨论):你的highermaximum ,你的smallerminimum ,你的插入只是(:) ,东阳就像你写它自己insert x list = x:list Note that your version will fail if you give it the empty list because the pattern matching is non-exhaustive. 请注意,如果您提供的版本为空列表,则版本将失败,因为模式匹配并不详尽。 Also you could write remove in terms of filter: remove x list = filter (== x) list . 您也可以按照filter来写removeremove x list = filter (== x) list

Now why doesn't your code load properly? 现在,为什么您的代码无法正确加载? ghci tells you: ghci告诉您:

• Non type-variable argument in the constraint: Num [a]
  (Use FlexibleContexts to permit this)
• When checking the inferred type
    solvingProblem :: forall a.
                      (Ord a, Num [a], Num a) =>
                      a -> [a] -> [a]

Which I agree is pretty cryptic, but what it's saying is that the return type of solvingProblem is a list of a and for some reason it is also an instance of the Num type class. 这我同意是非常神秘的,但就是它的意思是说的返回类型solvingProblem是清单a ,由于某种原因它也是实例Num类型的类。 The reason why it says it's an instance of Num is because one of the return value of solvingProblem is 0 which is a number, which is a bit odd because it is also a list. 之所以说它是Num的实例,是因为solvingProblem的返回值之一是0 ,它是一个数字,这有点奇怪,因为它也是一个列表。 Changing the 0 with [] makes the code compile and work (if you change insert with (:) otherwise you get the non-exhaustive pattern matching I was talking about earlier). []更改0可使代码编译并工作(如果使用(:)更改insert ,则将得到我之前提到的非穷尽模式匹配)。

λ> solvingProblem 103 [15,20, 5, 45, 34]
[5,5,45,45]
it :: (Ord t, Num t) => [t]

The problem is with the last guard clause in createList . 问题在于createList的最后一个保护子句。

The type you intended for createList seems to be: 您打算用于createList的类型似乎是:

createList :: Int -> [Int] -> Int -> Int

but if you look at the last guard clause you have: 但是,如果您查看最后一个保护子句,您将:

| otherwise = createList (max - ...) listOfValues (insert ...)
                         ^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^^
                          Int        [Int]        [Int]

Even though GHC is very good at inferring types, always adding type signatures to your code is a good way of catching these kinds of errors early. 尽管GHC非常擅长推断类型,但始终在代码中添加类型签名是一种尽早发现此类错误的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM