If not, your "conclusion that Haskell's differences are what throw people off" is incorrect.
I don't think the author of the email is "stereotyping the Haskellers as a bunch of ivory tower PhDs", he's relating a story that's actually true of finding out that a basic technique he wanted to implement was most easily implemented by following the technique in a PhD thesis.
Ideally, lazy evaluation will automatically memoize all your computations. Haskell's 'thunk' system does this in part. However, doing full memoization on most functions will result in huge memory bloat. Which brings us to the essential trade off of memory vs. cpu time. By default, GHC will conserve memory and memoize only when it is obvious that you want values cached. If you want memoization, there are a few things you can do.
1) Add 'let' clauses to your function bodies. GHC will automatically memoize these.
2) Add certain compiler flags for automatic memoization (I don't know what these are, but I know they exist)
3) do it "semi-manually" by generating closures.
I'm sure there are other options, but these three are relatively simple to implement without even needing a memoize function (Though I'm sure one can be written).
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
Now, I don't know if this technique works for the function you're interested in. If you want something more general, you can do soemthing like: memoize :: Ord a => (a -> b) -> IO (a -> b)
memoize f = do
ref <- newIORef Map.empty
return $ \x -> unsafePerformIO $ do
m <- liftM (Map.lookup x) (readIORef ref)
case m of
Just y -> return y
Nothing -> let y = f x
in do modifyIORef ref (M.insert x y)
return y
That's just off the top of my head (probably has syntax errors, never mind the type checker), and it has a number of drawbacks. For one, the map will get larger and larger -- you could use a LRU purging scheme but that's more work than I'm doing today. It doesn't support polymorphic functions, this could be fixed with some extra care but because of the way "memoize" would have to work you'll need to know a decent amount about the Haskell type system.There, I responded to llimllib's ultimatum that somehow my conclusion was incorrect if I didn't personally know how to implement a memoization function. Can I have a cookie?
That rant at the end of my post above was me venting frustrations, on a lot of Haskell forums I see people posting about how much they think Haskell is not the language for them and I think, "If it's not for you, can you go to a forum that's not about Haskell?"
In Haskell, the semantics are unchanged if the runtime evaluates a particular thunk multiple times. Haskell is fairly unique in this regard. For exactly this reason, Haskell threads do not need to synchronize when evaluating thunks in multicore environments. Synchronization has a cost, and that cost can exceed the cost of duplicate evaluation.
It's a trade off.
Of course, the real problem is that people who are new to Haskell expect to sit down and solve problems in the same way they've been solving problems all along. This author was used to the "map" function from Lisp, and wasn't thinking about whether it was necessary, or whether Haskell had a different way of doing things.
Here's what the "mapN" function does:
The reason that we don't want to use "mapN" in Haskell is because of the following problem: The result is either "[Int] -> [Int]" or "[Int -> Int]", with no clue as to which is correct. This is why Haskell has "zipWith", "zipWith3", ... Not as pedantry, but because the alternative is ambiguous. (You can use Control.Applicative + ZipList, but that's a tad verbose).My conclusion is that Haskell's differences are what throw people off -- programmers have to relearn how to do things they thought they had figured out for good years ago. (In this case, "everything is curried" means "varargs requires type classes"). It's FINE if you don't want to learn to do things differently and I won't think less of you, I respect your choices for what you do with your precious free time, and I understand that you might want stuff just to work now without learning anything, just please stop stereotyping the Haskellers as a bunch of ivory tower PhDs. I'm not even done with my Bachelor's.