(define (sel-idx lst)
(let ((out '()) (len (length lst)))
(dolist (el lst)
; select and index of the list different from the current
(while (= (setq idx (rand len)) $idx))
;(println "coppia: " $idx idx)
(push (list $idx idx) out -1))
out))
(sel-idx '(1))
;-> ((0 0)) --> ERROR(sel-idx '(1 2))
;-> ((0 1) (1 0)) --> OK
(sel-idx '(1 2))
;-> ((0 1) (1 1)) --> ERROR
Why doesn't the function produce correct results?(define (sel-idx-ok lst)
(let ((out '()) (len (length lst)) (tmp 0))
(dolist (el lst)
(setq tmp $idx)
; select and index of the list different from the current
(while (= (setq idx (rand len)) tmp))
;(println "coppia: " $idx idx)
(push (list tmp idx) out -1))
out))
(sel-idx-ok '(1))
;-> ... --> infinite loop
(sel-idx-ok '(1 2))
;-> ((0 1) (1 0)) --> OK
(sel-idx '(0 1 2 3 4 5 6 7 8 9))
;-> ((0 4) (1 9) (2 4) (3 8) (4 8) (5 9) (6 1) (7 1) (8 6) (9 6))
(define (test lst)
(let (len (length lst))
(dolist (el lst)
; select and index of the list different from the current
(while (!= (setq idx (rand len)) $idx)))
(println "index: " idx)))
Why (test '(1 2 3 4 5)) fall (almost always) into an infinite loop?(define (primes-to num)
"Generate all prime numbers less than or equal to a given number"
(cond ((< num 2) '())
((< num 3) '(2))
(true
(letn ((m (/ (- num 3) 2))
(arr (array (+ m 1)))
(lim (/ (- (int (sqrt num)) 3) 2))
(lst '(2)))
(for (i 0 lim)
(when (nil? (arr i))
(letn ((p (+ (* 2 i) 3))
(j (/ (- (* p p) 3) 2)))
(for (k j m p (> k m))
(setf (arr k) true)))))
(for (i 0 m)
(when (nil? (arr i))
(push (+ (* 2 i) 3) lst -1)))
lst))))
(time (println (length (primes-to 1e6))))
;-> 78498
;-> 100.292
(time (println (length (primes-to 1e7))))
;-> 664579
;-> 1396.784
(time (println (length (primes-to 1e8))))
;-> 5761455
;-> 15544.004
(time (println (length (primes-to 1e9))))
;-> 50847534
;-> 178960.989