[英]Member Function in Haskell
Working on a small assignment for class, having a lot of trouble with Haskell. 正在为班级做少量作业,但Haskell遇到了很多麻烦。 I am trying to make a recursive method for finding if an integer is part of a list or not.
我正在尝试使用一种递归方法来查找整数是否为列表的一部分。 I know the gist, but am unable to get it working correctly with the haskell syntax.
我知道要点,但是无法通过haskell语法使其正常工作。 Check if the current list is empty, if so then False, then check if integer is equal to the head of the current list, if so, then True, then call member again with the same value you are searching for, and the tail of the list.
检查当前列表是否为空,如果是,则为False,然后检查integer是否等于当前列表的头部,如果是,则为True,然后再以您要搜索的相同值再次调用成员,并以名单。 What can I do to get this functioning properly.
我该怎么做才能使此功能正常运行。
Currently this is what I have: 目前,这是我所拥有的:
member ::Int -> [Int] -> Bool
member x y
if y [] then False
else if x == head y then True
else member x tail y
I have also tried using 我也尝试使用
member :: (Eq x) => x -> [x] -> Bool
as the beginning line, and also a much simplier : 作为起点,并且也更简单:
let member x y = if null y then False
else if x == head y then True
else member x tail y
Any help would be appreciated. 任何帮助,将不胜感激。
with pattern matching you can write it more clearly 与模式匹配,您可以更清晰地编写它
member :: (Eq a) => a -> [a] -> Bool
member x [] = False
member x (y:ys) | x==y = True
| otherwise = member x ys
element _ [] = False
element e (x:xs) = e == x || e `element` xs
-- OR
element e xs = if xs == [] then False
else if e == head xs then True
else e `element` tail xs
-- OR
element e xs = xs /= [] && (e == head xs || e `element` tail xs)
-- x `op` y = op x y
-- If you're feeling cheeky
element = elem
Your syntax appears very confused, but your logic makes sense, so here's a bucket list of things to remember: 您的语法看起来很混乱,但是您的逻辑是有道理的,因此这里有很多要记住的事情:
Functions can be defined by multiple equations. 函数可以由多个方程式定义。 Equations are checked top to bottom.
从上到下检查方程式。 That means using
=
. 这意味着使用
=
。
Pattern matches are not equality tests. 模式匹配不是相等性测试。 A pattern match breaks a value into its constituents if it matches and fails otherwise.
模式匹配如果匹配则将值分解为其组成部分,否则失败。 An equality test
x == y
returns a Bool
about the equality of x
and y
. 相等性测试
x == y
返回有关x
和y
相等性的Bool
。
Pattern matching is used for flow control via... 模式匹配用于通过...进行流量控制
a case
statement, like case
陈述,例如
case xs of { [] -> ... x:xs' -> ... }
Multiple equations, like 多个方程式,例如
element _ [] = ... element e (x:xs) = ...
Note that you can ignore a value in a pattern with _
. 请注意,您可以使用
_
忽略模式中的值。 With multiple equations of a function with multiple arguments, you're really pattern matching on all the arguments at once. 对于具有多个参数的函数的多个方程式,您实际上是一次对所有参数进行模式匹配。
Bool
s are used for flow control via if _ then _ else _
: Bool
通过if _ then _ else _
用于流量控制:
if xs == [] then False else True
which is really just 这真的只是
case x == y of { True -> False False -> True }
and Bool
s can use the ordinary operators (&&)
( infixr 3
) and (||)
( infixr 2
) Bool
可以使用普通运算符(&&)
( infixr 3
)和(||)
( infixr 2
)
The difference is especially nefarious on lists. 列表上的差异尤其有害。
instance Eq a => Eq [a]
, so in order to use ==
on lists, you need to know that the elements of the lists can be compared for equality, too. instance Eq a => Eq [a]
,因此为了在列表上使用==
,您需要知道列表元素也可以进行相等比较。 This is true even when you're just checking (== [])
. 即使仅检查
(== [])
。 [] == []
actually causes an error, because the compiler cannot tell what type the elements are. [] == []
实际上会导致错误,因为编译器无法确定元素的类型。 Here it doesn't matter, but if you say, eg nonEmpty xs = xs /= []
, you'll get nonEmpty :: Eq a => [a] -> Bool
instead of nonEmpty :: [a] -> Bool
, so nonEmpty [not]
gives a type error when it should be True
. 这里没关系,但是如果您说,例如
nonEmpty xs = xs /= []
,您将得到nonEmpty :: Eq a => [a] -> Bool
而不是nonEmpty :: [a] -> Bool
,因此nonEmpty [not]
在应为True
时会给出类型错误。
Function application has the highest precedence, and is left-associative: 函数应用程序的优先级最高,并且是左关联的:
element x xs
reads as ((element x) xs)
element x xs
读为((element x) xs)
element x tail xs
reads as (((element x) tail) xs)
, which doesn't make sense here element x tail xs
读为[ (((element x) tail) xs)
,在这里没有意义 f $ x = fx
, but it's infixr 0
, which means it basically reverses the rules and acts like a big set of parentheses around its right argument f $ x = fx
,但是它是infixr 0
,这意味着它基本上颠倒了规则,并且在其正确的参数周围就像一大套括号
element x $ tail xs
reads as ((element x) (tail xs))
, which works element x $ tail xs
读为((element x) (tail xs))
, Infix functions always have lower precedence than prefix application: 中缀函数的优先级始终低于前缀应用程序:
x `element` tail xs
means ((element x) (tail xs))
, too x `element` tail xs
意思也是((element x) (tail xs))
let decls in expr
is an expression . let decls in expr
中的let decls in expr
是一个表达式 。 decls
is only in scope inside expr
, and the entire thing evaluates to whatever expr
evaluates to. decls
仅在expr
内的作用域内,并且整个事物的计算结果均decls
expr
评估的结果。 It makes no sense on the top level. 在顶层没有意义。
Haskell uses indentation to structure code, like Python. Haskell使用缩进来构造代码,例如Python。 Reference
参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.