[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: applying macros

William James

5/12/2015 12:10:00 AM

Rainer Joswig wrote:

> (defun average-function (list)
> (assert list (list)
> "List should not be empty.")
> (loop for i from 0
> for item in list
> sum item into result
> finally (return (/ result i))))

Gauche Scheme:

(define (average numbers :optional (sum 0) (i 0))
(if (null? numbers)
(and (positive? i) (/ sum i))
(average (cdr numbers) (+ sum (car numbers)) (+ 1 i))))


--
The struggle of our time is to concentrate, not to dissipate: to renew our
association with traditional wisdom: to re-establish a vital connection between
the individual and the race. It is, in a word, a struggle against Liberalism.
--- T.S. Elliot
1 Answer

William James

12/1/2015 5:34:00 PM

0

WJ wrote:

> Rainer Joswig wrote:
>
> > (defun average-function (list)
> > (assert list (list)
> > "List should not be empty.")
> > (loop for i from 0
> > for item in list
> > sum item into result
> > finally (return (/ result i))))

Ocaml:

let average nums =
let rec loop sum i = function
[] -> float sum /. float i
| x::xs -> loop (sum+x) (i+1) xs
in loop 0 0 nums ;;

average [1;2;3;4];;
===>
2.5

--
"If a government uses the instruments of power in its hands for the purpose of
leading a people to ruin, then rebellion is not only the right but also the
duty of every individual citizen."