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.  A neat,  simple example of making code more efficient
      (Costello, Roger L.)
   2. Re:  A neat,      simple example of making code more efficient
      (Matthias Guedemann)
   3. Re:  A neat,      simple example of making code more efficient
      (Ertugrul Soeylemez)
   4.  Is it possible to implement groupBy using folds? (Zhi-Qiang Lei)
   5. Re:  Is it possible to implement groupBy using    folds?
      (Stephen Tetley)
   6. Re:  Data.Foldable's foldr' (Federico Mastellone)
   7.  Haskell: The Craft of Functional Programming     3rd ed is out. (KC)
   8.  Web frameworks (Manfred Lotz)


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

Message: 1
Date: Thu, 16 Jun 2011 06:34:27 -0400
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] A neat,    simple example of making code
        more efficient
To: "[email protected]" <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi Folks,

This book [1] has a neat, simple example of making code more efficient.

Suppose you want to create a function, quad, that raises a number to the 4th 
power.

Example: quad 2 = 2 * 2 * 2 * 2 = 16

The function could be implemented like this:

    quad a = a * a * a * a

That implementation requires three multiplications, which can be pretty 
expensive, especially with arbitrary numbers.

Let's see if we can make it more efficient, by finding an implementation that 
uses less multiplications. 

By the associative law we can group the expression:

    (a * a) * (a * a)

Each subexpression performs a square operation:

    (square a) * (square a)

Which is itself a square operation:

    square (square a)

That implementation requires only two multiplications. Let's take an example to 
see why:

quad 2 = square (square 2)
            = square (2 * 2)        -- First multiplication
            = square (4)              -- Reduction 
            = 4 * 4                       -- Second multiplication
            = 16                           -- Reduction to the answer

Thus, by a little rework the number of multiplications is reduced from three to 
two. That is a significant efficiency improvement.

Cool, aye?

/Roger

[1] 
http://www.amazon.com/Introduction-Functional-Programming-using-Haskell/dp/0134843460/ref=sr_1_1?ie=UTF8&qid=1308220033&sr=8-1



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

Message: 2
Date: Thu, 16 Jun 2011 12:51:39 +0200
From: Matthias Guedemann <[email protected]>
Subject: Re: [Haskell-beginners] A neat,        simple example of making code
        more efficient
To: "Costello\, Roger L." <[email protected]>,
        "beginners\@haskell.org" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii


Hi Roger,

> Which is itself a square operation:
> 
>     square (square a)

this is a special case of square-and-multiply or binary exponentiation
(see http://en.wikipedia.org/wiki/Exponentiation_by_squaring)

general square and multiply also works for an odd exponent and reduces
the complexity from O(n) to O(log n)

regards
Matthias



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

Message: 3
Date: Thu, 16 Jun 2011 13:29:11 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] A neat,        simple example of making code
        more efficient
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

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

> This book [1] has a neat, simple example of making code more
> efficient.
>
> [...]
>
> Thus, by a little rework the number of multiplications is reduced from
> three to two. That is a significant efficiency improvement.
>
> Cool, aye?

As Matthias noted correctly this is a well known method for
exponentiation with arbitrary integer exponents (works even for rational
exponents in certain groups).

I would like to add that exactly this method drives the (^)
exponentiation function, so your function becomes

    quad :: Num a => a -> a
    quad = (^4)

and benefits from the same exponential speedup, although writing the
direct multiplication may give you a performance improvement by a
constant factor, which may or may not be significant.


Greets,
Ertugrul


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





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

Message: 4
Date: Thu, 16 Jun 2011 21:55:37 +0800
From: Zhi-Qiang Lei <[email protected]>
Subject: [Haskell-beginners] Is it possible to implement groupBy using
        folds?
To: Haskell Beginer <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi,

The source code of groupBy shows that groupBy does not go through every 
element. Is it really possible to implement it using folds? Thanks.

-- | The 'groupBy' function is the non-overloaded version of 'group'.
groupBy                 :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy _  []           =  []
groupBy eq (x:xs)       =  (x:ys) : groupBy eq zs
                           where (ys,zs) = span (eq x) xs


