0

Here's my code:

main = do
   contents <- getContents 
   let threes = groupsOf 3 (map read $ lines contents)
      where groupsOf 0 _ = []
            groupsOf _ [] = []
            groupsOf n xs = take n xs : groupsOf n (drop n xs)
    putStrLn $ show threes

When I run this while piping a text file into the input I get:

test.hs:4:13: parse error on input `groupsOf'

Not sure what I'm doing wrong here. From what I can tell my syntax is correct...

dopatraman
  • 13,416
  • 29
  • 90
  • 154
  • Possible duplicate of [Haskell where clause syntax inside a do block](http://stackoverflow.com/questions/14092801/haskell-where-clause-syntax-inside-a-do-block) – behzad.nouri Feb 22 '16 at 03:28

1 Answers1

2

You presented lots of syntax issues.

contents = <- getContents

That is invalid and should be contents <- getContents.

let threes = groupsOf 3 (map read $ lines contents)
   where groupsOf 0 _ = []

You can't have where after a let clause without further indentation. You could either move the where clause to after the function,\ declare groupsOf in a let clause, or indent the where a little past the indentation of the variables in the let clause:

let threes = groupsOf 3 (map read $ lines contents)
    groupsOf 0 _ = []
    groupsOf _ [] = []
    groupsOf n xs = take n xs : groupsOf n (drop n xs)

Edit: And after referring to the Haskell 2010 report, I don't think let { decls } where {decls} is actually valid Haskell. GHC parses the expressions, which I think is legitimate and nice in some situations although bad style here.

Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166