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 make a kinder, gentler "fail"? (Adam Foltzer)
   2. Re:  How to think like a functional programmer?
      (Ertugrul Soeylemez)
   3. Re:  How does one uninstall a Hackage package? :)
      (Michael Litchard)
   4.  Simple UTF-8 output (Christopher Howard)
   5. Re:  Simple UTF-8 output (Elvio Toccalino)


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

Message: 1
Date: Thu, 26 May 2011 16:43:32 -0400
From: Adam Foltzer <[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="iso-8859-1"

Hi Sean,

The MonadError class in mtl might be what you're looking for:
http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Error-Class.html#t:MonadError

Here's what I came up with for your particular example, along with tests in
both the (Either String) monad and the IO monad:
http://hpaste.org/47101/monaderror_example

Note that if you want to use Maybe, you'll have to make a custom instance of
MonadError for Maybe, since it doesn't exist by default.

Cheers,
Adam

On Thu, May 26, 2011 at 2:15 PM, Sean Perry <[email protected]> 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
>


On Thu, May 26, 2011 at 2:24 PM, Elvio Toccalino
<[email protected]>wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110526/c7ce62d1/attachment-0001.htm>

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

Message: 2
Date: Thu, 26 May 2011 22:58:51 +0200
From: Ertugrul Soeylemez <[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=US-ASCII

"Costello, Roger L." <[email protected]> wrote:

> 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?

I don't know whether there is a book, but I can tell you how I did it.
Just like you and most others I came from an imperative background.
First of all, don't try too hard.  It can be very frustrating, when you
are a beginner.  There is nothing wrong with writing nonidiomatic code
at the beginning.

I will give you some basic rules you can follow to get fluent in
Haskell:

  * Understand the syntax

    It happens to a lot of newbies that they get overwhelmed and totally
    confused by the number of symbols, which they have never seen before
    in other languages.  Don't panic!  Most of these symbols are
    ordinary functions.  If you disregard those, you will have a handful
    of symbols left, likely even less than in other languages.  Most of
    those are just syntactic sugar for, again, regular functions and
    function applications.  Haskell is an incredibly simple language at
    its core.

  * Understand the basics of the type system

    Haskell's type system is very sophisticated and can take months or
    years to understand fully, especially when you start working with
    some of the type system extensions.  But this pays off!  Once you
    know how to read and write type signatures you will find that most
    functions together with their type signatures are fairly
    self-explanatory.  I constantly learn the interface of new libraries
    by just looking at the types.

    The type system will also improve the safety of your programs up to
    the point where you will experience a strange sensation:  Once your
    program passes the compiler, it usually works!

    Finally you will find that it is advantageous to write a type
    signature, before writing the corresponding definition.  For example
    for a function to add two integers using only increment (succ) and
    decrement (pred), first write its type signature:

        add :: Integer -> Integer -> Integer

    And only after that write its definition:

        add x 0 = x
        add x y = add (succ x) (pred y)

    Firstly this will prepare your mind for the implementation, because
    you know what structure the function has to have.  Secondly it will
    prevent a broad class of bugs.

  * Know your tools

    This is about the most important rule, because in Haskell you have
    an incredible amount of abstractions and design patterns, which you
    simply cannot have in most other languages.  The introduction of
    laziness alone gives you many new design patterns you probably have
    never seen before.

    Of course in the beginning you will use the tools you know from
    other languages, as far as applicable to Haskell.  But the more you
    work with it, the more you will learn the 'other' tools.  And once
    you learned them, other languages will really annoy you. ;)

  * Understand the language semantics

    Finally this last point will give you the in-depth understanding of
    what is going on under the hood.  It will help you understand the
    tools you are using.  You will learn what "non-strict" actually
    means, what the "bottom" value is, how to prevent stack/heap
    overflows, etc.

Now don't just search for tutorials or books.  Instead, write
applications!  Haskell can be learned best by writing actual code.  I
will give you a very interesting problem to begin with.

The n-th Mersenne number is 2^n - 1.  There are certain Mersenne
numbers, which are prime numbers.  For this kind of numbers there exists
a fast primality test, which is also used in the large distributed prime
number searches like GIMPS.  It's called the Lucas-Lehmer primality test
and involves generating an infinite sequence of numbers and picking a
certain element from it.

This task is not just a great opportunity to learn functional thinking,
but in particular Haskell thinking, because you would solve the problem
differently in other functional languages.

In an imperative approach you would write a loop, which destructively
updates a variable a certain number of times, before reading its value.
The first translation of this to Haskell is to write a recursive
function, which uses function parameters and recursion as a replacement
for destructively updated variables.  This is not really an improvement,
as you will write essentially the same code, but with recursive calls
instead of destructive updates.  You got rid of the side effects, but
not of the general imperative idea of the computation.

Now comes the most important third point:  Know your tools!  In this
case you have a tool, which you won't find in most other languages:
infinite data structures, in particular infinite lists.  Using the
'iterate' combinator you can make a direct translation of this infinite
sequence.  You write an infinite list.  Then using the (!!) function you
just pick the element you want.  This has two advantages:  Firstly the
code is a straightforward translation of the mathematical definition of
the algorithm.  Secondly and more importantly you have a separation of
concerns here:  You have the infinite list.  Independent of that you
have the function to pick an element from it.  Independent of both you
have an IO computation to display the result.  This makes your code very
composable.

With this solution you have also used a second design pattern, which you
won't find in the mainstream languages:  corecursion.  At some point you
will learn to use these tools and design patterns more consciously.
This is your road to mastering Haskell.


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/





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

Message: 3
Date: Thu, 26 May 2011 18:07:48 -0700
From: Michael Litchard <[email protected]>
Subject: Re: [Haskell-beginners] How does one uninstall a Hackage
        package? :)
