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. Building a tree (Norbert Melzer)
2. Re: development workflow ? (Daniel Trstenjak)
3. Re: Building a tree (Lyndon Maydwell)
4. Type constructors sharing a common field (Lorenzo Tabacchini)
5. Re: Type constructors sharing a common field (Daniel Trstenjak)
6. Re: Type constructors sharing a common field (Benjamin Edwards)
7. Re: Type constructors sharing a common field (Daniel Trstenjak)
----------------------------------------------------------------------
Message: 1
Date: Mon, 28 Apr 2014 17:49:28 +0200
From: Norbert Melzer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Building a tree
Message-ID:
<CA+bCVssoZHs0YHuG_Bu6pxb-WaGQDy=ps2bx8n6a0xoun7q...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi!
I have to implement a solver for the game 2048 and decided to use Haskell
from the pool of possible languages.
My current implementation has a fixed recursion depth of 5 and simply sums
up the possible score the best line of moves would give. One problem with
this implementation is, that in some cases obvious defends are favoured
because of the points that the dead end grants in the short term.
So I now want to build up a tree of moves and pick that move that has the
deepest subtree.
My problem now is, that I haven't worked with trees before, so I don't have
any idea how to build it up recursively. Also, since I don't consider new
stones that would spawn after doing my move, I might create infinite
subterranean just swiping left and right or up and down or anything else,
how would I be able to consider such subtrees as a game over on the first
repetition?
TIA
Norbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140428/d792d53b/attachment-0001.html>
------------------------------
Message: 2
Date: Mon, 28 Apr 2014 17:59:50 +0200
From: Daniel Trstenjak <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] development workflow ?
Message-ID: <20140428155950.GA13973@machine>
Content-Type: text/plain; charset=us-ascii
On Sun, Apr 27, 2014 at 10:24:38PM -0500, John M. Dlugosz wrote:
> OK, use sandbox and "add-source".
> But does that address the smart "make" concept? That is, will it
> automatically rebuild the gloss package if a file in it changed,
> when I compile the program that imports it?
Yes.
------------------------------
Message: 3
Date: Tue, 29 Apr 2014 11:09:07 +1000
From: Lyndon Maydwell <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Building a tree
Message-ID:
<CAM5QZtx=j1a+k14etk4ktphkxaphyt9cg5fgvvacjtiz7q6...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
There are some nice "unfold" style methods available in Data.Tree if
you're looking for a library to help out. I've gotten into the habit
of generating all possibilities lazily and then pruning rather than
doing the pruning during generation, so unfold works well with this.
Is that the kind of thing you're after?
On Tue, Apr 29, 2014 at 1:49 AM, Norbert Melzer <[email protected]> wrote:
> Hi!
>
> I have to implement a solver for the game 2048 and decided to use Haskell
> from the pool of possible languages.
>
> My current implementation has a fixed recursion depth of 5 and simply sums
> up the possible score the best line of moves would give. One problem with
> this implementation is, that in some cases obvious defends are favoured
> because of the points that the dead end grants in the short term.
>
> So I now want to build up a tree of moves and pick that move that has the
> deepest subtree.
>
> My problem now is, that I haven't worked with trees before, so I don't have
> any idea how to build it up recursively. Also, since I don't consider new
> stones that would spawn after doing my move, I might create infinite
> subterranean just swiping left and right or up and down or anything else,
> how would I be able to consider such subtrees as a game over on the first
> repetition?
>
> TIA
> Norbert
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 4
Date: Mon, 28 Apr 2014 20:26:21 +0200
From: Lorenzo Tabacchini <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Type constructors sharing a common field
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
Imagine the following data type:
data Person = Child { childAge :: Int, school :: School }
| Adult { adultAge :: Int, job :: Job }
Both the alternatives share an "age" field, but in order to access it we
are obliged to match all the constructors:
personAge :: Person -> Int
personAge (Child {childAge = age}) = age
personAge (Adult {adultAge = age}) = age
Is there a way to define a common field in a data type (somehow like
inheritance in the OOP world)?
It would be practical if we could define "age :: Int" as a common field
and do something like:
personAge (_ {age = age}) = age
An alternative solution could be extracting the varying fields in a
different type:
data WorkingPerson = Child School | Adult Job
data Person = Person { age :: Int, workingPerson :: WorkingPerson }
Or to make Person a type class (which would probably require existential
types in order to be useful).
But the first one looks simpler and more natural.
Does this feature exist (maybe in other forms) and, if not, is there a
reason?
Would it make sense to have a GHC extension (or even to make a Template
Haskell hack) for this feature?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140428/01373aa9/attachment-0001.html>
------------------------------
Message: 5
Date: Tue, 29 Apr 2014 12:13:04 +0200
From: Daniel Trstenjak <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Type constructors sharing a common
field
Message-ID: <20140429101304.GA8577@machine>
Content-Type: text/plain; charset=us-ascii
Hi Lorenzo,
I don't think that the first one is more natural, because you could define
a child like: Child { childAge = 99, ... }, but you also might have
someone quite old still studying, so I might go with something like:
data Occupation = Student School
| Employee Job
data Person = Person { age :: Int, occupation :: Occupation }
But if you really want the first one, then you're most likely only one
"lens"[1] away from a solution:
age :: Lens' Person Int
age = lens getAge setAge
where
getAge Child { childAge = age } = age
getAge Adult { adultAge = age } = age
setAge (c@Child {}) newAge = c { childAge = newAge }
setAge (a@Adult {}) newAge = a { adultAge = newAge }
To get the age of a person you could now write:
person ^. age
And to modify it:
person & age .~ 22
But I wouldn't prefer the lens solution.
Greetings,
Daniel
[1] https://hackage.haskell.org/package/lens
------------------------------
Message: 6
Date: Tue, 29 Apr 2014 10:45:00 +0000
From: Benjamin Edwards <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Type constructors sharing a common
field
Message-ID:
<can6k4niercosbedoawz7cuz2gepse6fnva2ovcxzd0yxwmf...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
In addition to the comments made by Daniel, as a general rule, you don?t
want a sum type with selectors functions as your example stands, because
you can end up with compiling programs like this:
module Main where
main :: IO ()main =
let p = Child 10
age = adultAge p
in print age
this craps out with an error at runtime.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140429/83a79eb8/attachment-0001.html>
------------------------------
Message: 7
Date: Tue, 29 Apr 2014 13:57:18 +0200
From: Daniel Trstenjak <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Type constructors sharing a common
field
Message-ID:
<CAPZ0SW4a_NknWjt7SZnB+e+5z1HBZJ48GEVizyv773C0E4a=t...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
> this craps out with an error at runtime.
If you're trying to promote Haskell as a safe language, then this behaviour
is always a bit humbling.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140429/49e8ee6f/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 70, Issue 51
*****************************************