the task is to implement a function palindrome (:: String -> Bool) which checks whether the input is a palindrome.
import Data.List
palindrome :: String -> Bool
palindrome str
|str == revstring = True
|otherwise = False
revstring :: String -> String
revstring str = reverse str
I guess I made a mistake in my signature, but I don't know exactly where.
str == revstring
On the left of the equality, we have a string ( str
). On the right, a function ( revstring
). This can't be right. We can fix it using instead
str == revstring str
However, at that point, we can simply call reverse
. There is no need to define revstring
at all.
We could then have
palindrome :: String -> Bool
palindrome str
|str == reverse str = True
|otherwise = False
but this is still sub-optimal. The comparison str == reverse str
already produces the Bool
value we need to return. There is no need to use guards to check that value, we can simply return it.
palindrome :: String -> Bool
palindrome str = str == reverse str
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.