Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #23633

Re: Function problem

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: Function problem
Date 2013-06-15 00:30 +0000
Organization A noiseless patient Spider
Message-ID <kpgcj3$nnl$1@dont-email.me> (permalink)
References <kkjt1s$qt1$1@dont-email.me>

Show all headers | View raw


WJ wrote:

> > Write a Function 'total' that takes an orderd list ie. 
> > ((itemA quantityA costA)(itemB quantityB costB)....)
> > but returns a list giving the total cost plus the overall cost.
> > 
> > Eg:
> > 
> > LISP>(total'((book 2 10)(pen 3 2)(notepad 1 12)))
> > ((BOOK 20)(PEN 6)(NOTEPAD 12)(TOTAL 38))
> 
> Factor:
> 
> USING: locals ;
> 
> :: total ( seq -- seq )
>   0 :> tot!
>   seq
>   [ first3 *
>     dup tot + tot!
>     2array
>   ]
>   map
>   "total" tot 2array  suffix ;
> 
> 
> { { "book" 2 10 } { "pen" 3 2 } { "notepad" 1 12 } } total .
>  ==>
> { { "book" 20 } { "pen" 6 } { "notepad" 12 } { "total" 38 } }

Ruby:

total = 0
    ==>0
[["book",2,10],["pen",3,2],["notepad",1,12]].map{|a,b,c|
  total += b*c; [a, b*c]} + [["total", total]]
    ==>[["book", 20], ["pen", 6], ["notepad", 12], ["total", 38]]

Back to comp.lang.forth | Previous | Next | Find similar


Thread

Re: Function problem "WJ" <w_a_x_man@yahoo.com> - 2013-06-15 00:30 +0000

csiph-web