Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Tutorial: Haskell for the Evil Genius
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Andrew Pennebaker  
View profile  
 More options Sep 14 2012, 7:37 pm
From: Andrew Pennebaker <andrew.penneba...@gmail.com>
Date: Fri, 14 Sep 2012 19:35:36 -0400
Local: Fri, Sep 14 2012 7:35 pm
Subject: Re: [Haskell-cafe] Tutorial: Haskell for the Evil Genius

> Experiment #4:

> fib :: Int -> Int
> fib n = mem !! n

> mem :: [Int]
> mem = map aux [0..]
> -- remark: even [Int] is not a very efficient data structure for this

> aux 0 = 0
> aux 1 = 1
> aux n = mem!!(n-1) + mem!!(n-2)

> main :: IO ()
> main = mapM_ (print . fib) (replicate 10 30)

> Question: How fast is this compared to #1? Why?

Final remark: Evil geniuses should use the scientific method, not the

> opinionative method.

Noted and reflected in the new version.

Thank you for taking the time to write out a detailed, practical analysis
of the question. Yes, I should assume less and test more. I tried
these out<https://github.com/mcandre/mcandre/tree/master/haskell/memfib>as
requested, and came up with results demonstrating that Haskell, does
not, as I mistakenly interpreted, automatically memoize all function calls.
Which makes sense, otherwise memory would quickly run out.

I found some useful reference code on the Haskell Wiki and constructed my
own memoized Fibonacci function using the MemoTrie library, which,
fortunately, is builtin with Haskell Platform and therefore does not
require the tutorial reader to install additional code.

The new version of the tutorial includes an ordinary recursive Fibonacci
function (fib1.hs), and the same code but renamed to fib', memoized using
the memo function from the MemoTrie library (fib2.hs), and exposed as fib.
Unix time information provides real numbers for comparison: The memoized
version is clearly much faster.

I rewrote the section, deleting the part that stated memoization was
automatic, and added text describing how Haskell makes memoization easy.

_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.