求问一道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/04/29 20:52: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
求问一道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的实例