ScratchData LogoScratchData
Back to gtoal's profile

Programming Technique: Memo Functions

GTgtoal•Created October 2, 2014
Programming Technique: Memo Functions
1
1
70 views
View on Scratch

Instructions

Ackermann's function is a recursive function (like factorial) which can't be easily rewritten in a non-recursive way (unlike factorial which is just a loop). It's an expensive function to calculate, and when written in the most obvious way, some calls to the function take so long to complete that there's no point in doing them. This program uses a technique called 'memo functions' to remember previously computed results, allowing it to avoid most of the recursive calls and execute in far less time. This technique can be applied to any function where the same arguments always give the same results (ie the function has no internal state. These are what computer scientists (but maybe not mathematicians) call 'idempotent' functions). Compare the memo function version to the simple version at http://scratch.mit.edu/projects/28287344/

Description

This memo function uses an array to remember the parameter/result pairs, but that only works if the parameters are contiguous or densely packed. To make a memo function of an arbitrary function, you need to use something like an associative array or a dictionary to store the parameter/result combinations. A simple dictionary program is available at http://scratch.mit.edu/projects/30045458/

Project Details

Project ID28293808
CreatedOctober 2, 2014
Last ModifiedOctober 19, 2014
SharedOctober 19, 2014
Visibilityvisible
CommentsAllowed