[Haskell-beginners] Dipping Toes Into Haskell

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Thu Mar 12 23:50:16 UTC 2015


data Piece = X | O

means that X and O are constants, having data type 'Piece'.
In other words, you directly use X and O instead of Piece X and Piece O.

Piece is a type, and X and O are constructors (or simply the two possible
values).

A good similar example is the Bool type,

    data Bool = False | True
    -- Two possible values, i.e. False and True

For more info, take a look here:
http://en.wikibooks.org/wiki/Haskell/Type_declarations

Also, to make life easier, you might want to use:

    data Piece = X | O
      deriving Show -- Make this type showable

With this, you will be able to use 'print' with elements of this datatype.

For a tutorial on setting up emacs, take a look here:
https://github.com/serras/emacs-haskell-tutorial/blob/master/tutorial.md

For updating nested lists, you have to create a function that takes two
numbers (the positions) and iterates over the whole structure, just
updating the required position.
While this may seem like an overkill, it gets optimized by ghc.

I don't have any experience with lenses, but they should be usable here.
Understanding them will require a good understanding of the type system.

On 13 March 2015 at 04:32, 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
>
>


-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150313/d2e40668/attachment.html>


More information about the Beginners mailing list