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: wanto post haskell question (Wonchan Lee)
2. Re: Monadic composition without throwing genericity under
the bus? (Dave Bayer)
3. Re: Fast tree manipulation (Heinrich Apfelmus)
4. Re: Monadic composition without throwing genericity under
the bus? (Stephen Tetley)
5. Re: Monadic composition without throwing genericity under
the bus? (Heinrich Apfelmus)
6. Re: Monadic composition without throwing genericity under
the bus? (Heinrich Apfelmus)
7. Re: Re: Monadic composition without throwing genericity
under the bus? (Stephen Tetley)
8. Re: Fast tree manipulation (Gabi)
9. Re: Re: Monadic composition without throwing genericity
under the bus? (Dave Bayer)
----------------------------------------------------------------------
Message: 1
Date: Thu, 4 Feb 2010 11:39:28 +0900
From: Wonchan Lee <[email protected]>
Subject: Re: [Haskell-beginners] wanto post haskell question
To: ??? <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=EUC-KR
Hello, Kwangrae.
I think the following function will work for you:
multSM :: Double -> Matrix -> Matrix --multiplies a scalar and a matrix
multSM d m = [ [d * b | b <- a] | a <- m]
I just use nested list comprehension.
2010/2/2 Á¶±¤·¡ <[email protected]>:
> hi I have a question about haskell coding.
>
> Matrices
>
> One of many ways to define matrices in Haskell is the list of matrix rows,
> where a row is a list of double precision oating point numbers:
>
> type Matrix=[[Double]]
>
> Using this type you should define the following functions (definition of dim
> is given as an inspirational example).
>
> dim :: Matrix -> (Int,Int) --returns matrix dimension (number of rows and
> columns)
> dim m = if (not.isMatrix) m then error "Not a matrix" else (length m, length
> (head m))
>
> isMatrix :: Matrix -> Bool --checks whether all rows have the same length
> isMatrix m = and [length (head m) == length a | a <- m]
>
> I have done upto here and I am stuck on multSM.
>
> multSM :: Double -> Matrix -> Matrix --multiplies a scalar and a matrix
>
> when I do:
>
> multSM d m = [[(b*a)| b<-[d], a<-(head m)]] or [map (*d) (head m)]
>
> ...I get (using Hugs):
>
> Main> multSM 3 [[2,3,4],[1,3,4],[6,3,0]]
> [[6.0,9.0,12.0]]
>
> So I solve 3 [2,3,4] but I do not know how to get the rest 3
> [[1,3,4],[6,3,0].
>
> Please help me on this problem.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
Message: 2
Date: Wed, 3 Feb 2010 23:45:14 -0500
From: Dave Bayer <[email protected]>
Subject: Re: [Haskell-beginners] Monadic composition without throwing
genericity under the bus?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
Let me be as concise as I can, for a second try.
One can't make a function-valued monad into an instance of Category, because a
Category takes two type arguments, while a Monad takes one?
I'm having serious cognitive dissonance here. The "back 40 acres" of the GHC
manual is filled with brilliant work-arounds and references to research papers
for far more arcane type dilemmas than this. My local hardware store is filled
with pieces of copper that fit 1/2" pipe to 3/4" pipe, and they don't check for
Ph.Ds at the door. But there's no provision in the language to fit a type class
taking two arguments to a type class taking one argument?
I simply can't believe that I'm the first person to stumble over this. Either
this is a famous rough edge, and others can list off a dozen similar
circumstances where one gets stuck, or there's an easy work-around I'm just not
seeing.
Can anyone confirm that it's simply not possible to plumb type classes the way
I want to plumb them? If so, should I be proposing a language extension on a
different mailing list?
On Feb 2, 2010, at 11:08 AM, Daniel Fischer wrote:
> You can help it somewhat by adding more FunDeps to Composable,
>
> class Composable a b c | a b â c, a câ b, b câ a where
> compose â· a â b â c
That's nice, I didn't think of that, but as you say it doesn't fix the problem.
------------------------------
Message: 3
Date: Thu, 04 Feb 2010 11:26:36 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Fast tree manipulation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Gabi wrote:
> Maybe Data.Sequence is a good option. Seems to be designed to give
> good performance when mutating
> If trees could be represented using Data.Sequence, I think that tree
> manipulation performance would be bearable.
> Any other libraries that might be useful?
I don't see why you would want to use Data.Sequence for representing trees.
Trees are usually represented as
data Tree a b = Leaf b | Branch a (Tree a b) (Tree a b)
While trees are persistent, they are also shared; "mutating" a tree will
only change a small part of it. And if you are doing a lot of local
operations, a zipper may help
http://en.wikibooks.org/wiki/Haskell/Zippers
In any case, I'd recommend to go ahead and use the simplest possible
representation of trees for a small prototype. If any performance
problems crop up, ask the list again.
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 4
Date: Thu, 4 Feb 2010 10:35:50 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Monadic composition without throwing
genericity under the bus?
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Dave
You might need more that a type system extension for this one.
I've called your composition operator (*>>*), if you stack the type
signatures together I can't see a way of getting from (*>>*) to (.).
Compare it to the Kleisli composition of monads - where the step to
(.) is more apparent (swap '-> m' for cat):
-- (.) :: cat b c -> cat a b -> cat a c
-- (.) :: b `cat` c -> a `cat` b -> a `cat` c -- infix
-- (*>>*) :: Monad m => m (b -> c) -> m (a -> b) -> m (a -> c)
-- Kleisli composition does satisfy Category
-- _Kleisli_ :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c)
I think I've derived what you are doing below with Graham Hutton's
parser monad, the 'easy work around' is to use (*>>*) rather than try
to use (.):
module Fun where
-- Starting with a Graham Hutton's parser as it is (probably) the
-- simplest monadic parser
newtype Parser a = Parser { parse :: (String -> [(a,String)]) }
instance Monad Parser where
return a = Parser (\cs -> [(a,cs)])
p >>= f = Parser (\cs -> concat [parse (f a) cs' | (a,cs') <- parse p cs])
item :: Parser Char
item = Parser (\cs -> case cs of
"" -> []
(c:cs) -> [(c,cs)])
zero :: Parser a
zero = Parser (\cs -> [])
sat :: (Char -> Bool) -> Parser Char
sat p = do {c <- item; if p c then return c else zero}
char0 :: Char -> Parser Char
char0 c = sat (c==)
char :: Char -> Parser ShowS
char c = do {ch <- sat (c==) ; return (showChar c) }
-- consume but don't produce ?
place0 :: String -> Parser String
place0 str = do { str' <- mapM char0 str; return "" }
place :: String -> Parser ShowS
place str = place0 str >> return (\s -> s) -- no string aka id
string0 :: String -> Parser String
string0 str = mapM char0 str
string :: String -> Parser ShowS
string str = do { str' <- string0 str; return (showString str) }
--------------------------------------------------------------------------------
dot :: Parser ShowS
dot = char '.'
comment :: Parser ShowS
comment = place "-- "
runParser :: Parser ShowS -> String -> String
runParser p inp = post $ ((parse p) inp)
where
post [(ans,_)] = ans $ ""
post xs = error (unlines $ map (\(f,cs) -> show (f "",cs)) xs)
demo1 :: String
demo1 = runParser (comment >> dot) "-- ."
startPragma :: Parser ShowS
startPragma = string "{-#"
demo2 :: String
demo2 = runParser (startPragma) "{-#"
space :: Parser ShowS
space = char ' '
languageU :: Parser ShowS
languageU = string "LANGUAGE"
-- first attempt - 'endo' style keeps the same type
(*>*) :: Monad m => m (a -> a) -> m (a -> a) -> m (a -> a)
mf *>* mg = do { f <- mf; g <- mg; return (f.g) }
demo3 :: String
demo3 = runParser (startPragma *>* space *>* languageU) "{-# LANGUAGE"
-- second attempt - 'bluebird' style - proper composition
-- (more general)
(*>>*) :: Monad m => m (b -> c) -> m (a -> b) -> m (a -> c)
mf *>>* mg = do { f <- mf; g <- mg; return (f.g) }
demo4 :: String
demo4 = runParser (startPragma *>>* space *>>* languageU) "{-# LANGUAGE"
-- (.) :: cat b c -> cat a b -> cat a c
-- (*>>*) :: Monad m => m (b -> c) -> m (a -> b) -> m (a -> c)
-- ???
-- Kleisli composition does satisfy Category
-- _Kleisli_ :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c)
-- TYPE ERROR
-- demo5x :: String
-- demo5x = runParser (startPragma . space . languageU) "{-# LANGUAGE"
------------------------------
Message: 5
Date: Thu, 04 Feb 2010 11:43:42 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Monadic composition without throwing
genericity under the bus?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Dave Bayer wrote:
> Let me be as concise as I can, for a second try.
>
> One can't make a function-valued monad into an instance of Category,
> because a Category takes two type arguments, while a Monad takes one?
> [...]
> I simply can't believe that I'm the first person to stumble over
> this. Either this is a famous rough edge, and others can list off a
> dozen similar circumstances where one gets stuck, or there's an easy
> work-around I'm just not seeing.
>
> Can anyone confirm that it's simply not possible to plumb type
> classes the way I want to plumb them? If so, should I be proposing a
> language extension on a different mailing list?
You want a composition of functors
Wrap m ~ m ° (->)
but since higher-kinded polymorphism is a bit limited in Haskell
(decidability!), I don't think there's a way to make this an instance of
Category directly.
The usual solution is to make Wrap a newtype
newtype Wrap m a b = Wrap (m (a -> b))
instance Monad m => Category (Wrap m) where ...
and live with it.
If you want to be a bit more generic, you can use a newtype to denote
functor composition
{-# LANGUAGE TypeSynonymInstances #-}
newtype f `O` g a b = O (f (g a b))
instance Monad m => Category (m `O` (->)) where
id = return id
f . g = liftM2 (.) f g
But in both cases, there is no way around the fact that the Category
class needs a new type as argument.
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 6
Date: Thu, 04 Feb 2010 11:46:28 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Monadic composition without throwing
genericity under the bus?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Dave Bayer wrote:
> I've been playing with parsing, for text filtering applications such
> as an alternate GHC literate preprocessor. Here, it pays to have one's
> monad handle both the input and output streams out of sight. Using
> ShowS-valued monads, one can express most grammatical constructs as
> simple composition. I'm sure many people have had this idea, but the
> resulting parsers that I write end up much shorter than any demo code
> I've seen. For example, the "code is indented, comments are flush,
> periods delimit block comments" literate preprocessor that I depend on
> daily (leaving out the heredoc code) is just
>
> dot, comment, dotLine, commentLine, codeLine, dotBlock, delit â· Parser
>
> dot = char '.'
> comment = place "-- "
>
> codeLine = white â till (heredoc ⨠whiteLine) word
> commentLine = comment â line
>
> dotLine = comment â dot â whiteLine
> dotBlock = dotLine â till (dotLine ⨠eof) (whiteLine ⨠commentLine)
>
> delit = till (skip (many whiteLine) â eof)
> (whiteLine ⨠dotBlock ⨠codeLine ⨠commentLine)
This looks intriguing! Can you elaborate on how this works?
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 7
Date: Thu, 4 Feb 2010 11:25:36 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Re: Monadic composition without
throwing genericity under the bus?
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
The parser has a type restricted return type - always a 'Hughes
string' with efficient concatenation (ShowS :: String -> String)
rather than some polymorphic answer (e.g a parse tree). For formatting
it allows you to add text without consuming any:
inserttext :: String -> Parser
inserttext str = return (showString str)
Or rewrite matched input:
rewrite :: String -> String -> Parser
rewrite inp out = string inp >> return (showString out)
Or drop text, I presume this is what the place combinator does.
Its a nice technique.
On 4 February 2010 10:46, Heinrich Apfelmus <[email protected]> wrote:
>
> This looks intriguing! Can you elaborate on how this works?
------------------------------
Message: 8
Date: Thu, 4 Feb 2010 14:34:44 +0200
From: Gabi <[email protected]>
Subject: [Haskell-beginners] Re: Fast tree manipulation
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Heinrich,
I didn't see any mature zipper libraries to efficiently manipulate trees.
Can you point me to any mature libs ?
Aside from that, there are many reasons why I would represent trees
using sequence. The major one is that many GA operations like 2 point
crossover and mutation are trivial if we represent the genome in
vectors or sequence instead of trees.
The downside is the cost of the translation of sequences back to trees.
--
Regards,
Gabi
http://bugspy.net
------------------------------
Message: 9
Date: Thu, 4 Feb 2010 08:26:32 -0500
From: Dave Bayer <[email protected]>
Subject: Re: [Haskell-beginners] Re: Monadic composition without
throwing genericity under the bus?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1
Thanks for all your answers.
Yes, Stephen explains exactly what I'm doing. I had been using a literate
preprocessor that could have been written in Basic, but didn't include
heredocs. It was very short, but I wanted something more idiomatic, so I stared
at everyone's parser tutorials, wondering about monadic -vs- applicative etc.
etc. The twin irritations of do notation everywhere, and ++ everywhere, pushed
me over the edge to try ShowS-valued monads, and I loved how the code looked.
Normally I accept wrappers on everything as just how one does business in
Haskell. As you say, to avoid undecidable type issues. Here I was struggling
for days to make the code as lean as possible, and I could almost get away
without wrappers.
I've been following the migration convention of ASCII for historic Prelude,
Unicode for generic version of same, so I didn't want to steal ° for this one
purpose; it should be categorical composition. I like the answer that m ° (->)
is simply something different, I shouldn't think of it as composition. I had
been using â for this, I'll probably go back to it.
For a simple parser tutorial, or my immediate application of munging text e.g.
literate preprocessor, ok to hardwire ShowS-valued monads. However, when one
does have to break down and go into do notation (e.g. to implement my heredocs
which steal the indentation from the closing EOF, and otherwise looks cleaner
with multiple parsing passes), it might be slicker to use a general (a â b)
type in place of ShowS. In fact, I believe one could do anything other parsers
do, this way, with the benefit of most expressions being composition not
requiring do-notation.
Anyhow, going further with monads returning (a â b) required me to make my
peace with this composition issue, so I thank each of you!
I will write up a tutorial on this form of parsing when I submit my literate
preprocessor to Cabal.
On Feb 4, 2010, at 6:25 AM, Stephen Tetley wrote:
> The parser has a type restricted return type - always a 'Hughes
> string' with efficient concatenation (ShowS :: String -> String)
> rather than some polymorphic answer (e.g a parse tree). For formatting
> it allows you to add text without consuming any:
>
> inserttext :: String -> Parser
> inserttext str = return (showString str)
>
> Or rewrite matched input:
>
> rewrite :: String -> String -> Parser
> rewrite inp out = string inp >> return (showString out)
>
> Or drop text, I presume this is what the place combinator does.
>
> Its a nice technique.
>
> On 4 February 2010 10:46, Heinrich Apfelmus <[email protected]> wrote:
>>
>> This looks intriguing! Can you elaborate on how this works?
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 20, Issue 9
****************************************