#lang scheme
;this program finds the power of number set
; (1 2 3)^2 -> XXXXXXXXXX XXXXXXXXXX2)(3 3))
;example: (setpower ' XXXXXXXXXX)
; it uses mult function which multiplies two sets, and
; main function, which uses recursion to perform multiplication until
; set's degree is not 1
;
; mult function also utilizes comb function, which checks
; the nesting level of elements it multiplies:
; 1 * 2 -> (1 2), uses list arg1 arg2
; (1) * 2 -> (1 2), uses append arg1 (list arg2)
; 1 * (2) -> (1 2), uses cons arg1 arg2
; (1) * (2) -> (1 2), uses append arg1 arg2
; It is done to ensure intermediate multiplications in recursive calls
; for example (1 2)^3 -
; XXXXXXXXXX XXXXXXXXXX XXXXXXXXXX))
; here the transition from 2nd to 3rd level should not create
; additional ().
; because of this, this program works only with number lists
; (it cannot distinguish whether additional pars came inside recursion
; or as data input, like XXXXXXXXXX))), but it still provides
; a good example of recursion in implementing of mathematical concept
(define (mult l1 l2)
(apply append (map (lambda(x)(map (lambda(y)(comb x y)) l2)) l1)))
(define (comb x y)
(cond
((and (not (list? x)) (not (list? y))) (list x y))
((and (not (list? x)) (list? y)) (cons x y))
((and (list? x) (not (list? y))) (append x (list y)))
((and (list? x) (list? y)) (append x y))))
(define (setpower l n)
(cond
((< n 0) #f)
((= n 0) '())
((= n 1) l)
(#t (mult l (setpower l (- n 1))))))