#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 »
The code is written in Scheme language, a functional programming language. Write a 1,2-page report explaining the code on each line. This code uses the recursion; explain how recursion is used in the code. Use examples if you have to. This report is 70% of your grade.


#lang scheme ;this program finds the power of number set ; (1 2 3)^2 -> ((1 1)(1 2)(1 3)(2 1)(2 2)(2 3)(3 1)(3 2)(3 3)) ;example: (setpower '(1 2 3) 2) ; 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 -> ; ((1 1 1)(1 1 2)(1 2 1)(1 2 2)(2 1 1)(2 1 2)(2 2 1)(2 2 2)) ; 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 (1 (2 3))), 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)))))) n="" 0)="" #f)="" ((="n" 0)="" '())="" ((="n" 1)="" l)="" (#t="" (mult="" l="" (setpower="" l="" (-="" n="">
Answered Same DayAug 14, 2021Swinburne University of Technology

Answer To: #lang scheme ;this program finds the power of number set ; (1 2 3)^2 -> XXXXXXXXXX XXXXXXXXXX2)(3...

Sudipta answered on Aug 15 2021
137 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