Best regards,
Zhi-Qiang Lei
[email protected]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110616/42a5e19b/attachment-0001.htm>

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

Message: 5
Date: Thu, 16 Jun 2011 15:36:38 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Is it possible to implement groupBy
        using   folds?
Cc: Haskell Beginer <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

This foldr version uses a partitioned accumulator and a
post-processing step - it should be possible to achieve the same with
an accumulator of [[a]] and deeper pattern matching:


groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy test = post . foldr fn ([],[])
  where
    post ([],yss) = yss
    post (xs,yss) = xs : yss

    fn a ([],yss)                = ([a], yss)
    fn a (b:bs, yss) | test a b  = (a:b:bs, yss)
                     | otherwise = ([a], (b:bs):yss)



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

Message: 6
Date: Thu, 16 Jun 2011 17:12:12 -0300
From: Federico Mastellone <[email protected]>
Subject: Re: [Haskell-beginners] Data.Foldable's foldr'
To: [email protected]
Cc: Beginners Haskell Mailinglist <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Thanks Yitzchak and Daniel (again)

I think I looked at this code in a non-functional way! I was expecting, no
matter what, the fold to return a value and not a function, so my head, or
HHC (Head Haskell Compiler), was throwing a syntax error!

This is one crazy little example
id id id id id ... id x

Regards!

On Wed, Jun 15, 2011 at 12:38 PM, Yitzchak Gale <[email protected]> wrote:

> Federico Mastellone wrote:
> > foldl :: (a -> b -> a) -> a -> t b -> a
> >
> > Them, outside of the class, foldr' is defined like this:
> >
> > -- | Fold over the elements of a structure,
> > -- associating to the right, but strictly.
> > foldr' :: Foldable t => (a -> b -> b) -> b -> t a -> b
> > foldr' f z0 xs = foldl f' id xs z0
> >   where f' k x z = k $! f x z
> >
> > I don't understand this definition, foldl receives 3 parameters and here
> it
> > is used with 4, how is it possible?
> > Even the function passed to foldl has 3 parameters when a function of 2
> is
> > needed.
>
> In the type of foldl, the parameter 'a' can be any type - even
> a function.
>
> So let's see what we get when we substitute 'c -> d' for 'a'
> in the type of foldl:
>
> ((c -> d) -> b -> (c -> d)) -> (c -> d) -> t b -> (c -> d)
>
> Now, remembering that -> is right-associative
> in type expressions, we can remove some parentheses:
>
> ((c -> d) -> b -> c -> d) -> (c -> d) -> t b -> c -> d
>
> So when 'a' is a function, we see that foldl indeed takes
> 4 parameters, and its first parameter is a function that
> takes 3 parameters.
>
> Regards,
> Yitz
>



-- 
Federico Mastellone
Computer Science Engineer - ITBA

".. there are two ways of constructing a software design: One way is to make
it so simple that there are obviously no deficiencies, and the other way is
to make it so complicated that there are no obvious deficiencies. The first
method is far more difficult."

Tony Hoare, 1980 ACM Turing Award Lecture.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110616/e0593d9e/attachment-0001.htm>

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

Message: 7
Date: Thu, 16 Jun 2011 16:23:16 -0700
From: KC <[email protected]>
Subject: [Haskell-beginners] Haskell: The Craft of Functional
        Programming     3rd ed is out.
To: haskell-cafe <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

http://www.haskellcraft.com/craft3e/Home.html

-- 
--
Regards,
KC



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

Message: 8
Date: Fri, 17 Jun 2011 08:30:10 +0200
From: Manfred Lotz <[email protected]>
Subject: [Haskell-beginners] Web frameworks
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Hi there,
There are some web frameworks out there, as snap, yesod, happstack, and
perhaps even more.

I'd like to get a recommendation which web framework might be easiest to
get into, and to use for a haskell beginner. 



-- 
Thanks,
Manfred





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

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


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

Reply via email to