Beginners Digest, Vol 16, Issue 26

2009-10-30 Thread beginners-request
Send Beginners mailing list submissions to
beginners@haskell.org

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
beginners-requ...@haskell.org

You can reach the person managing the list at
beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Chessboard Module, opinions on? (i?fai)
   2.  Re: Chessboard Module, opinions on... (Christian Maeder)
   3.  Memoization (Shawn Willden)
   4. Re:  Memoization (Daniel Fischer)
   5. Re:  Chessboard Module, opinions on? (Andy Elvey)
   6. Re:  Chessboard Module, opinions on? (Andy Elvey)
   7.  list monad question (Matthias Guedemann)
   8. Re:  list monad question (David Virebayre)
   9. Re:  list monad question (Jan Jakubuv)


--

Message: 1
Date: Wed, 28 Oct 2009 11:23:31 -0400
From: i?fai 
Subject: Re: [Haskell-beginners] Chessboard Module, opinions on?
To: Darrin Thompson 
Cc: Beginners@haskell.org
Message-ID: <10bde766-a51e-44fd-903b-bc6c9085f...@me.com>
Content-Type: text/plain; charset=iso-8859-1; format=flowed; delsp=yes

If I had a clock, I would consider it :P

On 2009-10-28, at 11:20 AM, Darrin Thompson wrote:

> On Wed, Oct 28, 2009 at 2:33 AM, iæfai  wrote:
>> The chess AI process is something I still have to hunt for mind  
>> you, but the
>> part that is the most interesting is that I am going to be  
>> controlling a
>> $50,000 robot with this in class :P.
>>
>
> For $50k do you get a robot that can play a serious intimidating
> blitz? Can it smack the clock with Class A player _authority_? :-)
>
> --
> Darrin



--

Message: 2
Date: Wed, 28 Oct 2009 17:01:29 +0100
From: Christian Maeder 
Subject: [Haskell-beginners] Re: Chessboard Module, opinions on...
To: i?fai 
Cc: Beginners@haskell.org
Message-ID: <4ae86ad9.6000...@dfki.de>
Content-Type: text/plain; charset=ISO-8859-1

iæfai schrieb:
> Andy, feel free. I should note that I am going to update this code to
> use Text.PrettyPrint.HughesPJ shortly. In addition, it will be

Using a pretty printer library for this fixed format seems unnecessary
to me.

C.


--

Message: 3
Date: Wed, 28 Oct 2009 19:45:35 -0600
From: Shawn Willden 
Subject: [Haskell-beginners] Memoization
To: beginners@haskell.org
Message-ID: <200910281945.35769.shawn-hask...@willden.org>
Content-Type: text/plain;  charset="us-ascii"

Hi,

I've just begun experimenting with Haskell, and I'm having a lot of fun, but 
my first semi-serious program is in need of some optimization, and based on 
the results of profiling I think a really good start is to memoize one 
particular function which is called many, many times (and currently consumes 
over 80% of the program run time).

The function takes a two-dimensional range and a location within that range 
and returns a list of the locations vertically and horizontally adjacent to 
that location and within the bounds

type Bounds = ((Int,Int),(Int,Int))
type Location = (Int,Int)
adjacentCells :: Bounds -> Location -> [Location]
adjacentCells bounds (col, row) = filter valid cells
where
  valid loc = inRange bounds loc
  neighborLoc' = [(col-1,row),(col+1,row),(col,row-1),(col,row+1)]

The ranges are fairly small -- certainly no more than 10x10, and each location 
has an associated list of at most 4 neighbors (edge locations have less).  
During a given run of the program, the bounds are fixed.

So, any tips on how I can memoize this function?  I have read the Memoization 
page on the wiki, and I understand (I think) the recursive memoization 
section, but it's not clear to me how to apply that approach.  Section one on 
non-recursive memoization seems like what I want, but I don't understand the 
section and the links provided haven't shed any light for me.

The section uses the following notation to describe how to construct a map to 
be used to hold the keys and values:

  Map ()b  := b
  Map (Either a a') b  := (Map a b, Map a' b)
  Map (a,a')b  := Map a (Map a' b)

but I'm not sure what that notation is.  It's not Haskell code, I don't think.

Any and all suggestions appreciated.

Thanks,

Shawn.


--

Message: 4
Date: Thu, 29 Oct 2009 03:20:43 +0100
From: Daniel Fischer 
Subject: Re: [Haskell-beginners] Memoization
To: beginners@haskell.org
Message-ID: <200910290320.44286.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-1"

