Few proposals:
* traditional for, so (for(i 2 1) ... body ... ) doesn't evaluate body. For example, I just tried to write program that test prime numbers, and typically it should have the following code:
(for (i 2 (- n 1))
(if (= (mod n i) 0) ...
but it doesn't work for n=2, it must be treated as a special case. It is not good. Maybe traditional for can be named for*.
* Also, few simple predicates: positive?, negative?, (divisible? i j) All three replace simple phrases, nevertheless, it is easier to think in terms (divisible? i j) than (= (% i j) 0), especially (clean positive? L) instead of (clean (lambda(x)(> x 0)) L)
You can use < and > with one parameter for negative? and positive?:
> (> 1)
true
> (< 1)
nil
> (< -1)
true
>
> (filter > '(1 2 -3 -5 9))
(1 2 9)
> (filter < '(1 2 -3 -5 9))
(-3 -5)
>
or you could define
> (constant (global 'negative?) <)
> (filter negative? '(-1 2 -3 4))
(-1 -3)
> (constant (global 'positive?) >)
> (filter positive? '(-1 2 -3 4))
(2 4)
>
Can you use the break condition option for for?
(for (i 2 1 1 (> i 1))
(println i))
simpler and more attractive than for* ...
yes, but I think, he wants it the other way around:
> (for (i 2 (- 1 1) 1 (< i 2)) (println i))
2
true
> (for (i 2 (- 5 1) 1 (< i 2)) (println i))
2
3
4
so its forced to go upwards only, for his purpose.
I've seen discussion on prime numbers, so I tried to make the most elegant imperative and functional style program that prints all primes from 2 to 100. For a functional part, I'm quite satisfied:
(println (clean (lambda(x)
(exists zero? (map (curry mod x)
(chop (sequence 2 x)))))
(sequence 2 100)))
But for imperative, I'm not. I want something like:
for n:=2 to 100
prime:=true
for j:=2 to n-1
if (j divides n) then prime:=false
next j
println n ": " prime
next n
There shouldn't be special cases.
positive? and negative? are mostly solved with > <. Nice trick.
You could do:
(dotimes (r 10)
(dotimes (c 10)
(if (= 1 (length (factor (set 'n (+ (* r 10) c)))))
(println n " is prime"))))
Current for macro does same behavior
(for (i 3 0 +1) (println i)) ; print 3,2,1,0
(for (i 3 0 -1) (println i)) ; print 3,2,1,0
I think it will be work more better like this,
;; If `for' argument [num-step] no given, variable will increase by default.
(for (i 3 0) (println i)) ; <no output>
(for (i 3 0 +1) (println i)) ; print 3,4,5,... (not reached 0)
(for (i 3 0 -1) (println i)) ; print 3,2,1,0
---
kosh (not good at english...)