Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

beginner haskell programmer

Tags:

haskell

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?

like image 691
doug Avatar asked Sep 06 '11 06:09

doug


People also ask

Can a beginner learn Haskell?

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 2022?

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.

How long does it take to learn Haskell?

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.

Is Haskell harder than Python?

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.


1 Answers

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)
like image 83
MGwynne Avatar answered Nov 15 '22 07:11

MGwynne