Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please complete part A. A) Rewrite the following ML functions from Chapter 9 as one-line, pure D functions using the appropriate higher-order functions from std.functional
Please complete part A.
A) Rewrite the following ML functions from Chapter 9 as one-line, pure D functions using the appropriate higher-order functions from std.functional and std.algorithm (reduce, map, filter): squarelist, inclist, sqsum, bxor, duplist, and evens. Also do compose, but call it compose_n (so it won't conflict with std.functional.compose), and make it generic (can accept functions of any type) For your information, std.range.retro wraps an array in a reverse iterator so you can traverse it backwards without explicitly reversing the array. Also, recall how you did this in ML: fun compose flist x # foldr (fn (f.sofar) s) f sofar) x flist; Since this is a curried function, it can also be written as: fun compose flist fn x #> foldr (fn (f.sofar) -> f sofar) x flst; This is more representative of how you will write compose n in D Here is a driver to test with (expected output is indicated in the comments) void main() int] nums [1,2,3,4,5] writeln(inclist(nums,1)) writeln(squarelist(nuns)); writeln(sqsum(nums)); writeln(evens (nums)): writeln(bxor([false,true])) writeln(duplist(nums)); writeln(duplist("a","b") auto f # compose_A[(real a) writeln(f(2.0) writeln(f(2.0)) /(2, 3, 4, 5, 6 111, 4, 9, 16, 25 7 5S /12, 4 true 111, 1, 2, 2, 3, 3, 4, 4, 5, 5 a, "a", b",b a/3.0,(real a) >a*a,(real a) >a+ 1.01); A) Rewrite the following ML functions from Chapter 9 as one-line, pure D functions using the appropriate higher-order functions from std.functional and std.algorithm (reduce, map, filter): squarelist, inclist, sqsum, bxor, duplist, and evens. Also do compose, but call it compose_n (so it won't conflict with std.functional.compose), and make it generic (can accept functions of any type) For your information, std.range.retro wraps an array in a reverse iterator so you can traverse it backwards without explicitly reversing the array. Also, recall how you did this in ML: fun compose flist x # foldr (fn (f.sofar) s) f sofar) x flist; Since this is a curried function, it can also be written as: fun compose flist fn x #> foldr (fn (f.sofar) -> f sofar) x flst; This is more representative of how you will write compose n in D Here is a driver to test with (expected output is indicated in the comments) void main() int] nums [1,2,3,4,5] writeln(inclist(nums,1)) writeln(squarelist(nuns)); writeln(sqsum(nums)); writeln(evens (nums)): writeln(bxor([false,true])) writeln(duplist(nums)); writeln(duplist("a","b") auto f # compose_A[(real a) writeln(f(2.0) writeln(f(2.0)) /(2, 3, 4, 5, 6 111, 4, 9, 16, 25 7 5S /12, 4 true 111, 1, 2, 2, 3, 3, 4, 4, 5, 5 a, "a", b",b a/3.0,(real a) >a*a,(real a) >a+ 1.01) Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started