isPalindrome::(Eq a) => [a] -> Bool
isPalindrome [] = True
isPalindrome [x] = True
isPalindrome (x1:xs:x2:[])
| x1 == x2 = isPalindrome xs
|otherwise = False
[1 of 1] Compiling Main ( myHas.hs, interpreted )
myHas.hs:37:27:
Couldn't match expected type `[a]' against inferred type `a1'
`a1' is a rigid type variable bound by
the type signature for `isPalindrome' at myHas.hs:33:18
In the first argument of `isPalindrome', namely `xs'
In the expression: isPalindrome xs
In the definition of `isPalindrome':
isPalindrome (x1 : xs : x2 : [])
| x1 == x2 = isPalindrome xs
| otherwise = False
I'm a beginner haskell programmer and got no clue as to why I'm getting this error, any help please?
So, yes, you can use Haskell, but you should focus on elementary, general techniques and essential concepts, rather than functional programming per se.
Is Haskell Worth Learning in 2022? Yes, Haskell is worth learning in 2022 because functional languages like it are getting more popular among big companies like Facebook. Functional languages are typically ideal for big data and machine learning.
To start building real-life applications with Haskell, you can expect to spend about two to three months working on the ins and outs of the language.
As mentioned earlier, Python is easier than Haskell to learn. The learning curve for Haskell is steep, especially for those with no prior functional programming experience. In terms of library support, Python has more libraries and use-cases than Haskell.
You treat xs
like a list, but (x1:xs:x2:[])
assumes it is an element of your input list.
Note that (x1:xs:x2:[])
will match only lists with 3 elements, and x1
, xs
and x2
will be elements of type a
.
So xs
is of type a
, but as you pass it to isPalindrome
, we can only assume it must be a list of something, so the type system calls the type [a1]
.
The easiest way to encode what you want is:
isPalindrome::(Eq a) => [a] -> Bool
isPalindrome l = l == (reverse l)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With