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: How to think like a functional programmer? (KC)
2. How to make a kinder, gentler "fail"? (Sean Perry)
3. Re: How to make a kinder, gentler "fail"? (Elvio Toccalino)
4. Re: How to think like a functional programmer? (Henning Diedrich)
5. Re: How to make a kinder, gentler 'fail'? (Sean Perry)
6. Re: How to think like a functional programmer? (Henning Diedrich)
7. Re: How to make a kinder, gentler 'fail'? (Elvio Toccalino)
----------------------------------------------------------------------
Message: 1
Date: Thu, 26 May 2011 10:55:25 -0700
From: KC <[email protected]>
Subject: Re: [Haskell-beginners] How to think like a functional
programmer?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252
> On 26 May 2011 17:27, Costello, Roger L. <[email protected]> wrote:
>>
>> How did you ?flip the switch? in your brain to the functional mindset?
>
What I found important is the following:
- to make your functions declaritive, then you can improve on them later
- do what Geraint Jones has called "wholemeal programming"; it helps
prevent a disease called "indexitis" and encourages lawful program
construction (from "Pearls of Functional Algorithm Design", Richard
Bird, 2010)
--
--
Regards,
KC
------------------------------
Message: 2
Date: Thu, 26 May 2011 11:15:36 PDT
From: Sean Perry <[email protected]>
Subject: [Haskell-beginners] How to make a kinder, gentler "fail"?
To: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
This is probably a FAQ of sorts, but I found composing the proper search terms
complicated.
When I write simple parsers and the like I tend to prefer returning useful error
strings instead of simply Nothing from Maybe.
For example this is a common utility function of mine. Yes, I know with the
addition of a Read n qualification I can make this more
generic but it does not help with the current discussion.
getNum :: String -> Either String Int
getNum n = case reads n of [(x, "")] -> Right x
[(x, cs)] -> Left $ "incomplete parse: " ++ cs
cs -> Left $ "invalid number: " ++ cs
But I would rather write the following so I am not bound to Either. This would
even work with Maybe since Nothing just drops the string from fail.
getNum :: Monad m => String -> m Int
getNum n = case reads n of [(x, "")] -> return x
[(x, cs)] -> fail $ "incomplete parse: " ++ cs
cs -> fail $ "invalid number: " ++ cs
Yeah, I know, no one likes the fact that fail raises an exception. What I would
like to do in my code is define something like
class (Monad a) => MonadGentle a where
myreturn = return
myfail s = fails s
But I can not get an instance of this to compile because it insists myreturn and
myfail are not visible.
Since this comes up a lot in the tutorials and books, I am curious why there is
not something like MonadGentle in Hackage or the libs. I use mzero occasionally,
but as I said I usually prefer some information with my errors since it makes
for
more human usable results.
Thanks.
------------------------------
Message: 3
Date: Thu, 26 May 2011 15:24:25 -0300
From: Elvio Toccalino <[email protected]>
Subject: Re: [Haskell-beginners] How to make a kinder, gentler "fail"?
To: Sean Perry <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <1306434268.3680.1.camel@el-fierro>
Content-Type: text/plain; charset="UTF-8"
Well, there's the 'failure' package. Another package builds on top of
it, the control-monad-failure (easier to use, maybe).
I read in the monad reader (don't remember the issue number) about it,
and have used it ever since. The idea is to let the instanced monad
decide what a failure means.
Check it out, and comment back.
Cheers.
On Thu, 2011-05-26 at 11:15 -0700, Sean Perry wrote:
> This is probably a FAQ of sorts, but I found composing the proper search terms
> complicated.
>
> When I write simple parsers and the like I tend to prefer returning useful
> error
> strings instead of simply Nothing from Maybe.
>
> For example this is a common utility function of mine. Yes, I know with the
> addition of a Read n qualification I can make this more
> generic but it does not help with the current discussion.
>
> getNum :: String -> Either String Int
> getNum n = case reads n of [(x, "")] -> Right x
> [(x, cs)] -> Left $ "incomplete parse: " ++ cs
> cs -> Left $ "invalid number: " ++ cs
>
> But I would rather write the following so I am not bound to Either. This would
> even work with Maybe since Nothing just drops the string from fail.
>
> getNum :: Monad m => String -> m Int
> getNum n = case reads n of [(x, "")] -> return x
> [(x, cs)] -> fail $ "incomplete parse: " ++ cs
> cs -> fail $ "invalid number: " ++ cs
>
> Yeah, I know, no one likes the fact that fail raises an exception. What I
> would
> like to do in my code is define something like
>
> class (Monad a) => MonadGentle a where
> myreturn = return
> myfail s = fails s
>
> But I can not get an instance of this to compile because it insists myreturn
> and
> myfail are not visible.
>
> Since this comes up a lot in the tutorials and books, I am curious why there
> is
> not something like MonadGentle in Hackage or the libs. I use mzero
> occasionally,
> but as I said I usually prefer some information with my errors since it makes
> for
> more human usable results.
>
> Thanks.
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 4
Date: Thu, 26 May 2011 20:31:33 +0200
From: Henning Diedrich <[email protected]>
Subject: Re: [Haskell-beginners] How to think like a functional
programmer?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
On 5/26/11 6:27 PM, Costello, Roger L. wrote:
>
> Hi Folks,
>
> I am working on a project and am trying hard to write the code in a
> functional style.
>
> Sadly, upon reviewing what I've written, I realize that I am not
> succeeding. I just devised a recipe for solving the problem and then
> created functions corresponding to steps in the recipe. I am still
> thinking imperatively, not functionally.
>
> Is there a book or article that describes how to approach problems
> from a functional mindset?
>
> How did you "flip the switch" in your brain to the functional mindset?
>
Hi Roger,
reminding oneself to take it literal, I found can be quite clarifying.
At the core of it, I found it helps to "avoid thinking state", i.e.
putting stuff into variables. If you find yourself doing that a lot,
it's a sign for the wrong track. You'll find, again and again, you don't
need to keep that state. Just pass up the result of a call.
Inversely, avoid "lines that do not deliver a result", which "just do
something". Or, to be precise, note when you don't use a result.
Taking "functional programming" literal, you'd try thinking about the
entire problem as but nested function calls with no intermediary state
holders, rather than "implicitly tacting state tables", if that makes
any sense.
Best,
Henning
> /Roger
>
>
> _______________________________________________
> 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/20110526/cc6cb43a/attachment-0001.htm>
------------------------------
Message: 5
Date: Thu, 26 May 2011 12:19:13 PDT
From: Sean Perry <[email protected]>
Subject: Re: [Haskell-beginners] How to make a kinder, gentler 'fail'?
To: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Yes, this sounds like what I want but I can not get it to work how I want (-:
Monad Reader 15 is where this is discussed. Apparently MonadFailure has been
replaced with Failure.
>From M.R 15 here is the example:
safeHeadFailure :: Failure String m => [a] -> m a
safeHeadFailure [] = failure "empty list"
safeHeadFailure (x:xs) = return x
safeHead' :: [a] -> Maybe a
safeHead' = safeHeadFailure
and sure enough, safeHead' works in Maybe. But if I change it to
safeHead' :: [a] -> Either String a
which is more like what will happen in real code I get:
No instance for (Failure String (Either String))
arising from a use of `safeHeadFailure'
Possible fix:
add an instance declaration for (Failure String (Either String))
In the expression: safeHeadFailure
In an equation for `safeHead'': safeHead' = safeHeadFailure
Yes, I installed control-monad-failure-mtl and yes I have a 'import
Control.Monad.Error' directive in the module.
This is straight from the article:
safeHeadPair' :: Failure String m => [a] -> [b] -> m (a, b)
safeHeadPair' [] [] = failure "both lists empty"
safeHeadPair' [] _ = failure "first list empty"
safeHeadPair' _ [] = failure "second list empty"
safeHeadPair' (x:xs) (y:ys) = return (x, y)
safeHead2 :: [a] -> [b] -> Either String (a, b)
safeHead2 = safeHeadPair'
I get the same failure here.
What am I missing?
On Thu May 26 11:24 , Elvio Toccalino sent:
>Well, there's the 'failure' package. Another package builds on top of
>it, the control-monad-failure (easier to use, maybe).
>I read in the monad reader (don't remember the issue number) about it,
>and have used it ever since. The idea is to let the instanced monad
>decide what a failure means.
>Check it out, and comment back.
>
>Cheers.
>
>On Thu, 2011-05-26 at 11:15 -0700, Sean Perry wrote:
>> This is probably a FAQ of sorts, but I found composing the proper search
>> terms
>> complicated.
>>
>> When I write simple parsers and the like I tend to prefer returning useful
>> error
>> strings instead of simply Nothing from Maybe.
>>
>> For example this is a common utility function of mine. Yes, I know with the
>> addition of a Read n qualification I can make this more
>> generic but it does not help with the current discussion.
>>
>> getNum :: String -> Either String Int
>> getNum n = case reads n of [(x, "")] -> Right x
>> [(x, cs)] -> Left $ "incomplete parse: " ++ cs
>> cs -> Left $ "invalid number: " ++ cs
>>
>> But I would rather write the following so I am not bound to Either. This
>> would
>> even work with Maybe since Nothing just drops the string from fail.
>>
>> getNum :: Monad m => String -> m Int
>> getNum n = case reads n of [(x, "")] -> return x
>> [(x, cs)] -> fail $ "incomplete parse: " ++ cs
>> cs -> fail $ "invalid number: " ++ cs
>>
>> Yeah, I know, no one likes the fact that fail raises an exception. What I
>> would
>> like to do in my code is define something like
>>
>> class (Monad a) => MonadGentle a where
>> myreturn = return
>> myfail s = fails s
>>
>> But I can not get an instance of this to compile because it insists myreturn
>> and
>> myfail are not visible.
>>
>> Since this comes up a lot in the tutorials and books, I am curious why there
>> is
>> not something like MonadGentle in Hackage or the libs. I use mzero
>> occasionally,
>> but as I said I usually prefer some information with my errors since it
>> makes for
>> more human usable results.
>>
>> Thanks.
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
Message: 6
Date: Thu, 26 May 2011 22:00:40 +0200
From: Henning Diedrich <[email protected]>
Subject: Re: [Haskell-beginners] How to think like a functional
programmer?
To: KC <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="windows-1252"; Format="flowed"
On 5/26/11 7:55 PM, KC wrote:
>> On 26 May 2011 17:27, Costello, Roger L.<[email protected]> wrote:
>>> How did you ?flip the switch? in your brain to the functional mindset?
> What I found important is the following:
> - to make your functions declaritive, then you can improve on them later
> - do what Geraint Jones has called "wholemeal programming"; it helps
> prevent a disease called "indexitis" and encourages lawful program
> construction (from "Pearls of Functional Algorithm Design", Richard
> Bird, 2010)
"Functional languages excel at wholemeal programming, a term
coined by Geraint Jones. Wholemeal programming means to think
big: work with an entire list, rather than a sequence of elements; develop
a solution space, rather than an individual solution; imagine
a graph, rather than a single path. The wholemeal approach often
offers new insights or provides new perspectives on a given problem.
It is nicely complemented by the idea of projective programming:
first solve a more general problem, then extract the interesting
bits and pieces by transforming the general program into more
specialised ones."
http://www.comlab.ox.ac.uk/ralf.hinze/publications/ICFP09.pdf
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110526/892bac91/attachment-0001.htm>
------------------------------
Message: 7
Date: Thu, 26 May 2011 17:05:23 -0300
From: Elvio Toccalino <[email protected]>
Subject: Re: [Haskell-beginners] How to make a kinder, gentler 'fail'?
To: Sean Perry <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <1306440326.3680.8.camel@el-fierro>
Content-Type: text/plain; charset="UTF-8"
Just checked the package doc, and apparently there's no instance of
Failure for the (Either String) monad. For that case 'failure = Left',
but looking at the docs I see the following (cryptic, to me) comment:
-- not a monad or applicative instance Failure e (Either e) where
failure = Left
(I suppose It's there because that 'e' type variable is not bound to
String, as we'd like)
BUT! I also see that (Either e) is an instance of the (similar?) Try
type class:
class Try f where
type Error f
-- Turn a concrete failure into an abstract failure
try :: Failure (Error f) f' => f a -> f' a
instance Try (Either e) where
type Error (Either e) = e
try (Left e) = failure e
try (Right x) = return x
Through this Try instance, you should be able to 'try action', where
action has 'Either String some' type, and the failures you throw in such
action would be translated to Left's.
On Thu, 2011-05-26 at 12:19 -0700, Sean Perry wrote:
> Yes, this sounds like what I want but I can not get it to work how I want (-:
>
> Monad Reader 15 is where this is discussed. Apparently MonadFailure has been
> replaced with Failure.
> From M.R 15 here is the example:
> safeHeadFailure :: Failure String m => [a] -> m a
> safeHeadFailure [] = failure "empty list"
> safeHeadFailure (x:xs) = return x
>
> safeHead' :: [a] -> Maybe a
> safeHead' = safeHeadFailure
>
> and sure enough, safeHead' works in Maybe. But if I change it to
>
> safeHead' :: [a] -> Either String a
>
> which is more like what will happen in real code I get:
>
> No instance for (Failure String (Either String))
> arising from a use of `safeHeadFailure'
> Possible fix:
> add an instance declaration for (Failure String (Either String))
> In the expression: safeHeadFailure
> In an equation for `safeHead'': safeHead' = safeHeadFailure
>
> Yes, I installed control-monad-failure-mtl and yes I have a 'import
> Control.Monad.Error' directive in the module.
>
> This is straight from the article:
>
> safeHeadPair' :: Failure String m => [a] -> [b] -> m (a, b)
> safeHeadPair' [] [] = failure "both lists empty"
> safeHeadPair' [] _ = failure "first list empty"
> safeHeadPair' _ [] = failure "second list empty"
> safeHeadPair' (x:xs) (y:ys) = return (x, y)
>
> safeHead2 :: [a] -> [b] -> Either String (a, b)
> safeHead2 = safeHeadPair'
>
> I get the same failure here.
>
> What am I missing?
>
> On Thu May 26 11:24 , Elvio Toccalino sent:
>
> >Well, there's the 'failure' package. Another package builds on top of
> >it, the control-monad-failure (easier to use, maybe).
> >I read in the monad reader (don't remember the issue number) about it,
> >and have used it ever since. The idea is to let the instanced monad
> >decide what a failure means.
> >Check it out, and comment back.
> >
> >Cheers.
> >
> >On Thu, 2011-05-26 at 11:15 -0700, Sean Perry wrote:
> >> This is probably a FAQ of sorts, but I found composing the proper search
> >> terms
> >> complicated.
> >>
> >> When I write simple parsers and the like I tend to prefer returning useful
> >> error
> >> strings instead of simply Nothing from Maybe.
> >>
> >> For example this is a common utility function of mine. Yes, I know with the
> >> addition of a Read n qualification I can make this more
> >> generic but it does not help with the current discussion.
> >>
> >> getNum :: String -> Either String Int
> >> getNum n = case reads n of [(x, "")] -> Right x
> >> [(x, cs)] -> Left $ "incomplete parse: " ++ cs
> >> cs -> Left $ "invalid number: " ++ cs
> >>
> >> But I would rather write the following so I am not bound to Either. This
> >> would
> >> even work with Maybe since Nothing just drops the string from fail.
> >>
> >> getNum :: Monad m => String -> m Int
> >> getNum n = case reads n of [(x, "")] -> return x
> >> [(x, cs)] -> fail $ "incomplete parse: " ++ cs
> >> cs -> fail $ "invalid number: " ++ cs
> >>
> >> Yeah, I know, no one likes the fact that fail raises an exception. What I
> >> would
> >> like to do in my code is define something like
> >>
> >> class (Monad a) => MonadGentle a where
> >> myreturn = return
> >> myfail s = fails s
> >>
> >> But I can not get an instance of this to compile because it insists
> >> myreturn and
> >> myfail are not visible.
> >>
> >> Since this comes up a lot in the tutorials and books, I am curious why
> >> there is
> >> not something like MonadGentle in Hackage or the libs. I use mzero
> >> occasionally,
> >> but as I said I usually prefer some information with my errors since it
> >> makes for
> >> more human usable results.
> >>
> >> Thanks.
> >>
> >>
> >> _______________________________________________
> >> Beginners mailing list
> >> [email protected]
> >> http://www.haskell.org/mailman/listinfo/beginners
> >
> >
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 35, Issue 52
*****************************************