Write a Lisp function ‘powerset(lst)’ that takes a list ‘lst’ as input and retur
ID: 672390 • Letter: W
Question
Write a Lisp function ‘powerset(lst)’ that takes a list ‘lst’ as input and returns the power set of the lst. Assume the input lst to be a set which is used to generate the power set of the input set. A power set is defined as a set of all subsets of a set.
Power set of {a, b, c, d, …} is equal to the union of power set of {b, c, d,…} and ‘a’ appended to each element of power set of {b, ,c, d,…}
Question1.Write a Lisp function ‘addx(x pset)’ that appends an element x to each element of a power set. Sample output of addx: >(addx ‘3 ‘(nil) )
( (3) NIL)
>(addx ‘2 ‘( (3) nil))
( (2 3) (2) (3) NIL)
>(addx ‘1 ‘( (2 3) (3) (2) nil))
( (1 2 3) (1 3) (1 2) (1) (2 3) (3) (2) NIL)
Question2.Use ‘addx’ to complete the Lisp function ‘powerset’. Use the ‘remove-duplicates’ function to remove duplicate elements in the powerset.
The following functions are NOT ALLOWED: mapc, mapcar, maplist, do, do*, dolist, setq, and when. Loops are also NOT ALLOWED. I know how to remove the duplicates. I am just having a hard time understanding how to write the 'addx' function. Any help would be greatly appreciated.
Explanation / Answer
public static List powerset(string[] inlist) ;{ ; if (inlist.Length == 0) // (if null inlist) ; { ; List result = new List(); ; result.Add(null); ; return result; // '(nil) ; } ; else ; { ; string[] tempps_arg = new string[inlist.Length - 1]; ; Array.Copy(inlist, 1, tempps_arg, 0, inlist.Length - 1); // (cdr inlist) ; ; List tempps = powerset(tempps_arg); // (let ((tempps (powerset (cdr inlist)))) ; List tempps_result = new List(tempps.ToArray()); ; ; foreach (string[] x in tempps) // (mapcar #'(lambda (x) ({...}) tempps) ; { ; List concatlist = new List(); ; concatlist.Add(inlist[0]); // (car inlist) ; if( x != null) concatlist.AddRange(x); // (cons (car inlist) x) ; tempps_result.Add(concatlist.ToArray()); // (append tempps (mapcar #'(lambda (x) ({...}) ) ; } ; ; return tempps_result; ; } ;}Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.