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:  Checking if a Stack is Empty (Emanuel Koczwara)
   2. Re:  Developing Web Applications with Haskell     and Yesod
      (Kim-Ee Yeoh)
   3. Re:  Developing Web Applications with Haskell and Yesod
      (Bryce Verdier)
   4. Re:  Suspend/resume computation using Cont monad and callCC
      (Stephen Tetley)
   5. Re:  Checking if a Stack is Empty (Tom Murphy)
   6. Re:  Suspend/resume computation using Cont monad and callCC
      (Dmitriy Matrosov)


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

Message: 1
Date: Tue, 12 Mar 2013 16:05:19 +0100
From: Emanuel Koczwara <[email protected]>
Subject: Re: [Haskell-beginners] Checking if a Stack is Empty
To: doaltan <[email protected]>, The Haskell-Beginners Mailing List
        -       Discussion of primarily beginner-level topics related to Haskell
        <[email protected]>
Message-ID: <1363100719.4767.3.camel@emanuel-Dell-System-Vostro-3750>
Content-Type: text/plain; charset="UTF-8"

Hi,

Dnia 2013-03-12, wto o godzinie 14:48 +0000, doaltan pisze:
> Hello,
> I have this stack data structure : 
> 
> data Stack = Empty | Element Char Stack deriving Show
> I want to check if it is equal to "Empty"
> When I try something like this : 
> 
> "a = Empty" or "a = (Empty)" in a haskell file and then write this on
> ghci : "a = Empty"
> I get this : 
> 
> <interactive>:1:0:
>     No instance for (Eq Stack)
>       arising from a use of `==' at <interactive>:1:0-11
>     Possible fix: add an instance declaration for (Eq Stack)
>     In the expression: a == (Empty)
>     In the definition of `it': it = a == (Empty)
> I don't know how to fix this. Can you help me so that I can check if a
> stack is Empty without getting this error? 

  First, it looks like you should read 2 or 3 times this book:
http://learnyouahaskell.com/

  Second, you should use a function like this:

isEmpty :: Stack -> Bool
isEmpty Empty = True
isEmpty _ = False

Emanuel






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

Message: 2
Date: Tue, 12 Mar 2013 22:11:10 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] Developing Web Applications with
        Haskell and Yesod
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <capy+zdqfmvkjl9uvrugjtjgphoo4ygz0nhwzh3ve5pspj6g...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hey Patrick,

On Tue, Mar 12, 2013 at 9:47 PM, KMandPJLynch <[email protected]>wrote:

> I sent a previous email to you in regard to this - if my request is
> rejected, will I be notified?
>

Typically, if no one responds to your post, either they are too busy or
they don't feel they can add constructively to the list (which is skimmed,
if not actually read by hundreds or thousands -- yep, every single email).

I'm reading the book "Developing Web Applications with Haskell and Yesod".
> It is a very interesting read and I'm hoping to be able to be able to put
> up a simple web app using it as a result.
> I was wondering if anyone has had experience with it.
>

There's a google group dedicated to yesod, I believe. The yesod website has
links to community resources specially dedicated to the web framework.

And you're always welcome to post specific queries about general haskell
here! Your previous questions about learning category theory generated
pretty useful discussion. :)

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

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

Message: 3
Date: Tue, 12 Mar 2013 09:53:09 -0700
From: Bryce Verdier <[email protected]>
Subject: Re: [Haskell-beginners] Developing Web Applications with
        Haskell and Yesod
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

Hey Patrick,

I have very limited expirence with Yesod. I too have read the book but I 
found that the dead tree version isn't as up to date as the online 
version. If code examples don't work from book, I would look online 
instead, you can find it here:

http://www.yesodweb.com/book

<shameless plug>Going back to what I said about my minimal expirence 
with it, I actually have a 2 part blog post where I perform a small web 
project in both Flask and Yeson. Here is the link to the Yesod site:
http://scrollingtext.org/my-first-yesod-app
</shameless plug>

It's a neat framework, and I hope to spend more time with it in the future.

Bryce

