tags:

views:

182

answers:

3

Hi ,

I met a problem caused by the tricky Indentation, here is the code looks in VI :

  1 import Data.List
  2 myQuickSort [] = []
  3 myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
  4     where   smaller = filter ( < x ) xs
  5             bigger  = filter ( >=x ) xs

But after ./cat 3.hs , It looks ,

root@pierr-desktop:/opt/playGround/haskell# cat 3.hs 
import Data.List
myQuickSort [] = []
myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
    where   smaller = filter ( < x ) xs
                    bigger  = filter ( >=x ) xs

Then load it in ghci

GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> :l 3.hs
[1 of 1] Compiling Main             ( 3.hs, interpreted )

3.hs:5:11: parse error on input `='
Failed, modules loaded: none.
Prelude>

How should I catch this invisible indentation error when programming haskell?

EDIT: Write it this way , the error will go. Is it a recommendded way to write the where binding - put variables in different lines as where?

   myQuickSort [] = []
   myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
       where
       smaller = filter (<x) xs
       bigger =  filter (>=x) xs
A: 

qs.hs:

import Data.List
myQuickSort [] = []
myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
      where smaller  = filter ( < x ) xs
            bigger   = filter ( >=x ) xs

GHCI:

GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> :l qs.hs 
[1 of 1] Compiling Main             ( qs.hs, interpreted )
Ok, modules loaded: Main.
*Main> myQuickSort [9,8,7,6]
[6,7,8,9]

What version of GHCi are you using?

You said (suggested) that your program is only those 5 lines, but the error is on line 61. Moreover, it's at column 14, and column 14 does not have an = in those 5 lines you gave.

The program also loads fine for me if the where is on its own line, or if it's on the line above, for instance.

Mark Rushakoff
Mark, I did not paste my whole program so the erro number mismatch with the source code. And I found my real problem is the error in indentation. I have rewritten my question.
pierr
+6  A: 

maybe it's an issue of tabs. tabs may look the same as a certain number of spaces, but Haskell won't think they are the same

newacct
+3  A: 

Your problem has to do with the expansion of tabs. Haskell assumes a tab is worth 8 spaces. Your editor likely has a different assumption. Try searching and replacing all tabs with 8 space in your editor, then adjust the spacing to line up the where clause.

Edward Kmett
Or, in Vim: `:set et` and `:retab` to expand tabs to spaces at the current setting, then `:set ts=8` to ensure that tabs are 8 spaces in the future.
ephemient