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:  What is a "Monad Comprehension" ? (John M. Dlugosz)
   2. Re:  Problem combining monads <sigh!> (John M. Dlugosz)
   3. Re:  Problem combining monads <sigh!> (Brent Yorgey)
   4. Re:  Problem combining monads <sigh!> (Kim-Ee Yeoh)
   5.  monad transformers and laziness (Dennis Raddle)
   6.  How to read: Pearls of Functional Algorithm      Design (Kim-Ee Yeoh)
   7.  function not working as expected, type issues (Alexej Magura)
   8. Re:  function not working as expected,    type issues (David McBride)


----------------------------------------------------------------------

Message: 1
Date: Tue, 08 Apr 2014 10:21:57 -0500
From: "John M. Dlugosz" <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] What is a "Monad Comprehension" ?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 4/7/2014 4:38 AM, akash g wrote:
> At one point of time, comprehensions were available for all monads.  Then it 
> was removed,
> and has now been added back as a language extension (from the cursory glance 
> that I gave)
>
> Basically, monad comprehensions are about using the samesyntactic sugar for 
> different
> monads (like the do notation)
>
> Perhaps, this'll help (if you haven't seen this already)
>
> https://ghc.haskell.org/trac/ghc/wiki/MonadComprehensions

Thanks for the answer, and for the link to a ghc/wiki in general!




------------------------------

Message: 2
Date: Tue, 08 Apr 2014 10:32:46 -0500
From: "John M. Dlugosz" <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Problem combining monads <sigh!>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

On 4/8/2014 12:54 AM, Kim-Ee Yeoh wrote:
> John,
>
> This is one MAJOR hurdle for newcomers to get over and pretty much everyone 
> stumbles and
> falls into typed combinator enlightenment. Eventually.
 > ?
> If you run through the above against the rest of the list of things that do 
> and don't
> work, I think you'll sew up typed combinators as a fine feather in your cap. 
> Just remember
> that [] is a Functor and also an Applicative and a Monad.

Thanks.  I'll work through that again.

?John




------------------------------

Message: 3
Date: Tue, 8 Apr 2014 13:34:12 -0400
From: Brent Yorgey <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Problem combining monads <sigh!>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Tue, Apr 08, 2014 at 12:54:17PM +0700, Kim-Ee Yeoh wrote:
> 
> Answer: []. Yes, [] is a functor, the way Maybe and IO are functors. So f =
> []. It's unusual but only syntatically in the sense that Haskell says to
> write Maybe Int but rejects [] Int. You have to write [Int] to mean [] Int.

This is not true. In fact, [] Int is perfectly valid Haskell syntax;
it's just not very common.

-Brent


------------------------------

Message: 4
Date: Wed, 9 Apr 2014 02:48:59 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Problem combining monads <sigh!>
Message-ID:
        <CAPY+ZdTu4cx87C_eD75aRNcgTKS=0nPRQAE=bfKpwwkGL=w...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Wed, Apr 9, 2014 at 12:34 AM, Brent Yorgey <[email protected]>wrote:

> This is not true. In fact, [] Int is perfectly valid Haskell syntax;


I stand corrected.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140409/522e185e/attachment-0001.html>

------------------------------

Message: 5
Date: Tue, 8 Apr 2014 14:50:30 -0700
From: Dennis Raddle <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] monad transformers and laziness
Message-ID:
        <cakxlvoonk2q7tzywaoayiy8p0utwbdftrcbkptgc51e8bq5...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

I wrote some code as part of my effort to do backtracking search of musical
structures. Most recently I wrote a function that "pruned" the search tree
by looking for illegal combinations of notes. In the interest of efficiency
I wanted this algorithm to terminate as soon as it encountered an illegal
combination rather than hunt the entire structure for every illegality.
This made me think of the laziness of "or": a function like

g = x || y || z

will not evaluate y and z if x turns out to be true.

I was also writing this code within a stack of monad transformers: State,
ErrorT, and WriterT. This was primarily for debugging reasons: I wanted to
have ErrorT to give good error messages with context, WriterT to log events
for later perusal, and State was there for convenience. I called this monad
stack "Er." So the first thing I wrote was something like this:

-- return True if an illegal combination is found
prune :: Music -> Er Bool
prune music = do
  flags <- forM [0..size music-1] (\ix -> checkOneNote music ix)
  return $ or flags

checkOneNote :: Music -> Int -> Er Bool
checkOneNote music ix = do
  tell $ "Checking note " ++ show ix ++ "\n"
  -- note: were thunks created by following line left unevaluated thanks to
laziness of "or flags" above?
  doSomething music ix

I already knew from past experience that mapM (and forM) evaluate every
computation within the list so I wasn't really expecting this to be lazy,
and it wasn't. Judging from what was "told" to my MonadWriter, I could see
it was checking every note and not stopping early at the first illegal note.

