[Haskell-beginners] Guards problem

Stijn Muylle stijnmuylle at gmail.com
Thu Apr 10 12:20:17 UTC 2014


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 <stijnmuylle at gmail.com> wrote:

>
> 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/037704f8/attachment.html>


More information about the Beginners mailing list