; Discussing some possible extension of Newlisp's add and mul functions
; suggested by Jeremy Dunn, I wrote these few simple functions.
; Just in case that someone is interested.

(set 'println= (lambda-macro(x)(println x " = " (eval x))))

(set 'factorial
(lambda(n)
(let((result 1))
(when (> n 0)
(for(i 1 n 1)
(set 'result (* result i))))
result)))

; one might expect that "sequence" work better than loop here,
; but sequence definition is really different, so it requires some
; changes that at the end, make definition more similar to loop.

(println= (factorial 6))
(println= (factorial 0))

(set 'double-factorial
(lambda(n)
(let((result 1))
(when (> n 0)
(for(i n 1 -2)
(set 'result (* result i))))
result)))

(println= (double-factorial 5))
(println= (double-factorial 6))
(println= (double-factorial 0))

(lambda(n)
(apply + (map int (explode (string n))))))

(set 'multiply-digits
(lambda(n)
(apply * (map int (explode (string n))))))

(println= (multiply-digits 12345))

(lambda(n)
(if (< n 10)
n

(set 'recursively-multiply-digits
(lambda(n)
(if (< n 10)
n
(recursively-multiply-digits
(multiply-digits n)))))

(println= (recursively-multiply-digits 12345))

; (factorial 6) = 720
; (factorial 0) = 1
; (double-factorial 5) = 15
; (double-factorial 6) = 48
; (double-factorial 0) = 1