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: Translating imperative algorithms to Haskell
(Stephen Blackheath [to Haskell-Beginners])
2. Re: Translating imperative algorithms to Haskell
(Stephen Blackheath [to Haskell-Beginners])
3. Re: Translating imperative algorithms to Haskell (Stephen Tetley)
4. Re: list of integers to list of nats (Christian Maeder)
5. Call for papers: MULTICONF-10, Orlando, USA, July 2010
(John Edward)
----------------------------------------------------------------------
Message: 1
Date: Mon, 22 Feb 2010 08:10:37 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
<[email protected]>
Subject: Re: [Haskell-beginners] Translating imperative algorithms to
Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Stephen, Mikhail & all,
I'll have a go...
Wikipedia's imperative pseudo-code:
function negascout(node, depth, α, β)
if node is a terminal node or depth = 0
return the heuristic value of node
b := β (* initial window is
(-β, -α) *)
foreach child of node
a := -negascout (child, depth-1, -b, -α)
if a>α
α := a
if αâ¥Î²
return α (* Beta cut-off *)
if αâ¥b (* check if
null-window failed high*)
α := -negascout(child, depth-1, -β, -α) (* full re-search *)
if αâ¥Î²
return α (* Beta cut-off *)
b := α+1 (* set new null
window *)
return α
My attempt to render into Haskell without ST monad and without
attempting to understand the algorithm at all.
Version 1 with lets
negascout :: Node -> Int -> Int -> Int -> Int
negascout node depth _ _ | terminal node || depth == 0 = heuristic node
negascout node depth alpha beta =
ns (children node) alpha beta beta -- initial window is (-beta,
-alpha)
where
ns (ch:chs) alpha beta b =
let alpha' = alpha `max` negascout ch (depth-1) (-b) (-alpha)
in if alpha' >= beta then alpha' else -- beta
cut-off
if alpha' >= b then -- full
re-search
let alpha'' = -negascout ch (depth-1) (-beta) (-alpha)
in if alpha'' >= beta then alpha'' -- beta cut-off
else ns chs alpha'' beta (alpha''+1) -- new window
else ns chs alpha' beta (alpha'+1) -- new window
ns [] alpha _ _ = alpha
Version 2 with cases
negascout :: Node -> Int -> Int -> Int -> Int
negascout node depth _ _ | terminal node || depth == 0 = heuristic node
negascout node depth alpha beta =
ns (children node) alpha beta -- initial window is (-beta, -alpha)
where
ns (ch:chs) alpha b =
case alpha `max` negascout ch (depth-1) (-b) (-alpha) of
alpha' | alpha' >= beta -> alpha' -- beta
cut-off
alpha' | alpha' >= b -> -- full
re-search
case -negascout ch (depth-1) (-beta) (-alpha) of
alpha'' | alpha'' >= beta -> alpha'' -- beta
cut-off
alpha'' -> ns chs alpha'' (alpha''+1) -- new window
alpha' -> ns chs alpha' (alpha'+1) -- new window
ns [] alpha _ = alpha
I think with case, it's slightly more readable. Marginally more verbose
than the imperative version, because Haskell makes you do your state
keeping more explicitly. Personally I find the Haskell easier to read.
When I read the imperative version, it takes work to assemble in my
head what is written out explicitly in the Haskell, but maybe that's
just me.
I certainly don't think the Haskell version is any more complex. I
think there are cases where mutability is so important to an algorithm
that Haskell struggles (at least in terms of performance), but I don't
think this is one of those cases.
Just for fun here's another version where I'm breaking it up into three
parts:
data Next a = Cont a | Break a
breakableFoldl :: (a -> b -> Next a) -> a -> [b] -> a
breakableFoldl body state xs = loop state xs
where
loop state [] = state
loop state (x:xs) = case body state x of
Cont state' -> loop state' xs
Break state' -> state'
negascout :: Node -> Int -> Int -> Int -> Int
negascout node depth _ _ | terminal node || depth == 0 = heuristic node
negascout node depth alpha0 beta = alphaOut
where
(alphaOut, _) = breakableFoldl (\(alpha, b) ch ->
(alpha `max` negascout ch (depth-1) (-b) (-alpha))
`betaCutoffOr` \alpha' ->
if alpha' >= b then -- full re-search
(-negascout ch (depth-1) (-beta) (-alpha))
`betaCutoffOr` \alpha'' -> Cont (alpha'',
alpha''+1)
else
Cont (alpha', alpha'+1)
) (alpha0, beta) (children node) -- initial window is
(-beta, -alpha)
-- Break out if the input alpha value hits the beta cutoff,
otherwise pass
-- it to a non-cutoff case.
betaCutoffOr :: Int -> (Int -> Next (Int,Int)) -> Next (Int,Int)
betaCutoffOr alpha _ | alpha >= beta = Break (alpha, undefined)
betaCutoffOr alpha nonCutoffCase = nonCutoffCase alpha
This is a higher level of abstraction, which communicates intent fairly
clearly, and shows how easily you can abstract common patterns out in
Haskell. An improvement in this case? I think so, but there are
arguments against that.
Here's the missing code that makes each version above typecheck:
data Node = Node {
heuristic :: Int,
children :: [Node]
}
terminal :: Node -> Bool
terminal = undefined
Steve
Stephen Tetley wrote:
> Hello all
>
> How are search trees generated and what is their 'shape' - i.e. leaf
> labelled, node labelled, binary trees or rose trees?
>
> I've a functional reformulation of the Wikipedia algorithm which is
> about the same line count (excepting auxiliaries, which is a bit of a
> cheat), but its producing bad results on a leaf and node labelled rose
> tree.
>
> By the way, the imperative essence of the negascout algorithm and what
> makes it elegant is how it cuts off (control flow), rather than
> statefulness (assignment). Even though the line count is roughly the
> same and I believe I match the traversal behaviour / cut offs, the
> imperative version is simply nicer than my functional version.
>
> Best wishes
>
> Stephen
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 2
Date: Mon, 22 Feb 2010 08:29:13 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
<[email protected]>
Subject: Re: [Haskell-beginners] Translating imperative algorithms to
Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
All,
And... A slight variation that shadows alpha (which some people don't
like, but I think it's a great technique) and thereby avoids the mistake
I made in my previous three versions where I forgot a ' in -negascout ch
(depth-1) (-beta) (-alpha'). (You have to watch that in Haskell.)
data Next a = Cont a | Break a
breakableFoldl :: (a -> b -> Next a) -> a -> [b] -> a
breakableFoldl body state xs = loop state xs
where
loop state [] = state
loop state (x:xs) = case body state x of
Cont state' -> loop state' xs
Break state' -> state'
negascout :: Node -> Int -> Int -> Int -> Int
negascout node depth _ _ | terminal node || depth == 0 = heuristic node
negascout node depth alpha0 beta = alphaOut
where
(alphaOut, _) = breakableFoldl (\(alpha, b) ch ->
(alpha `max` negascout ch (depth-1) (-b) (-alpha))
`betaCutoffOr` \alpha ->
if alpha >= b then -- full re-search
(-negascout ch (depth-1) (-beta) (-alpha))
`betaCutoffOr` \alpha -> Cont (alpha, alpha+1)
else
Cont (alpha, alpha+1)
) (alpha0, beta) (children node) -- initial window is
(-beta, -alpha)
-- Break out if the input alpha value hits the beta cutoff,
otherwise pass
-- it to a non-cutoff case.
betaCutoffOr :: Int -> (Int -> Next (Int,Int)) -> Next (Int,Int)
betaCutoffOr alpha _ | alpha >= beta = Break (alpha, undefined)
betaCutoffOr alpha nonCutoffCase = nonCutoffCase alpha
Steve
Stephen Blackheath [to Haskell-Beginners] wrote:
> Stephen, Mikhail & all,
>
> I'll have a go...
>
> Wikipedia's imperative pseudo-code:
>
> function negascout(node, depth, α, β)
> if node is a terminal node or depth = 0
> return the heuristic value of node
> b := β (* initial window is
> (-β, -α) *)
> foreach child of node
> a := -negascout (child, depth-1, -b, -α)
> if a>α
> α := a
> if αâ¥Î²
> return α (* Beta cut-off *)
> if αâ¥b (* check if
> null-window failed high*)
> α := -negascout(child, depth-1, -β, -α) (* full re-search *)
> if αâ¥Î²
> return α (* Beta cut-off *)
> b := α+1 (* set new null
> window *)
> return α
>
> My attempt to render into Haskell without ST monad and without
> attempting to understand the algorithm at all.
>
> Version 1 with lets
>
> negascout :: Node -> Int -> Int -> Int -> Int
> negascout node depth _ _ | terminal node || depth == 0 = heuristic node
> negascout node depth alpha beta =
> ns (children node) alpha beta beta -- initial window is (-beta,
> -alpha)
> where
> ns (ch:chs) alpha beta b =
> let alpha' = alpha `max` negascout ch (depth-1) (-b) (-alpha)
> in if alpha' >= beta then alpha' else -- beta
> cut-off
> if alpha' >= b then -- full
> re-search
> let alpha'' = -negascout ch (depth-1) (-beta) (-alpha)
> in if alpha'' >= beta then alpha'' -- beta cut-off
> else ns chs alpha'' beta (alpha''+1) -- new window
> else ns chs alpha' beta (alpha'+1) -- new window
> ns [] alpha _ _ = alpha
>
> Version 2 with cases
>
> negascout :: Node -> Int -> Int -> Int -> Int
> negascout node depth _ _ | terminal node || depth == 0 = heuristic node
> negascout node depth alpha beta =
> ns (children node) alpha beta -- initial window is (-beta, -alpha)
> where
> ns (ch:chs) alpha b =
> case alpha `max` negascout ch (depth-1) (-b) (-alpha) of
> alpha' | alpha' >= beta -> alpha' -- beta
> cut-off
> alpha' | alpha' >= b -> -- full
> re-search
> case -negascout ch (depth-1) (-beta) (-alpha) of
> alpha'' | alpha'' >= beta -> alpha'' -- beta
> cut-off
> alpha'' -> ns chs alpha'' (alpha''+1) -- new window
> alpha' -> ns chs alpha' (alpha'+1) -- new window
> ns [] alpha _ = alpha
>
> I think with case, it's slightly more readable. Marginally more verbose
> than the imperative version, because Haskell makes you do your state
> keeping more explicitly. Personally I find the Haskell easier to read.
> When I read the imperative version, it takes work to assemble in my
> head what is written out explicitly in the Haskell, but maybe that's
> just me.
>
> I certainly don't think the Haskell version is any more complex. I
> think there are cases where mutability is so important to an algorithm
> that Haskell struggles (at least in terms of performance), but I don't
> think this is one of those cases.
>
> Just for fun here's another version where I'm breaking it up into three
> parts:
>
>
> data Next a = Cont a | Break a
>
> breakableFoldl :: (a -> b -> Next a) -> a -> [b] -> a
> breakableFoldl body state xs = loop state xs
> where
> loop state [] = state
> loop state (x:xs) = case body state x of
> Cont state' -> loop state' xs
> Break state' -> state'
>
> negascout :: Node -> Int -> Int -> Int -> Int
> negascout node depth _ _ | terminal node || depth == 0 = heuristic node
> negascout node depth alpha0 beta = alphaOut
> where
> (alphaOut, _) = breakableFoldl (\(alpha, b) ch ->
> (alpha `max` negascout ch (depth-1) (-b) (-alpha))
> `betaCutoffOr` \alpha' ->
> if alpha' >= b then -- full re-search
> (-negascout ch (depth-1) (-beta) (-alpha))
> `betaCutoffOr` \alpha'' -> Cont (alpha'',
> alpha''+1)
> else
> Cont (alpha', alpha'+1)
> ) (alpha0, beta) (children node) -- initial window is
> (-beta, -alpha)
>
> -- Break out if the input alpha value hits the beta cutoff,
> otherwise pass
> -- it to a non-cutoff case.
> betaCutoffOr :: Int -> (Int -> Next (Int,Int)) -> Next (Int,Int)
> betaCutoffOr alpha _ | alpha >= beta = Break (alpha, undefined)
> betaCutoffOr alpha nonCutoffCase = nonCutoffCase alpha
>
>
> This is a higher level of abstraction, which communicates intent fairly
> clearly, and shows how easily you can abstract common patterns out in
> Haskell. An improvement in this case? I think so, but there are
> arguments against that.
>
> Here's the missing code that makes each version above typecheck:
>
> data Node = Node {
> heuristic :: Int,
> children :: [Node]
> }
>
> terminal :: Node -> Bool
> terminal = undefined
>
>
> Steve
>
> Stephen Tetley wrote:
>> Hello all
>>
>> How are search trees generated and what is their 'shape' - i.e. leaf
>> labelled, node labelled, binary trees or rose trees?
>>
>> I've a functional reformulation of the Wikipedia algorithm which is
>> about the same line count (excepting auxiliaries, which is a bit of a
>> cheat), but its producing bad results on a leaf and node labelled rose
>> tree.
>>
>> By the way, the imperative essence of the negascout algorithm and what
>> makes it elegant is how it cuts off (control flow), rather than
>> statefulness (assignment). Even though the line count is roughly the
>> same and I believe I match the traversal behaviour / cut offs, the
>> imperative version is simply nicer than my functional version.
>>
>> Best wishes
>>
>> Stephen
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 3
Date: Sun, 21 Feb 2010 20:07:29 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Translating imperative algorithms to
Haskell
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Stephen
I'd be tempted to make the 'fold' problem specific (especially as I'm
not sure I'd call a fold a 'fold' if it breaks early).
Here's what I came up with though it seems to have a bug somewhere.
The Step and Window types might benefit strictness annos in real life.
The imperative version on Wikipedia is far clearer than either mine or
yours though.
Best wishes
Stephen
data Tree a = Node a [Tree a]
| Leaf a
deriving (Eq,Show)
data Step a = NewWindow a a
| BetaCutoff a
data Window a = Win a a
runCutoff :: [(Window a -> Step a)] -> Window a -> a
runCutoff [] (Win alp _) = alp
runCutoff (f:fs) ab = case f ab of
NewWindow a b -> runCutoff fs (Win a b)
BetaCutoff a -> a
cutoff :: a -> (a -> Bool) -> (a -> Step a) -> Step a
cutoff a test elsek | test a = BetaCutoff a
| otherwise = elsek a
negascout :: Tree Int -> Int -> (Window Int) -> Int
negascout (Leaf h) _ _ = h
negascout (Node h _ ) d _ | d <= 0 = h
negascout (Node _ ns) d w@(Win _ b0) = runCutoff (map scout ns) w
where
scout _ (Win a _) | a >= b0 = BetaCutoff a
scout node (Win a b) = let a' = max a (nega node (-b) (-a))
in cutoff a' (>= b0) (\x -> full node (Win x b))
full node (Win a b) = let a' = nega node (-b0) (-a)
in cutoff a' (>= b) (\x -> NewWindow x (x+1))
nega node x y = negate $ negascout node (d-1) (Win x y)
On 21 February 2010 19:29, Stephen Blackheath [to Haskell-Beginners]
<[email protected]> wrote:
> All,
>
> And... A slight variation that shadows alpha (which some people don't
> like, but I think it's a great technique) and thereby avoids the mistake
> I made in my previous three versions where I forgot a ' in -negascout ch
> (depth-1) (-beta) (-alpha'). (You have to watch that in Haskell.)
>
------------------------------
Message: 4
Date: Mon, 22 Feb 2010 11:25:19 +0100
From: Christian Maeder <[email protected]>
Subject: [Haskell-beginners] Re: list of integers to list of nats
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
[email protected] schrieb:
> the function should produce Nothing if there is a negative Int in the input
> list and otherwise Just with the list of the corresponding Nats
>
> mapM looks like the right function for that, so I tried some examples that
> work like I need it. But in case of my exercise:
>
> mapM toEnum [1,2,3,4] :: Nat
>
> doesn't work.
Surely, it does not work, because the type of your first argument
"toEnum" does not match the expected type "Int -> Maybe Nat", as pointed
out below. Furthermore the overall result type is not "Nat" nor "[Nat]",
but "Maybe [Nat]".
Did you have higher order functions (like mapM or map) in your course?
If not, you are supposed to program "mapIntsToNats" using explizit
recursion over lists (and using case-distinctions aka pattern matching
for Lists and the Maybe data type).
Cheers Christian
P.S.
If you're looking for a ready solution, you're (most likely) wrong here.
We only try to help you, to find a solution yourself.
>> mapM :: Monad m => (a -> m b) -> [a] -> m [b]
>>
>> is also mapM :: (Int -> Maybe Nat) -> [Int] -> Maybe [Nat].
>>
>> so mapIntsToNats = mapM f, for some function f :: Int -> Maybe Nat
You should be able to program such a function "f" (using "toEnum" or
programming it from scratch similar to "toEnum").
------------------------------
Message: 5
Date: Mon, 22 Feb 2010 03:40:40 -0800 (PST)
From: John Edward <[email protected]>
Subject: [Haskell-beginners] Call for papers: MULTICONF-10, Orlando,
USA, July 2010
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
It
would be highly appreciated if you could share this announcement with your
colleagues, students and individuals whose research is in computer science,
computer engineering, information science and related areas.
Call
for papers: MULTICONF-10, Orlando, USA, July 2010
The
2010 multi-conference (MULTICONF-10) (website: http://www.PromoteResearch.org )
will
be held during July 12-14, 2010 in Orlando, Florida, USA. The primary goal of
MULTICONF is to promote research and developmental activities in computer
science, information technology, control engineering, and related fields.
Another goal is to promote the dissemination of research to a multidisciplinary
audience and to facilitate communication among researchers, developers,
practitioners in different fields. The following conferences are planned to be
organized as part of MULTICONF-10.
⢠International
Conference on Artificial Intelligence and Pattern Recognition (AIPR-10)
⢠International
Conference on Automation, Robotics and Control Systems (ARCS-10)
⢠International
Conference on Bioinformatics, Computational Biology, Genomics and
Chemoinformatics (BCBGC-10)
⢠International
Conference on Computer Communications and Networks (CCN-10)
⢠International
Conference on Enterprise Information Systems and Web Technologies (EISWT-10)
⢠International
Conference on High Performance Computing Systems (HPCS-10)
⢠International
Conference on Information Security and Privacy (ISP-10)
⢠International
Conference on Image and Video Processing and Computer Vision (IVPCV-10)
⢠International
Conference on Software Engineering Theory and Practice (SETP-10)
⢠International
Conference on Theoretical and Mathematical Foundations of Computer Science
(TMFCS-10)
MULTICONF-10
will be held at Imperial Swan Hotel and Suites. It is a full-service resort
that puts you in the middle of the fun!
Located 1/2 block south of the famed International Drive, the hotel is just
minutes from great entertainment like Walt Disney World® Resort, Universal
Studios and Sea World Orlando. Guests can enjoy free scheduled transportation
to these theme parks, as well as spacious accommodations, outdoor pools and
on-site dining â all situated on 10 tropically landscaped acres. Here, guests
can experience a full-service resort with discount hotel pricing in Orlando.
Please
see the website http://www.PromoteResearch.org for more details.
Sincerely
John
Edward
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100222/393adabf/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 20, Issue 34
*****************************************