On 03/12/2013 08:11 AM, Kim-Ee Yeoh wrote:
> Hey Patrick,
>
> On Tue, Mar 12, 2013 at 9:47 PM, KMandPJLynch 
> <[email protected] <mailto:[email protected]>> wrote:
>
>     I sent a previous email to you in regard to this - if my request
>     is rejected, will I be notified?
>
>
> Typically, if no one responds to your post, either they are too busy 
> or they don't feel they can add constructively to the list (which is 
> skimmed, if not actually read by hundreds or thousands -- yep, every 
> single email).
>
>     I'm reading the book "Developing Web Applications with Haskell and
>     Yesod".
>     It is a very interesting read and I'm hoping to be able to be able
>     to put up a simple web app using it as a result.
>     I was wondering if anyone has had experience with it.
>
>
> There's a google group dedicated to yesod, I believe. The yesod 
> website has links to community resources specially dedicated to the 
> web framework.
>
> And you're always welcome to post specific queries about general 
> haskell here! Your previous questions about learning category theory 
> generated pretty useful discussion. :)
>
> -- Kim-Ee
>
>
> _______________________________________________
> 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/20130312/a193f259/attachment-0001.htm>

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

Message: 4
Date: Tue, 12 Mar 2013 17:54:16 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Suspend/resume computation using Cont
        monad and callCC
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <cab2tprbhyvslzgs+csze86ez2robqwflwxzluzniv7gowrc...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

The resumption monad is even simpler, unfortunately I'm not aware of any
beginner level tutorials.

William Harrison at the University of Missouri has some papers introducing
resumption monads but the presentations then move very quickly.



On 12 March 2013 11:53, Ertugrul S?ylemez <[email protected]> wrote:

>
> Not directly answering your question, but what you need is called
> coroutines, and there are better monads for that purpose.  This is how
> the Cont monads are defined:
>
>     newtype Cont r a = Cont ((a -> r) -> r)
>
> But what you really need here is called a Coroutine monad:
>
>     newtype Coroutine f a = Coroutine (Either (f (Coroutine f a)) a)
>
> Don't worry about that scary type, because if you look closely you will
> find that this is just Free as defined in the 'free' package:
>
>     data Free f a
>         = Free (f (Free f a))
>         | Pure a
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130312/1c1206d8/attachment-0001.htm>

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

Message: 5
Date: Tue, 12 Mar 2013 11:13:00 -0700
From: Tom Murphy <[email protected]>
Subject: Re: [Haskell-beginners] Checking if a Stack is Empty
To: doaltan <[email protected]>,      The Haskell-Beginners Mailing List
        - Discussion of primarily       beginner-level topics related to Haskell
        <[email protected]>
Message-ID:
        <cao9q0txwrjtukykfwid3vx0far8md83tuuwreijrnnewtnt...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

This chapter of Real World Haskell exactly explains the problem you're having:

http://book.realworldhaskell.org/read/using-typeclasses.html

(The short answer is, you need an `Eq` instance for the data type
you've created (`Stack`). You can just use `deriving`.)

Tom


On Tue, Mar 12, 2013 at 7:48 AM, doaltan <[email protected]> wrote:
> Hello,
> I have this stack data structure :
> data Stack = Empty | Element Char Stack deriving Show
> I want to check if it is equal to "Empty"
> When I try something like this :
> "a = Empty" or "a = (Empty)" in a haskell file and then write this on ghci :
> "a = Empty"
> I get this :
> <interactive>:1:0:
>     No instance for (Eq Stack)
>       arising from a use of `==' at <interactive>:1:0-11
>     Possible fix: add an instance declaration for (Eq Stack)
>     In the expression: a == (Empty)
>     In the definition of `it': it = a == (Empty)
> I don't know how to fix this. Can you help me so that I can check if a stack
> is Empty without getting this error?
> Thank you.
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 6
Date: Wed, 13 Mar 2013 00:16:05 +0400
From: Dmitriy Matrosov <[email protected]>
Subject: Re: [Haskell-beginners] Suspend/resume computation using Cont
        monad and callCC
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

> On Tue, 12 Mar 2013 12:53:37 +0100
> Ertugrul S?ylemez <[email protected]> wrote:
> 
> > Dmitriy Matrosov <[email protected]> wrote:
> >
> > I have two functions f and g, and i want them to execute in
> > following order: first function f runs, then suspends and passes
> > control to function g. Function g runs, then suspends and
> > "unpauses" function f. Function f finishes and passes control to
> > function g, which also finishes. Here is illustration ('o' means
> > start of function, dot means suspend and pass control to other
> > function, 'x' means end of function):
> >
> > [...]
> >
> > I want to implement this using Cont monad and callCC.
> 
> Not directly answering your question, but what you need is called
> coroutines, and there are better monads for that purpose.  This is how
> the Cont monads are defined:
> 
>     newtype Cont r a = Cont ((a -> r) -> r)
> 
> But what you really need here is called a Coroutine monad:
> 
>     newtype Coroutine f a = Coroutine (Either (f (Coroutine f a)) a)
> 
> Don't worry about that scary type, because if you look closely you
> will find that this is just Free as defined in the 'free' package:
> 
>     data Free f a
>         = Free (f (Free f a))
>         | Pure a
> 

> On Tue, 12 Mar 2013 17:54:16 +0000
> Stephen Tetley <[email protected]> wrote:
> 
> The resumption monad is even simpler, unfortunately I'm not aware of
> any beginner level tutorials.
> 
> William Harrison at the University of Missouri has some papers
> introducing resumption monads but the presentations then move very
> quickly.

Thanks for suggestions! I'll try them (though, i suppose, to understand Free i
should read at least something about category theory first).

