简体   繁体   English

比较Haskell中具有模式匹配的两个列表

[英]Compare two lists with pattern matching in Haskell

I am trying to implement a function where I have two lists, the first is of any type and the second is a Boolean, and I want it to return just the first list if it is equal to true. 我正在尝试实现一个有两个列表的函数,第一个是任何类型,第二个是布尔值,如果它等于true,我希望它仅返回第一个列表。 For example: 例如:

pickIt [1, 2, 3] [True, False, True]  returns [1, 3]

Here is my code: 这是我的代码:

pickIt :: [a] -> Bool -> [a]
pickIt (x:xs) (y:ys) = (x, y) : pickIt xs ys
pickIt _ _ = []

I think my type is wrong, but I am completely stumped on how to approach this. 我认为我的类型是错误的,但是我完全迷住了如何解决这个问题。 Any help, guidance, or a link to go in the right direction would be helpful. 任何帮助,指导或正确方向的链接都将有所帮助。

Your type is wrong, you said you have a list of bools (in English) then your type said you have a single Bool. 您的类型有误,您说有一个布尔值列表(英语),然后您的类型说有一个布尔值。 Use [Bool] instead of Bool . 使用[Bool]代替Bool

You said (by example) that you want a list of the element from the first list, so [a] , as a result. 您说(例如)要从第一个列表中获取元素的列表,因此是[a] Then your code return tuples of (a,Bool) (ie see your (x,y) value). 然后您的代码返回(a,Bool)元组(即查看您的(x,y)值)。 Instead test if y is true and only if so cons on x via x: . 而是测试y是否为true,并且只有在通过x:x进行验证的情况下进行测试。

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

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