squirrels
squirrels

Reputation: 313

If syntax in where blocks

Can I use if syntax in a where block? In the following, the if in the second where block where second is defined yields a parse error and I can't figure it out.

data Label = A | B | C deriving (Show)
type Path = ([(Label, Int)],Int)
data Section = Section { getA :: Int, getB :: Int, getC :: Int } deriving (Show)

getPath :: Section -> (Path, Path)  -> (Path, Path)
getPath () (Section a b c) = go
    where 
        go ((toppath,top),(bottompath,bottom))
            |top<c+bottom = ((A,a):toppath,top+a, second)
            |otherwise = ((A,a):(C,c):bottompath,bottom+c+a, second)
                where second =
                    if (bottom<c+top)
                        then ((B,b):bottompath,bottom+a)
                        else ((A,b):(C:c):toppath,top+c+a)

Upvotes: 1

Views: 93

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476547

You can, but you should indent the if in the scope of the where, so:

getPath :: Section -> (Path, Path)  -> (Path, Path)
getPath () (Section a b c) = go
    where 
        go ((toppath,top),(bottompath,bottom))
            |top<c+bottom = ((A,a):toppath,top+a, second)
            |otherwise = ((A,a):(C,c):bottompath,bottom+c+a, second)
                where second =
                       if (bottom<c+top)  -- ← extra indentation
                        then ((B,b):bottompath,bottom+a)
                        else ((A,b):(C:c):toppath,top+c+a)

There are however some type errors, for example:

  • () is the data constructor of the unit type, but not a Section, so you should remove () in getPath () (Section a b c) = …;
  • you construct 3-tuples with ((A,a):toppath, top+a, second); and
  • the C in in (C : c) is a Label, not a Path, so that will also error.

You will thus need to fix some problems in the code.

Upvotes: 1

Related Questions