I've thought about this awhile and I simply can't think of an easy way to solve a problem. I am learning Standard ML in my free time and saw an elegant solution
to a certain problem. The problem is: given a set of coins, figure out how to use the coins to make change for a given amount.
The ML solution is clean and elegant due to the use of exceptions. I cannot imagine a simple clean way to do this in Perl, but an openly soliciting such.
here is the ML solution if you are curious
exception Change
fun change _ 0 = nil
| change nil _ = raise Change
| change (coin::coins) amt =
if coin > amt then
change coins amt
else
(coin :: change (coin::coins) (amt-coin))
handle Change => change coins amt
A sample call to this might be:
change [5,2] 16
ML has a TCL look to it, but it is a strongly typed language with an aggressively optimizing compiler
that Dominus gave an
interesting talk about --- if you do go read these slides, don't miss the part where ML detects an infinite loop by it's static rigorous type checking.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.