[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Lisp is *Fast*

William James

5/8/2015 6:54:00 AM

Rainer Joswig wrote:

> ; Let's generate a pair of a random string and a number.
> ; The number is the sum of the character ascii values in the string.
>
> (defun make-pair ()
> (loop with length = (random 100)
> with string = (make-string length)
> for i below length
> for char = (code-char (+ 32 (random 90)))
> summing (char-code char) into sum
> do (setf (aref string i) char)
> finally (return (cons sum string))))

Gauche Scheme:

(use srfi-27 :only (random-integer))
(use srfi-13 :only (string-tabulate))

(define (make-pair)
(define sum 0)
(define string
(string-tabulate
(lambda _
(let1 random (+ 32 (random-integer 90))
(inc! sum random)
(integer->char random)))
(random-integer 100)))
(cons sum string))


(make-pair)
===>
(2634 . "n]'?Cm7$h[Fcq+0x7(-9,hn&TXQ -]lPWG?1")

--
Use this [sword] for me, if I rule well; if not, against me.
--- Trajan
The strongest reason for the people to retain the right to bear arms is, as a
last resort, to protect themselves against tyranny in government.
--- Thomas Jefferson
2 Answers

William James

11/29/2015 5:20:00 AM

0

WJ wrote:

> Rainer Joswig wrote:
>
> > ; Let's generate a pair of a random string and a number.
> > ; The number is the sum of the character ascii values in the string.
> >
> > (defun make-pair ()
> > (loop with length = (random 100)
> > with string = (make-string length)
> > for i below length
> > for char = (code-char (+ 32 (random 90)))
> > summing (char-code char) into sum
> > do (setf (aref string i) char)
> > finally (return (cons sum string))))

Ocaml:

let make_pair () =
let sum = ref 0 in
let bytes = Bytes.init (Random.int 100)
(fun _ ->
let byte = 32 + (Random.int 90) in
sum := !sum + byte;
char_of_int byte)
in (!sum, bytes) ;;

make_pair ();;
===>
(4313, "P(P$IyeblF,/ry#Jppklq<B\"DWx:Aig)aE6.v(sx=WF_5&K)A\\cbIj")

--
How does the Swedish political elite respond to their largest cities breaking
down? By pretending there is no problem. The media elite shares the same
contempt for and fear of the common people, and has largely played along.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html

William James

2/3/2016 9:28:00 PM

0

WJ wrote:

> Rainer Joswig wrote:
>
> > ; Let's generate a pair of a random string and a number.
> > ; The number is the sum of the character ascii values in the string.
> >
> > (defun make-pair ()
> > (loop with length = (random 100)
> > with string = (make-string length)
> > for i below length
> > for char = (code-char (+ 32 (random 90)))
> > summing (char-code char) into sum
> > do (setf (aref string i) char)
> > finally (return (cons sum string))))

MatzLisp (Ruby):

def make_pair
sum = 0
s = Array.new(rand 100){code = rand 32..122; sum += code; code.chr}.join
[sum, s]
end

make_pair
===>
[3124, "72Ht1^kkC)#'S/YwjUQqf%\"@1(.HAogWoGfp],&N>0"]

--
This is war, and our greatest enemy is the enemy within: the submissive,
apologetic, guilt-ridden, self-hating drone. The moment we manage to destroy
the enemy within, destroying the rest of our enemies will be a walk in the
park. http://www.kolumbus.fi/aquilon/londonsp...