求问一道Haskell题目,是一道英文题目,我害怕翻译不到位,还是把原题po上来了:Give the type and definition for a funtion called 'matches' that take an element and a list where the element is of the same type as the elements in

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/30 05:18:58
求问一道Haskell题目,是一道英文题目,我害怕翻译不到位,还是把原题po上来了:Give the type and definition for a funtion called 'matches' that take an element and a list where the element is of the same type as the elements in
xTMSP+wΤ֮`gMq0d I vQ֊WX8vl%d_{ X좻nsϽܓԈ}>Vis$=:; ޮ򹽕D kqaeY )4 ViD>PX撽j-]^oГU@rBBE]TdH( HeC$āK!=0Gː:fu HBJu$bf"%E0J zaQMic5Ό!FIN*i)i,&fәbPXJ)Y= `JCZ٨1fR5!Y72(JBO)??OB8 IMOzYqqb2hd!_`n6Rzg?6ɷJ-+)Xf֨kom#]n?L1M{ҨYW@ =[yrR2Aj:` J2؆Сc:;&)Iu19u6O]ctg$CrJ.-sìvIkGHstQ)-:_Zer

求问一道Haskell题目,是一道英文题目,我害怕翻译不到位,还是把原题po上来了:Give the type and definition for a funtion called 'matches' that take an element and a list where the element is of the same type as the elements in
求问一道Haskell题目,
是一道英文题目,我害怕翻译不到位,还是把原题po上来了:Give the type and definition for a funtion called 'matches' that take an element and a list where the element is of the same type as the elements in the list.You should use recursive function style and may not use any library functions.Take care to deal with empty lists.For example:matches 3 [0,1,3,2,4] == [3] mathches 'a' ''aspidistra'' == " aa" matches 3 [ ] = [ ]
意思是:请给出一个名为‘matches’的函数的type类型和定义.这个函数有一个element和一个list (这一个element和这个list中的所有elements都是相同的类型).需要使用递归函数的形式并且不得使用任何库函数.小心处理空lists.例如:matches 3 [0,1,3,2,4] == [3] mathches 'a' ''aspidistra'' == " aa" matches 3 [ ] = [ ]

求问一道Haskell题目,是一道英文题目,我害怕翻译不到位,还是把原题po上来了:Give the type and definition for a funtion called 'matches' that take an element and a list where the element is of the same type as the elements in
matches :: (Eq a) => a -> [a] -> [a]
matches a [] = []
matches a (x:xs) = if a == x then x:matches a xs else matches a xs
类型必须是Eq的实例,不然==无法判断,至少例子上的整数和字符都是的,大部分常见的类型也都是Eq的实例