AleA
AleA

Reputation: 17

haskell parse error incorrect identation?

I'm trying to write a program that accepts in input a matrix made by list of list such as [[1,2],[3,4],[5,6]] and gives as output its transpose [[1,3,5],[2,4,6]].

This version works but it gives an error because it reaches the end of the list and doesn't stop.

transpose xxs = map head xxs : transpose (map tail xxs)

So I tried this one

transpose xxs = if ((length xxs)>0) then 
            map head xxs : transpose (map tail xxs)

But I get the error

parse error (possibly incorrect indentation or mismatched brackets)

So I tried writing the code without brackets, all in one line, without indentation but I didn't manage to solve this error.

I'm starting to suspect it's not a problem of brackets or spaces.

Upvotes: 0

Views: 60

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233150

I can't begin to guess why the compiler gives you that error message, but in Haskell, all if/then expressions need an else case as well:

transpose xxs =
  if (length xxs) > 0
  then map head xxs : prova (map tail xxs)
  else [[]]

Here I just put [[]] in the else branch, as that was the simplest thing I could think of to satisfy the compiler.

Upvotes: 1

Related Questions