[Haskell-cafe] trace

michael rice nowgate at yahoo.com
Thu Dec 24 16:21:50 EST 2009


Hmm... here are the functions I was looking to trace, the second one being an example from Scheme text "Concrete Abstractions" that I rewrote after seeing the first. Compared to the CL/Scheme memoization code, the Haskell seems like, how shall I put this, a sleight of hand, so much so that I'm driven to look behind the scenes to try to understand what is occurring. I remember that someone said, pattern matching is strict and LET is lazy, so I know the trick depends on laziness, but knowing that and understanding it are still a world apart.

Does tracing a function *always* require memoizing it?

Michael

memo_fib :: Int -> Integer
memo_fib =
   let fib 0 = 0
       fib 1 = 1
       fib n = memoized_fib (n-2) + memoized_fib (n-1)
   in  (map fib [0..] !!)

memo_walk_count :: Int -> Integer
memo_walk_count =
   let walk_count 0 = 1
       walk_count 1 = 1
       walk_count n = memo_walk_count (n-2) + memo_walk_count (n-1)
   in (map walk_count [0..] !!)

 

--- On Thu, 12/24/09, Daniel Fischer <daniel.is.fischer at web.de> wrote:

From: Daniel Fischer <daniel.is.fischer at web.de>
Subject: Re: [Haskell-cafe] trace
To: haskell-cafe at haskell.org
Cc: "michael rice" <nowgate at yahoo.com>
Date: Thursday, December 24, 2009, 3:52 PM



#yiv1755006913 p, #yiv1755006913 li {white-space:pre-wrap;}
Am Donnerstag 24 Dezember 2009 21:31:34 schrieb michael rice:
> Can someone provide a simple example of tracing a function.
>
> Michael

Is

import Debug.Trace

infixl 0 `debug`

debug = flip trace

dfib :: Int -> Integer
dfib =
    let fib 0 = 0
        fib 1 = 1
        fib n = dfib (n-2) + dfib (n-1) `debug` "eval fib " ++ show n
    in (map fib [0 .. ] !!)


*MFib> dfib 12
eval fib 12
eval fib 10
eval fib 8
eval fib 6
eval fib 4
eval fib 2
eval fib 3
eval fib 5
eval fib 7
eval fib 9
eval fib 11
144

the kind of example you're looking for?


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091224/7698a238/attachment.html


More information about the Haskell-Cafe mailing list