Am Donnerstag 29 Oktober 2009 02:45:35 schrieb Shawn Willden:
> Hi,
>
> I've just begun experimenting with Haskell, and I'm having a lot of fun,
> but my first semi-serious program is in need of some optimization, and
> based on the results of profiling I think a really good start is to memoi

Beginners Digest, Vol 16, Issue 27

2009-10-30 Thread beginners-request
e n 
> xs)
> -- eliminate xs
> combinations n = foldr ((. (. (:)) . flip map) . (>>=)) [[]] . replicate n
> -- completely pointfree
> combinations = (foldr ((. (. (:)) . flip map) . (>>=)) [[]]  .) . replicate

thank you, looks rather strange to me but works well.

regards

-- 
__
___  ____
Dipl. Inf. Matthias Guedemann  / __\/ _\  /__\
Computer Systems in Engineering   / /   \ \  /_\
Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__
Tel.: 0391 / 67-19359\/ \__/\__/
__


--

Message: 8
Date: Fri, 30 Oct 2009 13:40:13 +
From: Luca Ciciriello 
Subject: [Haskell-beginners] beginner question
To: 
Message-ID: 
Content-Type: text/plain; charset="iso-8859-1"


Hi all.

Just a very basic question.

 

I need to write a function str2lsts :: String -> [[String]] in order to 
transorm a string like:

 

"\"1\",\"cat\",\"dog\"§\"2\",\"duck\",\"goose\""

 

in the list of lists:

 

[["1","cat","dog"],["2","duck","goose"]]

 

I've tried to mix recursion, pattern matching and list comprehension, but the 
obtained result was embarrassing complex (> 20 lines of awful code). I think 
that a more simple solution certainly exists. 


 

Thanks in advance for any idea.

 

Luca
  
_
Download Messenger onto your mobile for free
http://clk.atdmt.com/UKM/go/174426567/direct/01/
-- next part --
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091030/4a0586e3/attachment-0001.html

--

Message: 9
Date: Fri, 30 Oct 2009 15:01:29 +0100
From: Ertugrul Soeylemez 
Subject: [Haskell-beginners] Re: list monad question
To: beginners@haskell.org
Message-ID: <20091030150129.1f758...@tritium.xx>
Content-Type: text/plain; charset=US-ASCII

Hello Matthias,

you may want to have a look at section 11 of my monads tutorial [1],
which contains monadic library functions like replicateM together with
examples and detailed explanations.

[1] http://ertes.de/articles/monads.html#section-11


Greets,
Ertugrul.


Matthias Guedemann  wrote:

> 
> Hi,
> 
> a friend of mine wanted to write function (in Perl) that creates all tuples of
> length 3 of the elements of a given list,
> e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to get
> better at Haskell, I wrote a small function using the list monad for this 
> (tuples
> replaced with lists)
> 
> all3 ls = do
>   a <- ls
>   b <- ls
>   c <- ls
>   return [a,b,c]
> 
> Now I want to make it capable to create all combinations of length n instead 
> of
> fixed length 3 (that's why list instead of tuple), but I don't really see how.
> As the do notation translates to 
> 
> ls >>= \a ->  etc. 
> 
> I thought it should be possible to have some sort of "foldr (>>=)" over a list
> of length n, but I can't figure out how to collect the variable number of
> results in a list for the "return".
> 
> Any hints for that?
> 
> best regards
> Matthias
> 
> 



-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




--

Message: 10
Date: Fri, 30 Oct 2009 15:39:25 +0100
From: Matthias Guedemann 
Subject: Re: [Haskell-beginners] Re: list monad question
To: beginners 
Message-ID: <1256912868-sup-...@pc44es141.cs.uni-magdeburg.de>
Content-Type: text/plain; charset=UTF-8


Hello Ertugrul,

this looks interesting. I read Brent Yorgeys Typeclassopedia and also the work
you cite in your tutorial. 
So I am trying to learn about Applicative, Monoids, Monads etc. by using
them. Your description of the library functions comes handy, as reading the
source directly does not really help if you're still struggling with
understanding the concepts. And on the other hand, due to the abstract nature of
monads, a lot of functionality should be "hidden" in the library (just like my
rewrite of replicateM)

thank you very much, will read it on the weekend

regards
Matthias




Excerpts from Ertugrul Soeylemez's message of Fr Okt 30 15:01:29 +0100 2009:
> Hello Matthias,
> 
> you may want to have a look at section 11 of my monads tutorial [1],
> which contains monadic library functions like replicateM together with
> examples and detailed explanations.
> 
> [1] http://ertes.de/articles/monads.html#section-11
> 
> 
> Greets,
> Ertugrul.
&

Beginners Digest, Vol 16, Issue 28

2009-10-30 Thread beginners-request
 like:
>
> "\"1\",\"cat\",\"dog\"§\"2\",\"duck\",\"goose\""
>
> in the list of lists:
>
> [["1","cat","dog"],["2","duck","goose"]]

A variety of solutions on these blog posts, and the comments:

http://gimbo.org.uk/blog/2007/04/20/splitting-a-string-in-haskell/
http://julipedia.blogspot.com/2006/08/split-function-in-haskell.html

Shawn.


--

Message: 4
Date: Fri, 30 Oct 2009 16:34:13 +0100
From: Matthias Guedemann 
Subject: Re: [Haskell-beginners] list monad question
To: Daniel Fischer 
Cc: beginners 
Message-ID: <1256916789-sup-...@pc44es141.cs.uni-magdeburg.de>
Content-Type: text/plain; charset=UTF-8

> Yes :D The pointfree f is nicely obfuscated. But if your friend is a perl
> coder, he should 
> be able to appreciate that.

Honestly, he just wanted a "one-loop-using solution" and was not too interested
in anything using Haskell :-)

