Showing posts with label Haskell Error. Show all posts
Showing posts with label Haskell Error. Show all posts

Saturday, February 1, 2014

Haskell Error 3


quicksort.hs:6:42:
    Unexpected parallel statement in a list comprehension
    Use -XParallelListComp

quicksort.hs:6:44: Not in scope: `a'

I placed '|' (pipeline) instead of a ',' (comma) to separate predicate section. Program with Error:
quicksort :: (Ord a) => [a] -> [a] 
quicksort [] = []
quicksort(x:xs)=let smallerSorted = quicksort [a | a <- xs, a <= x]
                    biggerSorted = quicksort [a | a <- xs| a > x] 
--But it supposed to be "a <- xs, a > x"
in smallerSorted ++ [x] ++ biggerSorted


Haskell Error 2

quickSort.hs:4:1:
    parse error (possibly incorrect indentation or mismatched brackets)
quickSort.hs:4:54: parse error on input `='


Two ways to avoid the above error:
    1: Haskell expects right indentation.
    2: Shouldn't use 'Tab Spacing' for indentation. We must use space bar.

The indentation error occurs only for specific keywords. The 'let' keyword expects the next line to be indented. And then the 'in' keyword should start the same column as 'let' starts.

I don't know why we shouldn't use 'Tab' for indentation. I got the above error because I used the 'Tab Spacing'. Used space bar and solved the problem.
Correct Version:
quicksort :: (Ord a) => [a] -> [a] 
quicksort [] = []
quicksort(x:xs)=let smallerSorted = quicksort [a | a <- xs, a <= x]
                    biggerSorted = quicksort [a | a <- xs, a > x]
                in smallerSorted ++ [x] ++ biggerSorted

Haskell Error 1

Invalid type signature: GetMax :: Ord a => [a] -> a
Should be of form <variable> :: <type>

This is because, the function 'GetMax' starts with capital case, it should be 'getMax'. In haskell function names shouldn't start with capital case, only module names can start with capital case.
Example: getMax :: (Ord a) => [a] -> a