Here is one in ML
Code:
val values = [(100, "Dollars"), (25, "Quarters"),
(10, "Dimes"), (5, "Nickels"), (1, "Pennies")];
fun head([]) = []
| head((x,y)::xs) = x :: head(xs);
fun tail([]) = []
| tail((x,y)::xs) = y :: tail(xs);
fun combine([],[]) = []
| combine(x, []) = []
| combine([], y) = []
| combine(x::xs, y::ys) = (x,y) :: combine(xs,ys);
fun count([], value, result) = result
| count(x::xs, value, result) =
if value = x then
count(xs, value, result +1)
else
count(xs, value, result);
fun count_all([], []) = []
| count_all([], y) = []
| count_all(x, []) = []
| count_all(x, y::ys) = count(x,y,0) :: count_all(x, ys);
fun change (coinvals, 0) = []
| change ([], amount) = []
| change (c::coinvals, amount) =
if amount<c then change(coinvals, amount)
else c :: change(c::coinvals, amount-c);
fun make_change(value) =
combine(
count_all(
change(
head(values), value
), head(values)
), tail(values)
);
Quote:
|
I need a write a programming code for that question
|
I'm not here to do your homework, show us how far you've gotten, then we might help...
If you know functional programming, then my ML version will provide great help.. If not, you're not any further than you were to begin with..