1
votes

I've just started working on my third school assignment and i've run into yet another noob error i can't seem to solve on my own.

We're to write a Sudoku solver, and i'm currently writing a function which will determine wether the elements in my Sudoku are of the right type. They're of the type Maybe Int (Just Int or Nothing).

Here are the relevant sections of code:

data Sudoku = Sudoku [[Maybe Int]]
  deriving (Eq, Show)

validValue :: Maybe Int -> Bool
validValue Nothing = True
validValue (Just n) = True 

checkEveryElement :: Sudoku -> Bool
checkEveryElement (Sudoku (x:xs))
    | and $ map $ validValue $ concat (x:xs) == True

The Sudoku itself is represented by a list of 9 elements, where each element is a list which consists of 9 elements themselves. So x in the above list (the head of the total list) is actually a list of 9 elements.

I've only started learning how to program these past five weeks, so bear with me. :) I am not sure i have used and correctly. The error i get while compiling is at the last line of the above sections of code.

Thanks!

edit: I forgot the actual error...'Possibly incorrect indentation or mismatched brackets.'

1
In guard expressions you specify the result with a single equals sign, not double. That's the reason for that specific error at least, ghc just sees the comparison and wonders what the result should be :)ollanta
i think you need a (): (and $ map $ validValue $ concat (x:xs)) == TrueMcBear Holden
To expand on @ollanta's comment, you don't strictly need the == True: the 'and' will already evaluate to a Bool, so there is no need to test equality with True. One solution is to change the == to =, but if you want to be painfully explicit about the guard, you could write == True = True.crockeea
validValue currently can't return False. I think you probably meant validValue (Just n) = {- some expression I could guess but you can figure out evaluating to True if n is valid and to False otherwise -}dfeuer

1 Answers

4
votes

You need an equals sign there, not compares_equal sign. You also have an extra $ in your guard expression. It should be

checkEveryElement :: Sudoku -> Bool
checkEveryElement (Sudoku (x:xs))
--  | and $ map $ validValue $ concat (x:xs) == True
    | and $ map   validValue $ concat (x:xs) =  True
--             ^^^                           ^^^

but it will always be True, according to your current definitions. With the == sign it is all part of the guard, the function's body is missing.

And the idiomatic way of writing the above is just

checkEveryElement (Sudoku (x:xs)) = 
  -- and $ map validValue $ concat (x:xs)    -- or, 
     all validValue       $ concat (x:xs)    

without the guard.