[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: g-adic development homework

William James

8/7/2015 10:51:00 AM

Kenny Tilton wrote:

> (defun sum-g-adic-l (g lst)
> (float (loop for a_i in lst
> for g^i = 1 then (* g g^i)
> summing (/ a_i g^i))))
>
> It's not any more functional under the hood, but it looks more
> functional, and as Andre Agassi said, image is everything.
>
> This works, too:
>
> (defun sum-g-adic (g lst &optional (g^i 1)(accum 0.0))
> (if lst
> (sum-g-adic g (rest lst) (* g g^i) (+ accum (/ (car lst) g^i)))
> accum))

Gauche Scheme:

(use gauche.collection :only (fold2))

(define (sum-g-adic-l g lst)
(fold2
(^(a_i sum g_i) (values (+ sum (/ a_i g_i)) (* g_i g)))
0.0 1.0
lst))


(sum-g-adic-l 3.3 '(2 4 5 9 88))
===>
4.663735611394014
391.35392999999993

--
You have politicians saying that ... as many Africans as want to come into
Sweden should be able to come.... I think there's a billion Africans now.
They've already said that everybody from Syria can come to Sweden because they
have a civil war there.... They have a huge housing crisis; they are actually
thinking of commandeering people's vacation homes because they need more housing
for immigrants. --- Kevin MacDonald (http://lnrlive.com/tpc/tpc201...)
1 Answer

William James

11/28/2015 10:32:00 PM

0

WJ wrote:

> Kenny Tilton wrote:
>
> > (defun sum-g-adic-l (g lst)
> > (float (loop for a_i in lst
> > for g^i = 1 then (* g g^i)
> > summing (/ a_i g^i))))
> >
> > It's not any more functional under the hood, but it looks more
> > functional, and as Andre Agassi said, image is everything.
> >
> > This works, too:
> >
> > (defun sum-g-adic (g lst &optional (g^i 1)(accum 0.0))
> > (if lst
> > (sum-g-adic g (rest lst) (* g g^i) (+ accum (/ (car lst) g^i)))
> > accum))
>
> Gauche Scheme:
>
> (use gauche.collection :only (fold2))
>
> (define (sum-g-adic-l g lst)
> (fold2
> (^(a_i sum g_i) (values (+ sum (/ a_i g_i)) (* g_i g)))
> 0.0 1.0
> lst))
>
>
> (sum-g-adic-l 3.3 '(2 4 5 9 88))
> ===>
> 4.663735611394014
> 391.35392999999993

Ocaml:

let sum_g_adic g lst =
let rec loop g_i accum = function
[] -> (accum, g_i)
| a_i::xs -> loop (g *. g_i) (accum +. (a_i /. g_i)) xs
in loop 1.0 0.0 lst ;;

sum_g_adic 3.3 [2.;4.;5.;9.;88.] ;;

(4.6637356113940136, 391.35392999999993)

--
Sahlin [stressed] that her compatriots must accept that the new Sweden is
multi-cultural.... "Like it or not, this is the new Sweden."
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html