Search code examples
haskellcompiler-errorsparse-error

parse error (possibly incorrect indentation or mismatched brackets) on line with let


I get

parse error (possibly incorrect indentation or mismatched brackets)

on the line with | i > j = do swap axs p j

apartition :: Ord a => STArray s Int a -> Int -> Int -> ST s Int
apartition axs p q = do 
  x <- readArray axs p
  let loop i j
    | i > j = do swap axs p j
                 return j
    | otherwise = do u <- readArray axs i
                     if u < x 
                       then do loop (i + 1) j 
                       else do swap axs i j
                            loop i (j − 1)
  loop (p + 1) q

I am really confused about the cause of this, could someone clear things up for me?


Solution

  • This is an indentation error, you need to place the guard at least one character to the right of the start of the definition of loop, so:

    apartition :: Ord a => STArray s Int a -> Int -> Int -> ST s Int
    apartition axs p q = do 
      x <- readArray axs p
      let loop i j
           | i > j = do swap axs p j
                        return j
           | otherwise = do u <- readArray axs i
                            if u < x 
                            then do loop (i + 1) j 
                            else do swap axs i j
                                    loop i (j - 1)
      loop (p + 1) q