Anyway, i think, that i understand why my implementation works so and
can explain it.

First, i want to remind the implementation of callCC (omiting Cont
wrapper):

    callCC f        = \k -> let g x = \_ -> k x
                            in  (f g) k

So, actually, callCC provides to function f continuation to monad
following callCC itself. This will be the key in my explanation.

Let's start with first question and explain how fT/gT pair works. Here
is the code from my previous mail with line-numbers:

      type T r            = ContT r (Writer String)

    1   fT :: T r ()
    2   fT                  = do
    3       lift $ tell "I'm in f-1\n"
    4       k' <- callCC gT
    5       lift $ tell "I'm in f-2\n"
    6       k' undefined
    7       lift $ tell "I'm in f-3\n"
    8
    9   gT :: ((() -> T r ()) -> T r ()) -> T r (() -> T r ())
    10  gT k                = do
    11      lift $ tell "I'm in g-1\n"
    12      callCC k
    13      lift $ tell "I'm in g-2\n"
    14      return (\_ -> return ())


And here is illustrations of first part of execution:

      |
      v
  3 'f-1'
  4 k' <- callCC gT <--------------------------------
            \                                       |
             -------> 11 'g-1'                      |
                      12 callCC k                   |
                    /                               |
  5 'f-2'  <--------                                |
  6 k' -------------> 13 'g-2'                      |
                      14 return (\_ -> return ()) ---

I.e. after point 'f-1' function gT is called with continuation k to
line 5. Then after point 'g-1' function gT calls this continuation k
and execution jumps back to line 5 in function f. But because
continuation k have been called in callCC, callCC throws continuation
k' to line 13 (in function g) into continuation k.  Then after point
'f-2' function f calls continuation k' to line 13. Function g resumes
execution and finishes. But what is the next continuation now? The one
supplied by (callCC gT) ! And this is again continuation to line 5 in
function f. Here i proceed to second illustration:

  3 'f-1'
  4 k' <- callCC gT <---------------------------------
  5 'f-2'                                            |
  6 k'                 13 'g-2'                      |
  7 'f-3'              14 return (\_ -> return ()) ---

So, function f executes again from point 'f-2'.  And meet continuation
k' again, but now k' is continuation returned by function gT at line
14. I.e. it is just (\_ -> return ()). Thus, it does nothing and i
proceed to point 'f-3'.

This explains track produced by Writer monad. But there is one more
question: why track produced using monad result differs?

Here is the code from my previous mail:

    type M r            = Cont r

    fM :: M r [String]
    fM                  = do
        let xs' = "I'm in f-1" : []
        (ys, k') <- callCC (gM xs')
        let ys' = "I'm in f-2" : ys
        zs <- k' ys'
        let zs' = "I'm in f-3" : zs
        return zs'

    gM :: [String]
          -> (([String], [String] -> M r [String]) -> M r [String])
          -> M r ([String], [String] -> M r [String])
    gM xs k             = do
        let xs' = "I'm in g-1" : xs
        ys <- callCC (curry k xs')
        let ys' = "I'm in g-2" : ys
        return (ys', \_ -> return ys')

And if you "trace" its execution in the same manner, you'll notice the
answer: result of monad fM actually determined by call of continuation
k', which occurs during "second" function fM run. And during this
"second" run continuation k' will be (\_ -> return ys'), where ys' is
from function gM. But when ys' had been evaluated in function gM,
second pass through 'f-2' not yet happen! That's why it is missed from
the result as well.

Ugh, well.. that was useless, but still so fascinating :-)

--
    Dmitriy Matrosov





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

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


End of Beginners Digest, Vol 57, Issue 20
*****************************************

Reply via email to