Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

#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...

1 answer below »
#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))))))
Answered Same Day Aug 14, 2021 Swinburne University of Technology

Solution

Sudipta answered on Aug 15 2021
150 Votes
Description of Scheme language, a functional programming language
(define (setpower l n)
(cond
((< n 0) #f)
((= n 0) '())
((= n 1) l)
(#t (mult l (setpower l (- n 1))))))
The above function uses recursion to return the power of number set. We pass two arguments l
that is the list of the set and n which is the power of set. So the function computes and return all
possible subset of length n.
Here, ((< n 0) #f) , this line checks if n is less than zero then it returns false as no set with
negative power is possible. Next, ((= n 0) '()), checks if n equals to zero then it returns a blank
list or set. And ((= n 1) l) this means if n equals to 1 then it returns 1. Finally the recursion
process is called, in which mult function is called with arguments l and (setpower l (- n 1)) which
means the...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here