Using F# language. Full and complete answer in order to get credit please. Thank
ID: 3584944 • Letter: U
Question
Using F# language. Full and complete answer in order to get credit please. Thank you.
1)Write an uncurried F# function cartesian (xs, ys) that takes as input two lists xs and ys and returns a list of pairs that represents the Cartesian product of xs and ys. (The pairs in the Cartesian product may appear in any order.) For example,
2) An F# list can be thought of as representing a set, where the order of the elements in the list is irrelevant. Write an F# function powerset such that powerset set returns the set of all subsets of set. For example,
Explanation / Answer
[1]
let rec cartesian = function
| (xs, []) -> []
| (xs, y::ys) -> List.map (fun x -> (x, y)) xs @ cartesian (xs, ys) ;;
[2]
let rec powerset = function
| [] -> [[]]
| x::xs ->
let powersetBefore = powerset xs
powersetBefore @ List.map (fun ys -> x::ys) powersetBefore;;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.