We want to rewrite the functionccso that its second argument is a list of the values ofthe coins to use rather than an integer specifying which coins to use. We could then havelists that defined each kind of currency:constus_coins = list(50, 25, 10, 5, 1);constuk_coins = list(100, 50, 20, 10, 5, 2, 1);We could then callccas follows:cc(100, us_coins);292To do this will require changing the programccsomewhat. It will still have the same form,but it will access its second argument differently, as follows:functioncc(amount, coin_values) {returnamount === 0? 1: amount < 0 || no_more(coin_values)? 0: cc(amount, except_first_denomination(coin_values)) +cc(amount - first_denomination(coin_values), coin_values);}Define the functionsfirst_denomination,except_first_denomination, andno_morein terms of primitive operations on list structures. Does the order of the listcoin_valuesaffect the answer produced bycc? Why or why not?
90Chapter 2Building Abstractions with DataExercise 2.20In the presence of higher-order functions, it is not strictly necessary for functions to havemultiple parameters; one would suffice. If we have a function such asplusthat natu-rally requires two arguments, we could write a variant of the function to which we passthe arguments one at a time. An application of the variant to the first argument couldreturn a function that we can then apply to the second argument, and so on. This prac-tice—calledcurryingand named after the American mathematician and logician HaskellBrooks Curry—is quite common in programming languages such as Haskell and OCaml.In JavaScript, a curried version ofpluslooks as follows.functionplus_curried(x) {returny => x + y;}Write a functionbrooksthat takes a curried function as first argument and as secondargument a list of arguments to which the curried function is then applied, one by one,in the given order. For example, the following application ofbrooksshould have the sameeffect asplus_curried(3)(4):brooks(plus_curried, list(3, 4));7While we are at it, we might as well curry the functionbrooks! Write a functionbrooks_curriedthat can be applied as follows:brooks_curried(list(plus_curried, 3, 4));7With this functionbrooks_curried, what are the results of evaluating the following twostatements?brooks_curried(list(brooks_curried,list(plus_curried, 3, 4)));brooks_curried(list(brooks_curried,list(brooks_curried,list(plus_curried, 3, 4))));Mapping over listsOne extremely useful operation is to apply some transformation to each element ina list and generate the list of results. For instance, the following function scales eachnumber in a list by a given factor:functionscale_list(items, factor) {returnis_null(items)?null: pair(head(items) * factor,scale_list(tail(items), factor));}scale_list(list(1, 2, 3, 4, 5), 10);[10, [20, [30, [40, [50, null]]]]]
2.2.1Representing Sequences91We can abstract this general idea and capture it as a common pattern expressedas a higher-order function, just as in section 1.3. The higher-order function here iscalledmap. The functionmaptakes as arguments a function of one argument anda list, and returns a list of the results produced by applying the function to eachelement in the list:functionmap(fun, items) {returnis_null(items)?
Upload your study docs or become a
Course Hero member to access this document
Upload your study docs or become a
Course Hero member to access this document
End of preview. Want to read all 640 pages?
Upload your study docs or become a
Course Hero member to access this document
Term
Spring
Professor
SHAFAYE,AB
Tags