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. Simplifying code (edgar klerks)
2. Reading pixels in Haskell SDL (han)
3. Re: Simplifying code (Daniel Fischer)
4. Re: Simplifying code (Patrick LeBoutillier)
5. Re: Simplifying code (Ertugrul Soeylemez)
6. Re: Simplifying code (edgar klerks)
7. Re: Simplifying code (Daniel Fischer)
----------------------------------------------------------------------
Message: 1
Date: Tue, 9 Feb 2010 23:19:02 +0100
From: edgar klerks <[email protected]>
Subject: [Haskell-beginners] Simplifying code
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Thanks for all your examples. These are helping me really much. I have an
imperative background (which hinders me), but I have the feeling functional
programming has the future and I find it really fun to do. I will produce my
own real functional version tomorrow and post it (maybe the day after that).
It is 1:00 so i got to go to bed.
Thanks for all your help!
(If this message is not placed under the thread can someone explain how I do
this from firefox. I have made my default mail handler gmail in firefox, but
many times the post doesn't appear under the thread. I understand this is an
annoyance.)
With kind regards,
Edgar
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100209/58c4b021/attachment-0001.html
------------------------------
Message: 2
Date: Wed, 10 Feb 2010 07:55:13 +0900
From: han <[email protected]>
Subject: [Haskell-beginners] Reading pixels in Haskell SDL
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I'm using SDL
(http://hackage.haskell.org/packages/archive/SDL/0.5.9/doc/html/Graphics-UI-SDL.html)
As far as I know, in the original SDL for C, reading and writing
pixels on a surface is done by accessing pointer + y * width + x. In
Pygame, since Python has no such thing as pointer, they have get_at
and set_at function for this task. I thought there would be something
equivalent in Haskell SDL, but couldn't find any.
For writing pixels on the Surface, I've found a couple of functions:
Graphics.UI.SDL.Video.fillRect
(http://hackage.haskell.org/packages/archive/SDL/0.5.9/doc/html/Graphics-UI-SDL-Video.html#v%3AfillRect)
Graphics.UI.SDL.Primitives.pixel
(http://hackage.haskell.org/packages/archive/SDL-gfx/0.5.3/doc/html/Graphics-UI-SDL-Primitives.html#v%3Apixel)
...
However, I can't find one that reads.
Graphics.UI.SDL.Types.surfaceGetPixels
(http://hackage.haskell.org/packages/archive/SDL/0.5.9/doc/html/Graphics-UI-SDL-Types.html#v%3AsurfaceGetPixels)
looks like the one, but I have no idea how I can use it, since it
returns Pixels which is defined as:
data PixelsData
type Pixels = Ptr PixelsData
I want to read a pixel at (x, y) of a Surface. Does anyone know how?
------------------------------
Message: 3
Date: Wed, 10 Feb 2010 00:31:07 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Simplifying code
To: Patrick LeBoutillier <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Dienstag 09 Februar 2010 23:07:55 schrieb Patrick LeBoutillier:
> Daniel,
>
> > Sure. If you don't mind that the mutations come in a different order,
> > one thing that works wonders is "sequence",
> >
> > sequence :: Monad m => [m a] -> m [a]
> >
> > In particular, for m = [], sequence :: [[a]] -> [[a]]. Then, knowing
> > what sequence does, we can write
> >
> > import Control.Monad (sequence)
> >
> > generateAll :: String -> [String]
> > generateAll word = sequence (map f word)
> > where
> > f c = case lookup c leat of
> > Just r -> [c,r]
> > Nothing -> [c]
>
> That's very nice!
Thanks. But from a clean-code-higher-level perspective, it's even nicer
with your
> -- Returns a list of possible characters for c
> mutateLetter :: Char -> [Char]
> mutateLetter c = c : (maybeToList $ lookup c leet)
(here is a point where it would be even nicer if lookup had the type
lookup :: (Eq a, MonadPlus m) => a -> [(a,b)] -> m b
). The performance-junkie in me would want to look at the core to make sure
the maybeToList is eliminated by the compiler, though.
>
> One question though: In the docs sequence is described as:
>
> "Evaluate each action in the sequence from left to right, and
> collect the results."
>
> How is one supposed to deduce what the behavior will be for the list
> monad (besides looking at the source)?
Given its polymorphic type
sequence :: Monad m => [m a] -> m [a]
, what can sequence do?
For
sequence []
, there's really only one possibility (not involving undefined/error), so
sequence [] = return []
Okay, that was the trivial part, now what can be done with nonempty lists?
It could ignore the input and return [] in any case, but that wouldn't be
useful at all, so we can discard that possibility. What could be usefully
done with
sequence (m1:ms) ?
It has to do something with m1 and something with ms, then combine the
results to a list of [a], which it returns.
What can it do with m1? Since all that sequence knows about m1 is the type
(Monad m => m a), it can't do anything but what's provided by that
constraint. Basically, it can only put it on the left of a (>>=). There's
on decision to be made, shall it be
sequence (m1:ms) = m1 >>= \x -> something with x and ms
or
something with ms >>= \xs -> (m1 >>= \x -> something with x and xs)
?
And what can it do with the tail of the list, ms? Why, sequence it of
course.
So it's either
sequence (m1:ms) = m1 >>= \x -> (sequence ms >>= \xs -> return (fun x xs))
{-
sequence (m1:ms) = do
x <- m1
xs <- sequence ms
return (fun x xs)
-}
or
sequence (m1:ms) = sequence ms >>= \xs -> (m1 >>= \x -> return (fun x xs))
{-
sequence (m1:ms) = do
xs <- sequence ms
x <- m1
return (fun x xs)
-}
where
fun :: forall a. a -> [a] -> [a]
Now there's a lot of nonsense you could use for 'fun',
fun x xs = reverse (x:xs)
fun x xs = x:xs ++ [x,x,x]
fun x xs = front ++ x:back where (front,back) = splitAt 17 xs
...
, but the most prominent function of type forall a. a -> [a] -> [a] is the
only one to be reasonably expected here, so
fun x xs = x : xs
and the only question that remains is in which order things are chained.
That is answered by the docs, left to right, so
sequence [] = return []
sequence (m1:ms) = m1 >>= \x -> sequence ms >>= \xs -> return (x:xs)
{-
sequence (m1:ms) = do
x <- m1
xs <- sequence ms
return (x:xs)
sequence (m1:ms) = m1 >>= \x -> liftM (x :) (sequence ms)
-}
(or equivalent).
Now you need to know how (>>=) is defined for [], namely
ys >>= f = concatMap f ys.
The short answer is, you can't deduce it wihout knowing the Monad instance
for [], and if you know that well enough to not be confused by "evaluate
the action" (which takes time), it's fairly straightforward.
>
>
> Patrick
------------------------------
Message: 4
Date: Tue, 9 Feb 2010 21:35:45 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Simplifying code
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Daniel,
As usual, thanks a lot for this enlightening response.
Patrick
On Tue, Feb 9, 2010 at 6:31 PM, Daniel Fischer <[email protected]> wrote:
> Am Dienstag 09 Februar 2010 23:07:55 schrieb Patrick LeBoutillier:
>> Daniel,
>>
>> > Sure. If you don't mind that the mutations come in a different order,
>> > one thing that works wonders is "sequence",
>> >
>> > sequence :: Monad m => [m a] -> m [a]
>> >
>> > In particular, for m = [], sequence :: [[a]] -> [[a]]. Then, knowing
>> > what sequence does, we can write
>> >
>> > import Control.Monad (sequence)
>> >
>> > generateAll :: String -> [String]
>> > generateAll word = sequence (map f word)
>> > where
>> > f c = case lookup c leat of
>> > Just r -> [c,r]
>> > Nothing -> [c]
>>
>> That's very nice!
>
> Thanks. But from a clean-code-higher-level perspective, it's even nicer
> with your
>
>> -- Returns a list of possible characters for c
>> mutateLetter :: Char -> [Char]
>> mutateLetter c = c : (maybeToList $ lookup c leet)
>
> (here is a point where it would be even nicer if lookup had the type
>
> lookup :: (Eq a, MonadPlus m) => a -> [(a,b)] -> m b
> ). The performance-junkie in me would want to look at the core to make sure
> the maybeToList is eliminated by the compiler, though.
>
>>
>> One question though: In the docs sequence is described as:
>>
>> "Evaluate each action in the sequence from left to right, and
>> collect the results."
>>
>> How is one supposed to deduce what the behavior will be for the list
>> monad (besides looking at the source)?
>
> Given its polymorphic type
>
> sequence :: Monad m => [m a] -> m [a]
>
> , what can sequence do?
>
> For
> sequence []
> , there's really only one possibility (not involving undefined/error), so
> sequence [] = return []
>
> Okay, that was the trivial part, now what can be done with nonempty lists?
> It could ignore the input and return [] in any case, but that wouldn't be
> useful at all, so we can discard that possibility. What could be usefully
> done with
>
> sequence (m1:ms) ?
>
> It has to do something with m1 and something with ms, then combine the
> results to a list of [a], which it returns.
> What can it do with m1? Since all that sequence knows about m1 is the type
> (Monad m => m a), it can't do anything but what's provided by that
> constraint. Basically, it can only put it on the left of a (>>=). There's
> on decision to be made, shall it be
>
> sequence (m1:ms) = m1 >>= \x -> something with x and ms
>
> or
>
> something with ms >>= \xs -> (m1 >>= \x -> something with x and xs)
> ?
> And what can it do with the tail of the list, ms? Why, sequence it of
> course.
> So it's either
>
> sequence (m1:ms) = m1 >>= \x -> (sequence ms >>= \xs -> return (fun x xs))
> {-
> sequence (m1:ms) = do
> x <- m1
> xs <- sequence ms
> return (fun x xs)
> -}
> or
>
> sequence (m1:ms) = sequence ms >>= \xs -> (m1 >>= \x -> return (fun x xs))
> {-
> sequence (m1:ms) = do
> xs <- sequence ms
> x <- m1
> return (fun x xs)
> -}
>
> where
>
> fun :: forall a. a -> [a] -> [a]
>
> Now there's a lot of nonsense you could use for 'fun',
>
> fun x xs = reverse (x:xs)
> fun x xs = x:xs ++ [x,x,x]
> fun x xs = front ++ x:back where (front,back) = splitAt 17 xs
> ...
> , but the most prominent function of type forall a. a -> [a] -> [a] is the
> only one to be reasonably expected here, so
>
> fun x xs = x : xs
>
> and the only question that remains is in which order things are chained.
> That is answered by the docs, left to right, so
>
> sequence [] = return []
> sequence (m1:ms) = m1 >>= \x -> sequence ms >>= \xs -> return (x:xs)
> {-
> sequence (m1:ms) = do
> x <- m1
> xs <- sequence ms
> return (x:xs)
>
> sequence (m1:ms) = m1 >>= \x -> liftM (x :) (sequence ms)
> -}
> (or equivalent).
> Now you need to know how (>>=) is defined for [], namely
>
> ys >>= f = concatMap f ys.
>
> The short answer is, you can't deduce it wihout knowing the Monad instance
> for [], and if you know that well enough to not be confused by "evaluate
> the action" (which takes time), it's fairly straightforward.
>
>>
>>
>> Patrick
>
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 5
Date: Wed, 10 Feb 2010 03:41:20 +0100
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: Simplifying code
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Patrick LeBoutillier <[email protected]> wrote:
> > Sure. If you don't mind that the mutations come in a different
> > order, one thing that works wonders is "sequence",
> >
> > sequence :: Monad m => [m a] -> m [a]
> >
> > In particular, for m = [], sequence :: [[a]] -> [[a]]. Then, knowing what
> > sequence does, we can write
> >
> > import Control.Monad (sequence)
> >
> > generateAll :: String -> [String]
> > generateAll word = sequence (map f word)
> > where
> > f c = case lookup c leat of
> > Just r -> [c,r]
> > Nothing -> [c]
>
> That's very nice!
>
> One question though: In the docs sequence is described as:
>
> "Evaluate each action in the sequence from left to right, and
> collect the results."
>
> How is one supposed to deduce what the behavior will be for the list
> monad (besides looking at the source)?
Intuitively it's very easy to understand and makes perfect sense, if you
interpret the list monad as nondeterminism:
sequence [c1,c2,c3] = do
x1 <- c1
x2 <- c2
x3 <- c3
return [x1,x2,x3]
sequence ["ab", "cd", "ef"] = do
x1 <- "ab"
x2 <- "cd"
x3 <- "ef"
return [x1,x2,x3]
= ["ace", "acf", "ade", "adf", "bce", "bcf", "bde", "bdf"]
In this code x1 stands for all results of c1, x2 for all results of c2
and x3 for all results of c3. It's a convenient way of doing list
comprehension without syntactic sugar. See also mapM and replicateM,
which have interesting behaviours in the list monad.
Greets
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/
------------------------------
Message: 6
Date: Wed, 10 Feb 2010 05:49:46 +0100
From: edgar klerks <[email protected]>
Subject: [Haskell-beginners] Re: Simplifying code
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello all,
I have a very interesting alternative solution of the problem. First I
generate a tree with all the permutations:
eg if I want to find al permutations of ao, I get the following tree
[a @]
| |
[o 0] [o 0]
Then I walk trough the tree so I can print it. There is only one ugly thing.
Showtree' returns a string (in our example a...@o@0). To make it a list i
put a \n between a left and right node and then use lines to make it a list
of strings. Can someone point me in the right direction how to beautify it a
bit?
I also have an annoying problem with gmail and firefox. It seems it doesn
add my posts to the current thread, but starts a new one. Oh well think I
switch to evolution. I will review all yours solutions tomorrow. I saw some
very beautifull things.
With kind regards,
Edgar Klerks
module Main where
import Data.Char
data WordTree = Chain (Char,WordTree)
| Choice (Char, WordTree) (Char, WordTree)
| Stop
instance Show WordTree where
show = unlines.showTree
type Rule = (Char, Char)
type Rules = [Rule]
infixl 4 ==>
a ==> b = (a,b)
rules :: Rules
rules = [ 'a' ==> '@', 'l' ==> '|']
buildTree :: String -> Rules -> WordTree
buildTree [] r = Stop
buildTree (c:cs) r = case lookup c r of
Just a -> Choice (a, buildTree cs r) (c, buildTree
cs r)
Nothing -> Chain (c, buildTree cs r)
showTree a = lines $ showTree' a []
showTree' (Chain (a,b)) p = a : showTree' b p
showTree' (Choice (a,b) (c,d)) p = c : showTree' d p ++ "\n" ++ (a :
showTree' b p)
showTree' (Stop) p = p
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100209/175b9dd9/attachment-0001.html
------------------------------
Message: 7
Date: Wed, 10 Feb 2010 10:46:03 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Simplifying code
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Mittwoch 10 Februar 2010 00:31:07 schrieb Daniel Fischer:
> Am Dienstag 09 Februar 2010 23:07:55 schrieb Patrick LeBoutillier:
> > Daniel,
<snip>
> > One question though: In the docs sequence is described as:
> >
> > "Evaluate each action in the sequence from left to right, and
> > collect the results."
> >
> > How is one supposed to deduce what the behavior will be for the list
> > monad (besides looking at the source)?
<snip>
Or, of course, you could come from the other end.
You want to generate the cartesian product of a list of lists.
How do you do that?
Easy, for each element of the first list, you stick that to the front of
each element of the product of the remaining lists:
cartesianProduct :: [[a]] -> [[a]]
cartesianProduct (l:ls) = [h:t | h <- l, t <- cartesianProduct ls]
-- what to do if there is no first list? The cartesian product of an empty
set of sets is a one element set, so
cartesianProduct [] = [[]]
or,
cartesianProduct (l:ls) = do
h <- l
t <- cartesianProduct ls
return (h:t)
cartesianProduct [] = return []
-- Hey, that's sequence!!!
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 20, Issue 17
*****************************************