2014-08-14 9:44 GMT+02:00 Jon Purdy <evincarofaut...@gmail.com>:
>> Interesting! But what does the (x.) and (-.) words do?
>
> Multiply and subtract in floating point. Factor obviates such
> distinctions with dynamic typing.

Now I get it. With locals your code translates to this in Factor:

:: mean-std-2 ( sum^2 sum inv-len -- mean std )
    sum inv-len * dup [ sum^2 inv-len * ] dip sq - sqrt ;

Or without locals:

: mean-std-8 ( inv-len sum^2 sum -- mean std )
    pick * -rot * over sq - sqrt ;

Here I rearranged the parameter order to make the stack shuffling more
convenient. Another variant using the tuck word:

: mean-std-6 ( sum^2 sum inv-len -- mean std )
    tuck * -rot * over sq - sqrt ;

I must say that the algorithm makes a compelling argument in favor of
locals! Or perhaps in favor of rearranging the parameters to fit the
problem better:

! Fry and tuck
: mean-std-10 ( sum^2 sum inv-len -- std mean )
    '[ _ * ] bi@ tuck sq - sqrt ;

I think the last one is fairly readable.


-- 
mvh/best regards Björn Lindqvist

------------------------------------------------------------------------------
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to