To: Ertugrul Soeylemez <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

There is a package on Hackage called cab. Take a look, it can remove
packages and more!

On Wed, May 25, 2011 at 6:39 AM, Ertugrul Soeylemez <[email protected]> wrote:
> Hello KC,
>
> short answer: you don't. ?Usually there is no reason to uninstall
> packages other than to free some disk space. ?If installed packages get
> in your way, then it's mostly because of messed up dependencies in other
> packages.
>
>
> Greets,
> Ertugrul
>
>
> --
> nightmare = unsafePerformIO (getWrongWife >>= sex)
> http://ertes.de/
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 4
Date: Thu, 26 May 2011 20:54:24 -0800
From: Christopher Howard <[email protected]>
Subject: [Haskell-beginners] Simple UTF-8 output
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi. I'm trying to print out UTF-8 strings (pulled from a MySQL database
through HDBC.ODBC). If I just print them out, the extended characters
are printed out as question marks. If I use show on them (which is what
I want to do in this case) they are printed as \NNNNN... (presumably the
Unicode number).

Google brought up hundreds of topics for "Haskell utf-8" but its like
looking for a single tree in a forest.

I took great care to ensure my Gentoo Linux system is UTF-8 ready, and I
can type or echo utf-8 characters on the command line just fine.

# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

-- 
frigidcode.com
theologia.indicium.us



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

Message: 5
Date: Fri, 27 May 2011 01:59:22 -0300
From: Elvio Toccalino <[email protected]>
Subject: Re: [Haskell-beginners] Simple UTF-8 output
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <1306472365.1647.2.camel@el-fierro>
Content-Type: text/plain; charset="UTF-8"

I usually pull such values as ByteStrings. The utf8-strings package
provides printing functions for such data.
Also, if you'd like to work on it as Text, there's the
Data.Text.Encodings module, to decodeUTF8 or decodeASCII ByteString's
into Text.

On Thu, 2011-05-26 at 20:54 -0800, Christopher Howard wrote:
> Hi. I'm trying to print out UTF-8 strings (pulled from a MySQL database
> through HDBC.ODBC). If I just print them out, the extended characters
> are printed out as question marks. If I use show on them (which is what
> I want to do in this case) they are printed as \NNNNN... (presumably the
> Unicode number).
> 
> Google brought up hundreds of topics for "Haskell utf-8" but its like
> looking for a single tree in a forest.
> 
> I took great care to ensure my Gentoo Linux system is UTF-8 ready, and I
> can type or echo utf-8 characters on the command line just fine.
> 
> # locale
> LANG=en_US.UTF-8
> LC_CTYPE="en_US.UTF-8"
> LC_NUMERIC="en_US.UTF-8"
> LC_TIME="en_US.UTF-8"
> LC_COLLATE="en_US.UTF-8"
> LC_MONETARY="en_US.UTF-8"
> LC_MESSAGES="en_US.UTF-8"
> LC_PAPER="en_US.UTF-8"
> LC_NAME="en_US.UTF-8"
> LC_ADDRESS="en_US.UTF-8"
> LC_TELEPHONE="en_US.UTF-8"
> LC_MEASUREMENT="en_US.UTF-8"
> LC_IDENTIFICATION="en_US.UTF-8"
> LC_ALL=
> 





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

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


End of Beginners Digest, Vol 35, Issue 53
*****************************************

Reply via email to