[Haskell-beginners] Dipping Toes Into Haskell

Kim-Ee Yeoh ky3 at atamo.com
Thu Mar 12 23:53:55 UTC 2015


Hi Tim,

Congrats on your choice of tic-tac-toe. I also believe it's a good toy to
get your feet wet with haskell.

1. Your concerns about writing a "custom datatype" are best addressed by
reading up on it. If you search for "haskell data type declarations",
you'll find good material explaining the difference between "data",
"newtype", and "type" declarations. You'll also understand the meaning of
the error message and what "data constructor" refers to.

2. Functions defined on lists are here:

https://hackage.haskell.org/package/base/docs/Data-List.html

The update function you're looking for is (!!).




-- Kim-Ee

On Fri, Mar 13, 2015 at 6:02 AM, Timothy Washington <twashing at gmail.com>
wrote:

> To get started, I'm trying to implement a simple *tictactoe* game. And I
> would like to be able to represent a Piece on the board, as either the
> string "X" or "O". This is what I have so far.
>
> module Main where
>
> data Piece = X | O
> type Row = [Piece]
> type Board = [Row]
>
> -- put an X or O in a position
> move :: Board -> Piece -> Board
> move board piece = board
>
> -- check win vertically
> -- check win horizontally
> -- check win diagonally
>
> main :: IO ()
> main = putStrLn "Hello World"
>
>
>
> *A)* Now, I'd like to be able to *load code interactively*, preferably
> within emacs. However I don't have access to my types with *ghci* or *ghc-mod
> (Interactive-Haskell)*. In either case, this call fails with the below
> error.
>
>
> let p = Piece X
> <interactive>:20:9-13: Not in scope: data constructor `Piece'
>
>
> *B)* And how do I make a *custom datatype* that's one of two strings
> (enumeration of either "X" or "O"). Cabal builds and runs the abouve code,
> so I know it can compile. But I'm confused as to where X or O is defined,
> and how I would supply it as an input.
>
> *C)* Finally, how do we update nested lists in Haskell. I want the move
> function to take a Board, Piece, and Position, and return a Board. I see
> some results from Hoogle <https://www.haskell.org/hoogle/?hoogle=update>.
> Is this where Lenses <https://www.haskell.org/hoogle/?hoogle=lens> or
> Zippers <https://www.haskell.org/hoogle/?hoogle=zipper> come into play?
>
>
> Thanks
>
> Tim Washington
> Interruptsoftware.com <http://interruptsoftware.com>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150313/1b836cfb/attachment.html>


More information about the Beginners mailing list