> The standard way to express f, however, is liftM2 (:), so
> 
> combinations = (foldr (liftM2 (:)) [[]] .) . replicate 
> -- isn't that boring?

true, it's almost readable 
At the moment trying to use pointfree is basically pointless for me, more
practice is needed.


regards
Matthias

-- 
__
___  ____
Dipl. Inf. Matthias Guedemann  / __\/ _\  /__\
Computer Systems in Engineering   / /   \ \  /_\
Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__
Tel.: 0391 / 67-19359\/ \__/\__/
__


--

Message: 5
Date: Fri, 30 Oct 2009 15:51:46 +
From: Luca Ciciriello 
Subject: RE: [Haskell-beginners] beginner question
To: , 
Message-ID: 
Content-Type: text/plain; charset="iso-8859-1"


Thanks. 

 

import Text.ParserCombinators.Parsec 

 

simple = between (char '"') (char '"') (many (satisfy (/= '"')))


multiple = sepBy simple (char ',') 
total = sepBy multiple (char '@') 

 

str2lsts :: String -> [[String]]
str2lsts str= case parse total "" str of
   Left err -> error (show err)
   Right lsts -> lsts

 

solves my problem. As you can see I've replaced '§' with '@' and now all works 
fine.

 

Luca. 
 
> From: daniel.is.fisc...@web.de
> To: beginners@haskell.org
> Subject: Re: [Haskell-beginners] beginner question
> Date: Fri, 30 Oct 2009 15:46:33 +0100
> 
> Am Freitag 30 Oktober 2009 14:40:13 schrieb Luca Ciciriello:
> > Hi all.
> >
> > Just a very basic question.
> >
> >
> >
> > I need to write a function str2lsts :: String -> [[String]] in order to
> > transorm a string like:
> >
> >
> >
> > "\"1\",\"cat\",\"dog\"§\"2\",\"duck\",\"goose\""
> >
> >
> >
> > in the list of lists:
> >
> >
> >
> > [["1","cat","dog"],["2","duck","goose"]]
> >
> >
> >
> > I've tried to mix recursion, pattern matching and list comprehension, but
> > the obtained result was embarrassing complex (> 20 lines of awful code). I
> > think that a more simple solution certainly exists.
> >
> 
> splitOnToken :: Eq a => a -> [a] -> [[a]]
> splitOnToken t xs
> = case break (== t) xs of
> (hd,tl) -> hd:case tl of
> (_:r@(_:_)) -> splitOnToken t r
> _ -> []
> 
> str2lsts = map (map read . splitOnToken ',') . splitOnToken '§'
> 
> if things weren't enclosed in quotation marks inside the string, it would be 
> the nicer
> 
> map (splitOnToken ',') . splitOnToken '§'
> 
> , provided of course, neither ',' nor '§' are valid characters for the target 
> strings.
> 
> import Text.ParserCombinators.Parsec
> 
> simple = between (char '"') (char '"') (many (staisfy (/= '"')))
> -- alternative: simple = char '"' >> manyTill anyChar (char '"')
> 
> multiple = sepBy simple (char ',')
> 
> total = sepBy multiple (char '§')
> 
> str2lsts str
> = case parse total "" str of
> Left err -> error (show err)
> Right lsts -> lsts
> 
> >
> >
> >
> > Thanks in advance for any idea.
> >
> >
> >
> > Luca
> 
> 
> ___
&g