0

My code:

addConcat :: [Int] -> [Int]
addConcat [x,y] = z:(z + y) where (z = x + y)

I'm implementing a function not exactly the one above but it's of the same format and I always get:

Syntax error in input (unexpected symbol "y")

So what is wrong with my Haskell code? I really need to use the 'where' clause but I think I'm doing something wrong.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
thelili018
  • 43
  • 1
  • 6

1 Answers1

3

I cannot reproduce the error you claim you get. If you are writing that code into a file and compiling it with ghc the error is:

<filename>.hs:2:38: parse error on input ‘=’

And the problem is that the syntax for where is wrong. Either write:

an_expression where z = x+y

Or you have to use curly braces:

an_expression where {z=x+y;}

You cannot use parenthesis to group an assignment.

Note that when writing in ghci you must group together the declarations and use a let to define functions:

Prelude> let {addConcat :: [Int] -> [Int]; addConcat [x,y] = [z,z+y] where z=x+y;}
Prelude> addConcat [1,2]
[3,5]

Note also that even fixing this your function still has a type error because the second argument of : must be a list while z+y is a number. You want z:[z+y] or more simply [z, z+y].

Bakuriu
  • 98,325
  • 22
  • 197
  • 231