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:  Expected vs. Inferred Type (Daniel Fischer)
   2. Re:  Points-free style (Ertugrul Soeylemez)


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

Message: 1
Date: Mon, 6 Jun 2011 10:49:19 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Expected vs. Inferred Type
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="us-ascii"

On Montag, 6. Juni 2011, 05:07, Arlen Christian Mart Cuss wrote:
> On Sun, 2011-06-05 at 17:44 -0800, Christopher Howard wrote:
> > Could someone explain the difference between an expected type and an
> > inferred type as mentioned in a compiler error message? I've never
> > heard definitions proper, and so I tend to get confused when the
> > compiler says there is a difference between the type it expected and
> > the type it inferred.
> 
> Hi Christopher,
> 
> The expected type is the type expected by the compiler in that position;
> 
> e.g. the argument of `not' should be a Bool:
> > :t not
> 
> not :: Bool -> Bool
> 
> 
> Hence in the expression `not x', `x' is *expected* to be a Bool.
> 
> The inferred (or actual) type is the type that Haskell has determined
> 
> the value *actually* is.

Note however, that `expected' and `inferred' are not intrisic, which is 
which depends on the order of type-checking. For example:

Prelude> do { c <- "string"; putChar c; }

<interactive>:1:20:
    Couldn't match expected type `[b]' against inferred type `IO ()'
    In the expression: putChar c
    In the expression:
        do c <- "string"
           putChar c
    In the definition of `it':
        it = do c <- "string"
                putChar c

so typechecking here proceeds left-to-right. If it proceeded right-to-left, 
you'd get an expected type of `IO a' and an inferred type of `[Char]'.

Also note that ghc-7.* says:

    Couldn't match expected type `[b0]' with actual type `IO ()'

i.e., uses `actual' instead of `inferred' in the hope that that is less 
confusing.




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

Message: 2
Date: Mon, 6 Jun 2011 11:01:41 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] Points-free style
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Alexander Shendi <[email protected]> wrote:

> I am working my way through "Learn You a Haskell for Great Good" and
> have reached page 84, where the book recommends that you define your
> functions in a "points-free style", using the "." and "$" operators.
>
> Now I have:
>
> sumProducts' :: Num a => [a] -> [a] -> a
> sumProducts' x y = sum (zipWith (*) x y)
>
> I would like to eliminate the "x" and the "y" in the definition, but
> all I have managed to contrive is:
>
> sumProducts :: Num a => [a] -> [a] -> a
> sumProducts x = sum . zipWith (*) x
>
> How do I proceed from here? Any advice is welcome :)

Honestly, don't!  The definition you have there is perfectly fine.
Please remember:  If the point-free style isn't easy to write, it's
probably also not easy to read.

In general, I would refrain from removing the points from functions with
more than one argument.  But the rule I use is a bit more complicated:
When I have a function with two arguments and both arguments have the
same "subjectness" or are combined in a "monoidic" way, then I don't
remove the points, unless the point-free style is really trivial to
derive.  Examples:

    -- Good (because trivial):
    add = (+)

    -- Good (very readable, point-free would be pointless):
    hypot x y = sqrt (x^2 + y^2)

    -- Bad (I don't even want to derive this by hand):
    hypot = undefined

However, in many cases the two arguments of a binary function have a
different "subjectness".  The last argument is the subject to be
transformed and the earlier argument modifies the transformation, but is
not itself a subject to be transformed.  In these cases I remove only
the last point:

    -- Bad:
    chunksOf n xs =
        takeWhile (not . null) (map (take n) (iterate (drop n) xs))

    -- Good:
    chunksOf n =
        takeWhile (not . null) . map (take n) . iterate (drop n)

    -- Bad:
    chunksOf =
        liftA2 ((.) . (takeWhile (not . null) .))
               (map . take)
               (iterate . drop)

Use point-free style, when it actually makes your code more readable!
Point-free style is used to give definitions in terms of data flow
instead of function applications.

Then there are the many cases, where an argument is just passed to
multiple functions and then the results of the individual functions are
combined.  In this case you can use the fact that (e ->) is an
applicative functor.  Simple example:  Calculating the average value of
a list.

    -- Good:
    average xs = sum xs / fromIntegral (length xs)

    -- Perhaps better:
    average = liftA2 (/) sum (fromIntegral . length)

To get back to your example:  The two arguments your function has have
the same "subjectness".  They are combined in a "monoidic" way and thus
what you really have is a mathematical formula.  Though not always it's
often difficult to get rid of the points in a formula.  Also even if
it's easy, it doesn't always improve the readability in any way.
Example:

    -- Good:
    \x -> 2*x + 1

    -- Bad:
    (+1) . (2*)

    -- Matter of taste (I still don't like it):
    let double = (2*) in
    succ . double


Greets,
Ertugrul


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





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

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


End of Beginners Digest, Vol 36, Issue 15
*****************************************

Reply via email to