简体   繁体   English

检查字符串是否在“i”字母处是回文?

[英]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.

相关问题 Haskell(相当长)回文检查 - Haskell (formidably long) palindrome check 如何重复字符串的字母以匹配另一个字符串的长度? - How do I repeat letters of a string to match the length of another string? 如何检查((字符串对和(字符串列表)的列表))中的字符串? - How can I check string in a (list of ( pair of string and (list of string)))? 如何在Haskell的字符串列表中添加字符串的字母? - How to add letters of a String in a String list in Haskell? 从字符串替换符号中过滤字母,以空格和小写字母代替 - Filter letters from string replacing symbols for blanks spaces and letters lowercase 我如何以无点样式重写这个回文验证器? - How would I rewrite this palindrome verifier in point-free style? 检查字符串中的所有元音是否都是大写字母的函数 - Function that checks if all vowels in a string are capital letters Haskell - 使用toUpper将列表[String]中的所有字母大写 - Haskell - Capitalize all letters in a list [String] with toUpper 如何检查字符串是否可以在 Haskell 中解析为某种类型? - How do i check if a string can be parsed to a certain type in Haskell? Haskell,如何遍历[[String type]]以检查列表中是否有给定的字符串? - Haskell, how can I traverse a [[String type]] to check whether a given string is in the list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM