Count number of palindromes in a list of strings, Haskell

﹥>﹥吖頭↗ 提交于 2019-12-11 16:42:23

问题


The function countPalindromes receives a list of strings and returns a count of how many of the strings are palindromes.

isPalindrome :: String -> Bool
isPalindrome w = w == reverse w

countPalindromes :: [String] -> Int
countPalindromes ss = length filter (== isPalindrome) ss 

I know that the function length is applied to two arguments instead of one. I just don't know how to fix this?


回答1:


You may use parentheses to affect function application:

countPalindromes ss = length (filter (== isPalindrome) ss)

The parentheses will cause the entire expression filter (== isPalindrome) ss to be grouped into a single term, and its result passed on to length.

This will get you to the next error; I encourage you to read it carefully and see if you can make progress from here yourself, then open a fresh question if you spend, say, fifteen minutes trying to understand it without making progress.



来源:https://stackoverflow.com/questions/49498908/count-number-of-palindromes-in-a-list-of-strings-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!