忽略非字母数字字符和大小写Haskell的回文检查器

忽略非字母数字字符和大小写Haskell的回文检查器,haskell,functional-programming,palindrome,Haskell,Functional Programming,Palindrome,如果需要制作一个程序来检查给定的字符串是否是回文,它应该可以工作,无论大小写是否不同,并且应该忽略非字母数字字符,只使用Data.char和常规函数中的ord和chr函数,而不使用其他函数。我能够创建常规回文检查器: reverseStr::String->String reverStr s | s == [] = [] reverseStr (h:t) = reverseStr t ++ [h] isPalindrome :: String -> Bool isPalindrom

如果需要制作一个程序来检查给定的字符串是否是回文,它应该可以工作,无论大小写是否不同,并且应该忽略非字母数字字符,只使用Data.char和常规函数中的ord和chr函数,而不使用其他函数。我能够创建常规回文检查器:

reverseStr::String->String
reverStr s | s == [] = []
reverseStr (h:t) = reverseStr t ++ [h]

isPalindrome :: String -> Bool
isPalindrome s = s == reverseStr s
我已经开始编写一个函数来规范案例:

normalizeCase::String->String
normalizeCase h | h == [] = []
normalizeCase (h) = if ord h > 64 && ord h < 123
    then map (chr $ (ord h + 32)) [h]
    else h
normalizeCase::String->String
正常化状态h | h=[]=[]
标准化状态(h)=如果ord h>64&&ord h<123
然后映射(chr$(ord h+32))[h]
否则h
但我有以下错误:

• Couldn't match expected type ‘Char -> Char’
              with actual type ‘Char’
• In the first argument of ‘map’, namely ‘(chr $ (ord h + 32))’
  In the expression: map (chr $ (ord h + 32)) [h]
  In the expression:
    if ord h > 64 && ord h < 123 then
        map (chr $ (ord h + 32)) [h]
    else
        h

  |
6 |   then map (chr $ (ord h + 32)) [h]   |             ^^^^^^^^^^^^^^^^^^


    • Couldn't match type ‘Char’ with ‘[Char]’
  Expected type: String
    Actual type: Char
• In the expression: h
  In the expression:
    if ord h > 64 && ord h < 123 then
        map (chr $ (ord h + 32)) [h]
    else
        h
  In an equation for ‘normalizeCase’:
      normalizeCase [h]
        = if ord h > 64 && ord h < 123 then
              map (chr $ (ord h + 32)) [h]
          else
              h
  |
7 |   else h   |        ^
•无法匹配预期的类型“Char->Char”
实际类型为“Char”
•在“map”的第一个参数中,即“(chr$(ord h+32))”
在表达式中:map(chr$(ord h+32))[h]
在表达式中:
如果ord h>64&&ord h<123,则
地图(chr$(ord h+32))[h]
其他的
H
|
6 |然后映射(chr$(ord h+32))[h]|^^^^^^^^^^^^^^^^^^
•无法将类型“Char”与“[Char]”匹配
所需类型:字符串
实际类型:Char
•在表达式中:h
在表达式中:
如果ord h>64&&ord h<123,则
地图(chr$(ord h+32))[h]
其他的
H
在“正常化”的方程式中:
正常化酶[h]
=如果ord h>64&&ord h<123,则
地图(chr$(ord h+32))[h]
其他的
H
|
7 |否则h |^
我对Haskell还是一个新手,不知道如何正确地实现ord或chr,以便它能与此检查器一起工作,因此任何帮助都将不胜感激

在此代码中:

normalizeCase::String->String
normalizeCase h | h == [] = []
normalizeCase (h) = if ord h > 64 && ord h < 123
    then map (chr $ (ord h + 32)) [h]
    else h
或者,使用
map

normalizeCase :: String -> String

-- Given any string:
normalizeCase s

  -- For each character:
  = map

    -- If it’s uppercase, lowercase it.
    (\ c -> if c >= 'A' && c <= 'Z'
      then chr (ord c + 32)
      else c)

    -- Over the whole string.
    s

对于删除非字母数字字符的附加任务,您可以使用
过滤器
,一个带有保护条件的列表理解,或者自己编写一个直接递归版本。

一种方法是规范大小写,例如,在检查回文之前将所有字符都大写。作为第一步,您能否找出如何使用
ord
判断字符是否为小写?玩一下
ghci
中的函数,看看你是否能注意到一个模式。normalizeCase::String->String normalizeCase h | h=[]=[]normalizeCase[h]=如果ord h>64&&ord h<123,则映射(chr$(ord h+32))[h]否则h会给我错误,并且不太确定如何修复。或者使用
map
来“循环”列表,或手动递归模式匹配解构。(最好是
map
)您似乎正在尝试同时执行这两项操作…非常感谢,在检查此项后,我们能够找出其余部分,非常感谢您的帮助!
normalizeCase :: String -> String

-- Given any string:
normalizeCase s

  -- For each character:
  = map

    -- If it’s uppercase, lowercase it.
    (\ c -> if c >= 'A' && c <= 'Z'
      then chr (ord c + 32)
      else c)

    -- Over the whole string.
    s
import Data.Char (toLower)

normalizeCase :: String -> String
normalizeCase s = map toLower s

-- Alternatively, a point-free/eta-reduced version:
normalizeCase = map toLower