[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Beginner question

William James

3/11/2016 11:29:00 PM

Pascal Bourguignon wrote:

> But you're not doing the same thing. Better use do than loop:
>
>
> (defun put-stones-to-strip-i (strip stones)
> (do ((current strip (or (cdr current) strip))
> (remains stones (1- remains)))
> ((< remains 0) strip)
> (incf (car current))))
>
>
>
> On the other hand, stones is often enough much greater than (length
> strip), it would be more efficient to do:
>
>
> (defun put-stones-to-strip-a (strip stones)
> (multiple-value-bind (stone/slot remainder) (truncate stones (length strip))
> (do ((current strip (cdr current)))
> ((null current) strip)
> (if (< 0 remainder)
> (progn (decf remainder)
> (incf (car current) (1+ stone/slot)))
> (incf (car current) stone/slot)))))

OCaml:

let put_stones_to_vector strip stones =
for i = 0 to stones - 1 do
let j = i mod (Array.length strip) in
strip.(j) <- strip.(j) + 1
done ;;

# let strip = [|0;0;0;0;0;0;0;0|];;
val strip : int array = [|0; 0; 0; 0; 0; 0; 0; 0|]
# put_stones_to_vector strip 3;;
- : unit = ()
# strip;;
- : int array = [|1; 1; 1; 0; 0; 0; 0; 0|]
# put_stones_to_vector strip 22;;
- : unit = ()
# strip;;
- : int array = [|4; 4; 4; 3; 3; 3; 2; 2|]
# Array.fold_left (+) 0 strip;;
- : int = 25


--
For two years ... he was held in solitary confinement in the Toronto West
Detention Centre, on the pretext that he is a threat to national security....
[A] court in Mannheim sentenced him to five years imprisonment for the crime of
"popular incitement" under Germany's notorious "Holocaust denial" statute.
http://www.revisionists.com/revisionists/z...