Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Using Maybe monad with Zipper (Hugo Ferreira)
   2. Re:  Combinations of choosing n items from a list (Lorenzo Bolla)
   3.  (no subject) (Alia)


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

Message: 1
Date: Mon, 14 Nov 2011 11:02:49 +0000
From: Hugo Ferreira <h...@inescporto.pt>
Subject: [Haskell-beginners] Using Maybe monad with Zipper
To: beginners@haskell.org
Message-ID: <4ec0f559.1090...@inescporto.pt>
Content-Type: text/plain; charset=ISO-8859-15; format=flowed

Hello,

I picked up code that originally had the following function:

leftCursor :: Z.Zipper a -> Maybe a
leftCursor z = if Z.beginp z then
                    Nothing
                else
                    Z.safeCursor $ Z.left z

However now I need a functions that returns all three elements
to the left of the zipper. I then considered using composition
and came up with the following:

left :: Maybe (Z.Zipper a) -> Maybe (Z.Zipper a)
left (Just z) = if Z.beginp z
               then
                 Nothing
               else
                 Just $ Z.left z
left Nothing = Nothing

cursor :: Maybe (Z.Zipper a) -> Maybe a
cursor (Just z) = Z.safeCursor z
cursor Nothing = Nothing

which I then used as follows:

instPrev1Of3TagRule :: POSTags -> Maybe TransformationRule
instPrev1Of3TagRule z = do
     (_, _, prev1)           <- leftCursor z
     (_, _, prev2)           <- (cursor . left . return) z
     (_, _, prev3)           <- (cursor . left . left . return) z
     (_, correct, incorrect) <- Z.safeCursor z
     return $ Prev1Of3TagRule (Replacement incorrect correct) prev1 
prev2 prev3

But I was not happy with this for two reasons:

1. I was repeating work needlessly. I should pick up the last
    zipper and just move the cursor again once for the next
    character.

2. I was not taking advantage of the Maybe Monad

So I finally changed the above to:

left :: Maybe (Z.Zipper a) -> Maybe (Z.Zipper a, a)
left (Just z) = if Z.beginp z
               then
                 Nothing
               else
                 Just $ (Z.left z, Z.cursor z)
left Nothing = Nothing

instPrev1Of3TagRule :: POSTags -> Maybe TransformationRule
instPrev1Of3TagRule z = do
     (z1,(_, _, prev1))      <- (left . return) z
     (z2, (_, _, prev2))     <- (left . return) z1
     (_z3, (_, _, prev3))    <- (left . return) z2
     (_, correct, incorrect) <- Z.safeCursor z
     return $ Prev1Of3TagRule (Replacement incorrect correct) prev1 
prev2 prev3

So my question is, is this correct? Can the above code be made
simpler/cleaner? Somehow the double use of Maybe seems wrong.

TIA,
Hugo F.







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

Message: 2
Date: Mon, 14 Nov 2011 11:02:56 +0000
From: Lorenzo Bolla <lbo...@gmail.com>
Subject: Re: [Haskell-beginners] Combinations of choosing n items from
        a list
To: peter.h...@memorphic.com
Cc: beginners@haskell.org
Message-ID:
        <CADjgTRy=xy2x2gawzmt5yw_fwjepxa0ynpbl22a-iuvpzqx...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

What about something like:

combinations n = filter (\s -> length s == n) . subsequences

hth,
L.



On Sun, Nov 13, 2011 at 10:42 PM, Peter Hall <peter.h...@memorphic.com>wrote:

> Hi all,
> This function took me a long time to write, getting my head around the
> double recursion. It returns a list of all possible sub-sets of a
> given length from a list.
>
> I have a couple of questions.
>
> 1. Can it be improved for efficiency or style?
> 2. Is there a library that already has this and other related
> functions? I assumed there would be but I couldn't find it on hoogle.
>
>
> combinations :: Int -> [a] -> [[a]]
> combinations _ [] = []
> combinations 0 _ = []
> combinations 1 x = map (:[]) x
> combinations n (x:xs) = (map (x:) $ combinations (n-1) xs) ++ combinations
> n xs
>
> e.g.
> > combinations 3 [1..5]
>
> [[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5],[3,4,5]]
>
> Thanks,
> Peter
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111114/78658a4f/attachment-0001.htm>

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

Message: 3
Date: Mon, 14 Nov 2011 05:06:21 -0800 (PST)
From: Alia <alia_kho...@yahoo.com>
Subject: [Haskell-beginners] (no subject)
To: beginners@haskell.org, esimo...@free.fr,
        goflow-us...@googlegroups.com,  nkol...@yahoo.co.uk,
        n-o-u-r-...@hotmail.com
Message-ID:
        <1321275981.14695.yahoomailmob...@web65711.mail.ac4.yahoo.com>
Content-Type: text/plain; charset="us-ascii"

http://nuevaesperanzaparatodos.com/modules/mod_wdbanners/site.php?html1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111114/b8143904/attachment-0001.htm>

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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


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

Reply via email to