[Haskell-beginners] Guards problem

Stijn Muylle stijnmuylle at gmail.com
Thu Apr 10 07:48:44 UTC 2014


All,

I ran into a small problem using guards, and I don't really understand what
I'm doing wrong.

I have this code:

----------------------------------
import Data.List.Split
type Cell = Bool
data Board = Board [[Cell]] deriving Show

trueList = True : trueList

createBoard :: Int -> Int -> Board
createBoard x y = Board (chunksOf x (take (x * y) trueList))

getCellAt :: Int -> Int -> Board -> Cell
getCellAt x y (Board b)
        | x >= (length b) =  False
        | y >= (length (b !! 0)) =  False
        | otherwise     =  (b !! x) !! y

-----------------------------------

When I try to execute this:

getCellAt (-1) 0 $ createBoard 3 3

I get a negative index exception. However, as -1 is smaller than 3 (length
of the board), I would expect False to be returned.

Am I overlooking something?

Thanks!
Stijn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140410/627b4dda/attachment.html>


More information about the Beginners mailing list