3

宿題のために、Haskell で再帰を使用して回文チェッカーを作成する必要があります。この関数は文字列を受け取り、Bool. コンパイルしようとすると、エラーが発生します。"Couldn't match type ‘Bool’ with ‘[Char]’."

私は Haskell に非常に慣れていないので、ばかげたことを見落としているだけだと思いますが、とにかく助けを求めることにしました。以下に自分のコードと、受け取った完全なエラーを貼り付けました。

isPalindrome :: String -> Bool
isPalindrome s
  | isPalindrome ((null s) || (length s == 1)) = True
  | isPalindrome ((s !! 0) /= (s !! (length s - 1))) = False
  | otherwise = isPalindrome (tail (init s))

私の実装では、入力文字列が空か、サイズが 1 かどうかを確認し、そうであれば true を返します。そうでない場合は、最初の文字と最後の文字が異なるかどうかを確認し、異なる場合は false を返します。それ以外の場合は、最初と最後の文字が切り取られた文字列を渡して、再び自分自身を呼び出します。

main.hs:15:19: error:
    • Couldn't match type ‘Bool’ with ‘[Char]’
      Expected type: String
        Actual type: Bool
    • In the first argument of ‘isPalindrome’, namely
        ‘((null s) || (length s == 1))’
      In the expression: isPalindrome ((null s) || (length s == 1))
      In a stmt of a pattern guard for
                     an equation for ‘isPalindrome’:
        isPalindrome ((null s) || (length s == 1))
   |
15 |   | isPalindrome ((null s) || (length s == 1)) = True
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
main.hs:16:19: error:
    • Couldn't match type ‘Bool’ with ‘[Char]’
      Expected type: String
        Actual type: Bool
    • In the first argument of ‘isPalindrome’, namely
        ‘((s !! 0) /= (s !! (length s - 1)))’
      In the expression: isPalindrome ((s !! 0) /= (s !! (length s - 1)))
      In a stmt of a pattern guard for
                     an equation for ‘isPalindrome’:
        isPalindrome ((s !! 0) /= (s !! (length s - 1)))
   |
16 |   | isPalindrome ((s !! 0) /= (s !! (length s - 1))) = False
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4

1 に答える 1