On Tue, Aug 16, 2011 at 2:07 PM, Mute clown <withnom...@gmail.com> wrote:
>
> Never used a mailing list before, got kinda pushed into using it since for 
> some reason i cant talk in IRC.
You need to register a name and identify yourself to be able to talk
on the irc channel. =

> I am trying to learn how to use factor, the whole stack oriented, 
> concatenative programming is alien to me. I am trying to implement Herons 
> Formula, to work out the area of a triangle:
> Area = sqrt(s(s-a)(s-b)(s-c))
> s = a+b+c /2
> I've sort of worked out how to do s:
> : dev2 ( d -- s ) 2 / ;
> : sem ( a b c -- s ) + + dev2 ;
> Problem 1:
> IDK why the following doesn't work.
> : sem ( a b c -- s ) dup + + dev2 ;
You have something mixed up here. When you write a word with stack
effect "( a b c -- s )", it means the word is expected to be called
with a b c on the stack in this order, removes them from the stack,
and put s on the stack. So your previous implementation of sem is the
correct one.
Based on the second problem, I'd say you want to use 3dup, but that's
just a guess of what you're trying to do with your "dup".

> Problem 2:
> say i get the above problem fixed(use dup before calling the word)
> I dont know how to proceed with the current stack:
> -bottom of the stack-
> a
> b
> c
> s
> -top of the stack-
> i've tried various words to modify the top of the stack but, can't seem to 
> arrange it into a way so i can compute what i need.
First of all, when you have problems with the stack, you can still use
locals: http://docs.factorcode.org/content/article-locals.html

Here are possible ways to write the function. The concatenative
version might not be the most readable solution though...

USING: arrays infix kernel locals math math.functions sequences ;

: sem ( a b c -- s ) + + 2 / ;
: heron ( a b c -- area ) [ sem ] 3keep 0 4array [ - ] with map product sqrt ;

:: heron1 ( a b c -- area )
    a b c sem :> s
    s s a - s b - s c - * * * sqrt ;

:: heron2 ( a b c -- area )
    a b c sem :> s
    [infix sqrt(s*(s-a)*(s-b)*(s-c)) infix] ;


Hope this helps :)

Jon

------------------------------------------------------------------------------
uberSVN's rich system and user administration capabilities and model 
configuration take the hassle out of deploying and managing Subversion and 
the tools developers use with it. Learn more about uberSVN and get a free 
download at:  http://p.sf.net/sfu/wandisco-dev2dev
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to