Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: newtype with a typclass (R?zvan Rotaru)
2. Re: newtype with a typclass (Emmanuel Touzery)
3. Re: A good data structure for representing a tic-tac-toe
board? (Peter Hall)
4. Re: A good data structure for representing a tic-tac-toe
board? (Brent Yorgey)
5. Re: A good data structure for representing a tic-tac-toe
board? (Lyndon Maydwell)
----------------------------------------------------------------------
Message: 1
Date: Mon, 18 Mar 2013 20:10:19 +0200
From: R?zvan Rotaru <[email protected]>
Subject: Re: [Haskell-beginners] newtype with a typclass
To: [email protected]
Message-ID:
<cap33cnbopg0tn7_36rc9-keeupj43yy6eg+yf7r3hou8xe9...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On 18 March 2013 18:34, <[email protected]> wrote:
>
> Not directly, no. It is possible to use existentials to do this ? at the
> cost that you lose the ability to do anything but what you have declared
> about it. For example, if you were to use this to make a map of Show-able
> values, the *only* thing you can do with the value is invoke "show" on it
> (in particular: you cannot pull out the value directly, because the only
> thing you know about it is that "show" works on it; you otherwise have no
> idea what it is!). And with such a value, referential transparency means
> there is no difference between this and storing the *result* of invoking
> "show" on it (that is, a String) instead, and laziness means that it may
> well actually store a thunk that will invoke "show" when the value is
> needed instead of computing and storing the value, so that there is in fact
> no difference between the two except that the type is much more complicated
> and both the type and the code are correspondingly more difficult to
> understand.
>
> What problem are you trying to solve by obfuscating things in this manner?
>
> Also note that most times when someone is trying to do this, they have
> confused typeclasses with OOP; they are not, and trying to treat them as
> such *will* lead you into dead ends.
>
> --
> brandon s allbery kf8nh sine nomine
> associates
> [email protected]
> [email protected]
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
Well, I need something similar to Java interfaces. Or at least this is my
current way of thinking about the problem. I am trying to build some types
for GUI Widgets, and I would like to use Maps to keep their properties.
It's meant to replace the record syntax, which I find unusable because of
the name clash. I also would like to have fast non-destructive update of
this type, by using structural sharing, and Maps as good for this.
So, for example a Button would have an id, a label and function for the
onclick event, and these three values should be kept in a map, with the
keys "id", "label" and "onclick".
It's true that keeping Show-able values is not enough, since I won't be
able to call the onclick function. Unfortunately, I have no idea how to do
this as well.
Razvan
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130318/3da09c61/attachment-0001.htm>
------------------------------
Message: 2
Date: Mon, 18 Mar 2013 19:18:46 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] newtype with a typclass
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<CAC42Re=qre0wto6w3pzdrnppwdmkrjqqgvzzxypygbkn7-j...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Maybe this article can interest you:
http://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/
I think a record syntax is probably the best match, the biggest mismatch
between your approach and the haskell way might be the haskell preference
for immutability.
Maybe you should check how such things are implemented in existing gui
libraries in haskell.
emmanuel
On 18 Mar 2013 19:12, "R?zvan Rotaru" <[email protected]> wrote:
>
>
> On 18 March 2013 18:34, <[email protected]> wrote:
>
>>
>> Not directly, no. It is possible to use existentials to do this ? at the
>> cost that you lose the ability to do anything but what you have declared
>> about it. For example, if you were to use this to make a map of Show-able
>> values, the *only* thing you can do with the value is invoke "show" on it
>> (in particular: you cannot pull out the value directly, because the only
>> thing you know about it is that "show" works on it; you otherwise have no
>> idea what it is!). And with such a value, referential transparency means
>> there is no difference between this and storing the *result* of invoking
>> "show" on it (that is, a String) instead, and laziness means that it may
>> well actually store a thunk that will invoke "show" when the value is
>> needed instead of computing and storing the value, so that there is in
>> fact
>> no difference between the two except that the type is much more
>> complicated
>> and both the type and the code are correspondingly more difficult to
>> understand.
>>
>> What problem are you trying to solve by obfuscating things in this manner?
>>
>> Also note that most times when someone is trying to do this, they have
>> confused typeclasses with OOP; they are not, and trying to treat them as
>> such *will* lead you into dead ends.
>>
>> --
>> brandon s allbery kf8nh sine nomine
>> associates
>> [email protected]
>> [email protected]
>> unix, openafs, kerberos, infrastructure, xmonad
>> http://sinenomine.net
>>
>
> Well, I need something similar to Java interfaces. Or at least this is my
> current way of thinking about the problem. I am trying to build some types
> for GUI Widgets, and I would like to use Maps to keep their properties.
> It's meant to replace the record syntax, which I find unusable because of
> the name clash. I also would like to have fast non-destructive update of
> this type, by using structural sharing, and Maps as good for this.
>
> So, for example a Button would have an id, a label and function for the
> onclick event, and these three values should be kept in a map, with the
> keys "id", "label" and "onclick".
>
> It's true that keeping Show-able values is not enough, since I won't be
> able to call the onclick function. Unfortunately, I have no idea how to do
> this as well.
>
> Razvan
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130318/d9475767/attachment-0001.htm>
------------------------------
Message: 3
Date: Mon, 18 Mar 2013 19:49:25 +0000
From: Peter Hall <[email protected]>
Subject: Re: [Haskell-beginners] A good data structure for
representing a tic-tac-toe board?
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<caa6hak7kcoxanvx3+v-1+y1f-xouru7fewqozq1muuhquz-...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Start with a data type for the cell values, instead of Char. Then use an
Array of Arrays, containing those values.
data Cell = Empty | O | X
type Board = Array Int Cell
Finding winning "rows" and "columns" is easy. Diagonals are slightly more
complicated.
Peter
On 18 March 2013 15:54, Costello, Roger L. <[email protected]> wrote:
> Hi Folks,
>
> Currently I am representing a tic-tac-toe board as a string, with 'X'
> denoting player 1 and 'O' denoting player 2. For example, I represent this
> 2x2 game board:
>
> 'X' |
> -----------------------
> | 'O'
>
> with this string: "X O"
>
> The nice thing about that representation is that it is each to identify
> which cells are filled or empty, and it is easy to mark a cell with an 'X'
> or 'O'.
>
> The problem with the representation is that it is difficult to determine
> when a player has won.
>
> Can you recommend a representation that makes it easy to:
>
> 1. determine when a player has won
> 2. identify cells that are filled or empty
> 3. mark an empty cell
>
> /Roger
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130318/5023767b/attachment-0001.htm>
------------------------------
Message: 4
Date: Mon, 18 Mar 2013 16:59:55 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] A good data structure for
representing a tic-tac-toe board?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Consider the following game:
Two players take turns choosing a number from 1-9 (inclusive). Once a
number has been chosen it cannot be chosen again. The winner is the
first player to have three of their selected numbers which sum
to 15.
Surprisingly, this game turns out to be isomorphic to tic-tac-toe,
which can be seen by arranging the numbers from 1-9 in a magic square,
such as
8 3 4
1 5 9
6 7 2
It is not hard to check that three distinct numbers from 1-9 sum to 15
if and only if they correspond to a tic-tac-toe win on the board
above. This means you can represent the state of the board as a pair
of sets. To check the state of a cell you look up the corresponding
number from the board above in both sets. To mark an empty cell you
add the corresponding number to one of the sets. To check whether a
player has won, you list all size-3 subsets of their set and check
whether any of them sum to 15.
You may or may not agree that this fulfills your criteria, but at
least it is cute.
-Brent
On Mon, Mar 18, 2013 at 03:54:58PM +0000, Costello, Roger L. wrote:
> Hi Folks,
>
> Currently I am representing a tic-tac-toe board as a string, with 'X'
> denoting player 1 and 'O' denoting player 2. For example, I represent this
> 2x2 game board:
>
> 'X' |
> -----------------------
> | 'O'
>
> with this string: "X O"
>
> The nice thing about that representation is that it is each to identify which
> cells are filled or empty, and it is easy to mark a cell with an 'X' or 'O'.
>
> The problem with the representation is that it is difficult to determine when
> a player has won.
>
> Can you recommend a representation that makes it easy to:
>
> 1. determine when a player has won
> 2. identify cells that are filled or empty
> 3. mark an empty cell
>
> /Roger
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 5
Date: Tue, 19 Mar 2013 11:16:40 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] A good data structure for
representing a tic-tac-toe board?
To: [email protected], The Haskell-Beginners Mailing List -
Discussion of primarily beginner-level topics related to Haskell
<[email protected]>
Message-ID:
<cam5qztzcci2wh+w-y8szfnxr1xu4qjo1zzfcsuv6mikskh_...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
If taking the array approach, I'd recommend using a single array indexed by
the (x,y) position of the cell, this way neither direction has a greater
implied significance. Diagonals should also be easier.
Aside: Tony Morris wrote a very interesting exercise based on tic-tac-toe
and it is available on Hackage: http://hackage.haskell.org/package/TicTacToe
On Tue, Mar 19, 2013 at 3:49 AM, Peter Hall <[email protected]>wrote:
> Start with a data type for the cell values, instead of Char. Then use an
> Array of Arrays, containing those values.
>
> data Cell = Empty | O | X
> type Board = Array Int Cell
>
> Finding winning "rows" and "columns" is easy. Diagonals are slightly more
> complicated.
>
> Peter
>
>
>
> On 18 March 2013 15:54, Costello, Roger L. <[email protected]> wrote:
>
>> Hi Folks,
>>
>> Currently I am representing a tic-tac-toe board as a string, with 'X'
>> denoting player 1 and 'O' denoting player 2. For example, I represent this
>> 2x2 game board:
>>
>> 'X' |
>> -----------------------
>> | 'O'
>>
>> with this string: "X O"
>>
>> The nice thing about that representation is that it is each to identify
>> which cells are filled or empty, and it is easy to mark a cell with an 'X'
>> or 'O'.
>>
>> The problem with the representation is that it is difficult to determine
>> when a player has won.
>>
>> Can you recommend a representation that makes it easy to:
>>
>> 1. determine when a player has won
>> 2. identify cells that are filled or empty
>> 3. mark an empty cell
>>
>> /Roger
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130319/5ae4c620/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 57, Issue 27
*****************************************