On Mon, 2010-02-22 at 08:57 -0500, Terrence Brannon wrote:
> I'm a bit confused on when to use MEMO: versus $[
> 
> Practically speaking, below the words clean and wordlist need only be
> computed once, and I'm not sure of the best way to do it, or what the
> tradeoffs are of different approaches. I'm also not sure how to do it
> completely correctly. Is there an article on this subject I missed?
MEMO: caches the result of the function in an assoc whose key is an
array of the arguments passed in, and checks the cache before going into
the body of the program each time it is called.

For example, for a Fibonacci number program, one might use

MEMO: fib ( n -- fib(n) )
    dup [ 1 = ] [ 2 = ] bi or
    [ drop 1 ] [ [ 2 - fib ] [ 1 - fib ] bi + ] if ;

which would cache the value of fib(n) after calculating it, and thus
*not* have O(2^n) time as it would have if MEMO: weren't used.

$[, on the other hand, calculates things at parse time, and so cannot
take any arguments.  

If you know that you will only have one value for text, then replacing
wordlist with 

: wordlist $[ clean R/ \s+/ re-split [ >string ] map ] ;

will work.

But if you want to make wordlist a function of the form ( text --
array ) then you would want to use MEMO:.
> 
> 
> USING: ascii kernel math.ranges random regexp sequences strings ;
> IN: lorem
> 
> 
> <PRIVATE
> 
> CONSTANT: text "alias consequatur aut perferendis sit voluptatem
> accusantium doloremque aperiam, eaque ipsa quae ab illo inventore
> veritatis et."
> 
> : clean ( -- string )  text >lower R/ [^\sA-Za-z]/ "" re-replace ;
> 
> PRIVATE>
> 
> : wordlist ( -- array )  clean R/ \s+/ re-split [ >string ] map ;
> 
> : getword ( -- string )   wordlist random ;
> : getwords ( n -- array ) wordlist swap sample ;
> : sentencewords (  -- array  ) 4 10 [a,b] random getwords ;
> : sentencestring ( -- string ) sentencewords " " join ;
> 
> : ucfirst ( string -- string ) 1 cut [ >upper ] dip append ;
> : sentence ( -- string ) sentencestring ucfirst "." append ;
> : sentences ( n -- array ) [ sentence ] replicate ;
> : paragraph ( -- string ) 3 7 [a,b] random sentences " " join ;
> : paragraphs ( n -- array ) [ paragraph ] replicate ;
> 
> ------------------------------------------------------------------------------
> Download Intel&#174; Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
> 



------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to