My first question is: by any chance was this an illusion through my use of
"tell"? What I mean, is that was it leaving thunks unevaluated thanks to
the "or flags" expressions lazy behavior, or would it have left them
unevaluated if I hadn't had the "tell" there?

I rewrote this using this function:


pruneRecursive :: Music -> Int -> Int -> Er Bool
pruneRecursive music maxIx ix
  | ix == maxIx = return False
  | do flag <- checkOneNote music maxIx ix
       if flag then return True
               else checkOneNote music maxIx (ix+1)

According to the writer messages, this was not doing any unnecessary work.

Just wondering if it was really necessary to rewrite this. What if I
removed the tell messages later, or put them inside 'when' like this

   when (debugFlag) (tell ..)

and then set debugFlag to False for time-critical program runs?

Dennis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140408/5bc16be3/attachment-0001.html>

------------------------------

Message: 6
Date: Wed, 9 Apr 2014 05:49:06 +0700
From: Kim-Ee Yeoh <[email protected]>
To: "[email protected]" <[email protected]>
Subject: [Haskell-beginners] How to read: Pearls of Functional
        Algorithm       Design
Message-ID:
        <CAPY+ZdT2=zup-aki0mhq5pwrb69lmywbfc6da62-sku_nas...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Bruce earlier emailed about trying to read this book but struggled with not
having basics like fold laws and list induction.

It turns out there's still a lot to gain from the book even if the
mathematical content is out of grasp for now.

I've written up some tips on how to get started:

http://www.atamo.com/blog/how-to-read-pearls-by-richard-bird-1/

The preamble is a bit long but it helps to keep the big picture firmly in
mind.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140409/eb6b2bfc/attachment-0001.html>

------------------------------

Message: 7
Date: Wed, 9 Apr 2014 00:03:54 -0600
From: Alexej Magura <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] function not working as expected, type
        issues
Message-ID: <etPan.5344e2ca.41a7c4c9.2b5e@Gin>
Content-Type: text/plain; charset="utf-8"

K, so I have a function that I?m writing that takes a *string* and a *count* 
and prints the *string* to STDOUT *count* times:

displayD :: IO () -> String -> Int
displayD string count = do
        (count > 0) && hPutStr stdout string
        (count > 0) && displayD string (count - 1)

However, when I try to compile the file, I get several errors; here?s a 
pastebin:?http://pastebin.com/DEsuAvfz

What I?m trying to do here is check to see if *count* is greater than 0, and 
then if it is, then I?d like to print *string* to STDOUT until *count* equals 
0. ?I tried doing it via pattern matching, like so:

displayD string (count > 0) = do

But I haven?t seen any examples of how to do pattern matching in functions 
using *do*, that is *IO*, so I?m unsure if the above is even anywhere near 
correct.

Still very uncomfortable with declaring function types: I have a hard time 
figuring out which type is returned and what type is expected as an arg. ?My 
initial guess is that the *first* type specified is the one returned, but I?m 
not sure.

--?
Alexej Magura
Sent with Airmail


------------------------------

Message: 8
Date: Wed, 9 Apr 2014 02:08:53 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] function not working as expected,
        type issues
Message-ID:
        <CAN+Tr43s9d4=Ug=kx1qxgp4hm3h9rjj+anbyxjdbkvh6orh...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Try using guards:
displayD :: IO () -> String -> Int
displayD string count =
        | count > 0 = hPutStr stdout string
        | otherwise = displayD string (count - 1)

Alternatively use if, then, else.



On Wed, Apr 9, 2014 at 2:03 AM, Alexej Magura <[email protected]> wrote:

> K, so I have a function that I'm writing that takes a *string* and a
> *count* and prints the *string* to STDOUT *count* times:
>
> displayD :: IO () -> String -> Int
> displayD string count = do
>         (count > 0) && hPutStr stdout string
>         (count > 0) && displayD string (count - 1)
>
> However, when I try to compile the file, I get several errors; here's a
> pastebin: http://pastebin.com/DEsuAvfz
>
> What I'm trying to do here is check to see if *count* is greater than 0,
> and then if it is, then I'd like to print *string* to STDOUT until *count*
> equals 0.  I tried doing it via pattern matching, like so:
>
> displayD string (count > 0) = do
>
> But I haven't seen any examples of how to do pattern matching in functions
> using *do*, that is *IO*, so I'm unsure if the above is even anywhere near
> correct.
>
> Still very uncomfortable with declaring function types: I have a hard time
> figuring out which type is returned and what type is expected as an arg.
>  My initial guess is that the *first* type specified is the one returned,
> but I'm not sure.
>
> --
> Alexej Magura
> Sent with Airmail
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140409/52e7b3bc/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 70, Issue 18
*****************************************

Reply via email to