[Haskell-cafe] Improvement suggestions

2012-08-15 Thread José Lopes

Hello everyone,

I am quite new to monadic code so I would like to ask for improvement 
suggestions on the last line of the code below.
I know I could do something like do strs - mapM ...; intercalate ... 
etc, but I would like to avoid the use of -.


Thank you,
José

data XmlState = XmlState Int
type XmlM a = State XmlState a

loop :: Document - XmlM String

someFn :: [Document] - XmlM String
someFn docs =
  return concat `ap` (sequence $ intersperse (return \n) (map loop 
docs))--- improve this line


--
José António Branquinho de Oliveira Lopes
58612 - MEIC-A
Instituto Superior Técnico (IST), Universidade Técnica de Lisboa (UTL)
jose.lo...@ist.utl.pt


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Improvement suggestions

2012-08-15 Thread Twan van Laarhoven

On 15/08/12 17:01, José Lopes wrote:

someFn docs =
   return concat `ap` (sequence $ intersperse (return \n) (map loop docs))


First of all, return x `ap` y = x `fmap` y or x $ y. fmap (or its infix 
synonym ($)) is the answer here, you could write:


someFn docs = concat . intersperse \n $ mapM loop docs

The function Data.List.intercalate is a compation of concat and intersperse, so 
you could write:


someFn docs = intercalate \n $ mapM loop docs

or, depending on your preference,

someFn = fmap (intercalate \n) . mapM loop


Twan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Improvement suggestions

2012-08-15 Thread Clark Gaebel
Try:

concat . intersperse \n $ (sequence $ map loop docs)

On Wed, Aug 15, 2012 at 11:01 AM, José Lopes jose.lo...@ist.utl.pt wrote:

 Hello everyone,

 I am quite new to monadic code so I would like to ask for improvement
 suggestions on the last line of the code below.
 I know I could do something like do strs - mapM ...; intercalate ...
 etc, but I would like to avoid the use of -.

 Thank you,
 José

 data XmlState = XmlState Int
 type XmlM a = State XmlState a

 loop :: Document - XmlM String

 someFn :: [Document] - XmlM String
 someFn docs =
   return concat `ap` (sequence $ intersperse (return \n) (map loop
 docs))--- improve this line

 --
 José António Branquinho de Oliveira Lopes
 58612 - MEIC-A
 Instituto Superior Técnico (IST), Universidade Técnica de Lisboa (UTL)
 jose.lo...@ist.utl.pt


 __**_
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/**mailman/listinfo/haskell-cafehttp://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Improvement suggestions

2012-08-15 Thread José Lopes

Thank you.

On 15-08-2012 23:09, Twan van Laarhoven wrote:

On 15/08/12 17:01, José Lopes wrote:

someFn docs =
   return concat `ap` (sequence $ intersperse (return \n) (map loop 
docs))


First of all, return x `ap` y = x `fmap` y or x $ y. fmap (or 
its infix synonym ($)) is the answer here, you could write:


someFn docs = concat . intersperse \n $ mapM loop docs

The function Data.List.intercalate is a compation of concat and 
intersperse, so you could write:


someFn docs = intercalate \n $ mapM loop docs

or, depending on your preference,

someFn = fmap (intercalate \n) . mapM loop


Twan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


--
José António Branquinho de Oliveira Lopes
58612 - MEIC-A
Instituto Superior Técnico (IST), Universidade Técnica de Lisboa (UTL)
jose.lo...@ist.utl.pt


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe