
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

On Thu, Apr 10, 2014 at 9:48 AM, Stijn Muylle
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?
I might be looking at it wrong, but won't your code go through 1. -1(x) >= 3(length b) ---> not true, so check next guard 2. 0(y) >= 3(length $ b!! 0) ---> not true, so check next guard 3. True(otherwise) ---> true /M

Side note: it's not very clear what you're trying to accomplish, but you may be better served by using array semantics. I sketched a quick and dirty code snippet in a hurry, feel free to clean it up and make it well-typed: https://gist.github.com/dserban/10370136 After loading it in GHCi, you can do: λ: getCellAt (1,1) True λ: getCellAt (-1,1) False

Found the problem: I had a bug in my implementation (see Marcus' comment).
The solution provided by Dan (using a 2d array) seems better, but right now
I'm just focussing on making it work ;)
On Thu, Apr 10, 2014 at 9:48 AM, Stijn Muylle
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

On Thu, Apr 10, 2014 at 02:20:17PM +0200, Stijn Muylle wrote:
Found the problem: I had a bug in my implementation (see Marcus' comment).
I think that should be "Magnus' comment" ;) /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus@therning.org jabber: magnus@therning.org twitter: magthe http://therning.org/magnus Heuristic is an algorithm in a clown suit. It’s less predictable, it’s more fun, and it comes without a 30-day, money-back guarantee. -- Steve McConnell, Code Complete
participants (3)
-
Dan Serban
-
Magnus Therning
-
Stijn Muylle