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. variable parser (John Moore)
2. Re: variable parser (Daniel Fischer)
3. Randomness woes (Erlend Hamberg)
4. prefix instead of infix (John Moore)
5. Critique my program (David Frey)
6. Re: Critique my program (Erlend Hamberg)
7. Re: prefix instead of infix (Daniel Fischer)
----------------------------------------------------------------------
Message: 1
Date: Sat, 9 Jan 2010 17:45:23 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] variable parser
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi,
I just want to add a new parser for variables but this doesn't seem to
work can anybody tell me what I'm doing wrong?
module ExprSyn where
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Expr
runExpr :: String -> IO ()
runExpr str = case runParser expr () " " str of
Left err -> putStrLn "Error:" >> print err
Right val -> print val
demo1 = runExpr "2+5"
data Expr = Mul Expr Expr
| Div Expr Expr
| Add Expr Expr
| Sub Expr Expr
| Val Float
|Let String Expr Expr
| Var String
deriving (Eq,Show)
expr :: Parser Expr
expr = variable <|> buildExpressionParser table factor
<?> "expression"
table :: [[Operator Char st Expr]]
table = [[op "*" Mul AssocLeft, op "/" Div AssocLeft]
,[op "+" Add AssocLeft, op "-" Sub AssocLeft]
]
where
op s f assoc
= Infix (do{ string s; return f}) assoc
factor :: Parser Expr
factor = do{ char '('
; x <- expr
; char ')'
; return x
}
<|> number
<|> variable
<?> "simple expression"
variable :: Parser Expr
variable = do { b <- letter
; do { cs <- word
;return (c:cs)
<|> return [c]
<|> number
}
number :: Parser Expr
number = do{ ds <- many1 digit
; return (Val $ read ds)
}
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100109/ad3167d4/attachment-0001.html
------------------------------
Message: 2
Date: Sat, 9 Jan 2010 19:17:25 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] variable parser
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Samstag 09 Januar 2010 18:45:23 schrieb John Moore:
> Hi,
> I just want to add a new parser for variables but this doesn't seem
> to work can anybody tell me what I'm doing wrong?
>
>
>
> module ExprSyn where
> import Text.ParserCombinators.Parsec
> import Text.ParserCombinators.Parsec.Expr
> runExpr :: String -> IO ()
> runExpr str = case runParser expr () " " str of
> Left err -> putStrLn "Error:" >> print err
> Right val -> print val
> demo1 = runExpr "2+5"
> data Expr = Mul Expr Expr
>
> | Div Expr Expr
> | Add Expr Expr
> | Sub Expr Expr
> | Val Float
> |
> |Let String Expr Expr
> | Var String
>
> deriving (Eq,Show)
> expr :: Parser Expr
> expr = variable <|> buildExpressionParser table factor
> <?> "expression"
variable succeeds on the start of the input "2". Then the second parser
isn't tried at all. Try reversing the alternatives:
expr = buildExpressionParser table factor <|> variable <?> "expression"
Perhaps even better and less surprising, don't let variable parse numbers,
so
expr = buildExpressionParser table factor <|> variable <|> number <?>
"expression"
> table :: [[Operator Char st Expr]]
> table = [[op "*" Mul AssocLeft, op "/" Div AssocLeft]
> ,[op "+" Add AssocLeft, op "-" Sub AssocLeft]
> ]
> where
> op s f assoc
> = Infix (do{ string s; return f}) assoc
> factor :: Parser Expr
> factor = do{ char '('
> ; x <- expr
> ; char ')'
> ; return x
> }
> <|> number
> <|> variable
> <?> "simple expression"
> variable :: Parser Expr
> variable = do { b <- letter
> ; do { cs <- word
> ;return (c:cs)
Wrong type, should be
return (Var (c:cs))
resp.
return (Var c])
> <|> return [c]
> <|> number
> }
>
> number :: Parser Expr
> number = do{ ds <- many1 digit
> ; return (Val $ read ds)
> }
>
>
> John
------------------------------
Message: 3
Date: Sun, 10 Jan 2010 18:25:20 +0100
From: Erlend Hamberg <[email protected]>
Subject: [Haskell-beginners] Randomness woes
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hello, everyone.
I'm experimenting with making a simple genetic algorithm test in Haskell and
have some questions about how to âpropagateâ randomness to the various
functions and about how to do the final iteration. I have used MonadRandom
which already have simplified the program quite a bit. The main functions in
my program are:
(Fitness is a Double, Genome is String of ones and zeros)
------------------------------------------------------------------------
-- calculates the fitness of the given genome
fitness :: Genome -> Fitness
-- takes a genome and a list of booleans and flips the bits according to the
-- bool list
mutate :: Genome -> [Bool] -> Genome
-- takes two genomes and swap the genes after the given point
crossOver :: (Genome, Genome) -> Int -> Genome
-- takes two genoms and a mutation rate and returns a new genome
reproduce :: (RandomGen g) => (Genome, Genome) -> Double -> Rand g (Genome)
-- takes a list of genomes and their corresponding fitness and creates a new
-- population by selecting two individuals by roulette wheel selection and
-- create an offspring until the new population is the same size as the
-- original
makePopulation :: (RandomGen g) => [(Genome, Fitness)] -> Rand g [Genome]
-- takes a list of genomes and creates the next generation by using makePop
nextGeneration :: (RandomGen g) => [Genome] -> Rand g [Genome]
-- returns a list of random genomes each with a given number of genes. used to
-- create the initial population
randomGenomes :: (RandomGen g) => Int -> Int -> Rand g [Genome]
-- picks a random element from a list of (a, âscoreâ). elements with a
higher
-- score are more likely to be picked
rouletteSelect :: (RandomGen g) => [(a, Double)] -> Rand g a
------------------------------------------------------------------------
I have two concerns about my architecture. The first one is that I have
several âlayersâ of randomness: The main function will call makePopulation
which in turn will call rouletteSelect and reproduce. The latter will then
call mutate, which also involves randomness. As can be seen above, I have
rewritten mutate to take a list of booleans so that I can use getRandomRs in
reproduce instead of having to have yet another layer of
g <- getSplit
let genome' = evalRand (mutate genome) g
Handling the random generator explicitly like this feels a bit ugly. Or is
this okay?
Calling getSplit and then evalRand recursively is what I do in makePopulation,
to make a new population. I would greatly appreciate comments on how this
âshouldâ be done. I realise that I could just get a list of random numbers
in
makePopulation and then use this as a source of randomness for the called
functions, but I fear that this would just lead to inelegant code managing
(chunks of) this list.
My second stumbling point is how to produce a list of successive generations.
I made the following test function to try to achieve this:
generations :: (RandomGen g) => [Genome] -> g -> [[Genome]]
generations pop g =
pop : generations (evalRand (nextGeneration pop) g1) g2
where (g1,g2) = split g
However, it doesn't feel right at all to do it like this. It also seems that
the fitness values of all generations except the first one are identical,
which is a good indication that that approach won't work. :-)
The full program, as it is now, can be found here:
http://hpaste.org/fastcgi/hpaste.fcgi/view?id=15782
The problem the algorithm is trying to solve can be found here:
http://www.ai-junkie.com/ga/intro/gat3.html
I'm grateful for all comments and suggestions, but mainly the two
aforementioned points.
--
Erlend Hamberg
âEverything will be ok in the end. If its not ok, its not the end.â
GPG/PGP: 0xAD3BCF19
45C3 E2E7 86CA ADB7 8DAD 51E7 3A1A F085 AD3B CF19
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url :
http://www.haskell.org/pipermail/beginners/attachments/20100110/fa9f57cb/attachment-0001.bin
------------------------------
Message: 4
Date: Sun, 10 Jan 2010 19:33:40 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] prefix instead of infix
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi,
Can anyone explain how to turn the code from infix to Prefix. I want to
include a let statement below. I not sure of how prefix works.
expr :: Parser Expr
expr = buildExpressionParser table factor
<?> "expression"
table :: [[Operator Char st Expr]]
table = [[op "*" Mul AssocLeft, op "/" Div AssocLeft]
,[op "+" Add AssocLeft, op "-" Sub AssocLeft]
]
where
op s f assoc
= Infix (do{ string s; return f}) assoc
factor :: Parser Expr
factor = do{ char '('
; x <- expr
; char ')'
; return x
}
<|> number
<?> "simple expression"
number :: Parser Expr
number = do{ ds <- many1 alphaNum
; return (Val $ read ds)
}
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100110/ccb4b7a6/attachment-0001.html
------------------------------
Message: 5
Date: Sun, 10 Jan 2010 11:44:38 -0800
From: David Frey <[email protected]>
Subject: [Haskell-beginners] Critique my program
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
A couple of months ago, I was tutoring my cousin in an introductory computer
science class. We talked about his assignment in which he had to implement a
subset of the Battleship board game using Java.
I decided it would be fun for me to try to complete the assignment in Haskell.
I forgot about the project for a while, but I picked it up and finished it
today.
I have put the code on BitBucket (a Mercurial based code hosting site for
those who don't know) at this location: http://bitbucket.org/dfrey/battleship/
I am hoping that some people from this list could review my code. I'm not
very concerned with correctness. I'm more interested in how my code can be
clarified through use of existing functions or standard Haskell idioms.
If you don't have Mercurial installed or don't want to clone the repository,
you can browse the source here:
http://bitbucket.org/dfrey/battleship/src/
The original PDF from my cousin's assignment can be viewed here:
http://bitbucket.org/dfrey/battleship/raw/81a01574f164/Assn4.pdf
Feel free to submit your comments in any format. Patches, written comments,
complete re-writes of the program, etc. are all welcome.
Thanks,
David Frey
------------------------------
Message: 6
Date: Sun, 10 Jan 2010 20:55:54 +0100
From: Erlend Hamberg <[email protected]>
Subject: Re: [Haskell-beginners] Critique my program
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On Sunday 10. January 2010 20.44.38 David Frey wrote:
> I am hoping that some people from this list could review my code. I'm not
> very concerned with correctness. I'm more interested in how my code can
> be clarified through use of existing functions or standard Haskell
> idioms.
A great start is â in my experience â is to use hlint on a program. Running
hlint v1.6.14 on the code finds 26 suggestions of which 3 are classified as
errors. While you don't necessarily want to follow every suggestion it comes
up with, it is still a great way to find code that can be improved.
If you have cabal-install, you can install hlint by running âcabal install
hlintâ and then run âhlint *.hsâ.
--
Erlend Hamberg
âEverything will be ok in the end. If its not ok, its not the end.â
GPG/PGP: 0xAD3BCF19
45C3 E2E7 86CA ADB7 8DAD 51E7 3A1A F085 AD3B CF19
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url :
http://www.haskell.org/pipermail/beginners/attachments/20100110/09123102/attachment-0001.bin
------------------------------
Message: 7
Date: Sun, 10 Jan 2010 21:14:15 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] prefix instead of infix
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Sonntag 10 Januar 2010 20:33:40 schrieb John Moore:
> Hi,
> Can anyone explain how to turn the code from infix to Prefix. I want
> to include a let statement below. I not sure of how prefix works.
Can you elaborate? I don't understand what it is you want to do.
Do you want to parse expressions in prefix notation (aka Polish notation),
such as +(3,4) (or, more Lispy, (+ 3 4))?
That'd be simple, but you wouldn't use buldExpressionParser for that (in
Polish notation, neither associativity nor precedence have a meaning).
Or do you want to add a parser for let-expressions to expr?
>
> expr :: Parser Expr
> expr = buildExpressionParser table factor
> <?> "expression"
> table :: [[Operator Char st Expr]]
> table = [[op "*" Mul AssocLeft, op "/" Div AssocLeft]
> ,[op "+" Add AssocLeft, op "-" Sub AssocLeft]
> ]
> where
> op s f assoc
> = Infix (do{ string s; return f}) assoc
> factor :: Parser Expr
> factor = do{ char '('
> ; x <- expr
> ; char ')'
> ; return x
> }
> <|> number
> <?> "simple expression"
> number :: Parser Expr
> number = do{ ds <- many1 alphaNum
> ; return (Val $ read ds)
> }
>
> John
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 19, Issue 6
****************************************