[英]Check if string is palindrome at "i" letters?
I got a program that checks if string is palindrome or not.我有一个程序可以检查字符串是否为回文。 I want to add functionality that lets user check if given string is palindrome at i letters.
我想添加功能,让用户检查给定的字符串是否在 i 个字母处是回文。 For example if string is "abbaabba" and user gives number 4 it returns true because "abba" is palindrome.
例如,如果字符串是“abbaabba”并且用户给出数字 4,则它返回 true,因为“abba”是回文。 If user gives 5 then it returns false, because "abbaa" is not palindrome.
如果用户给出 5,则返回 false,因为“abbaa”不是回文。
This is what I've done so far这是我到目前为止所做的
palindromes:: String -> Int -> Bool
palindromes p i
| p == reverse p = True
| otherwise = False
How do I add that functionality?如何添加该功能?
You can make use of take :: Int -> [a] -> [a]
here to pre-process the string:您可以在此处使用
take :: Int -> [a] -> [a]
来预处理字符串:
palindromes :: Eq a => [a] -> Int -> Bool
palindromes p i = p' == reverse p'
where p' =
take i p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.