Well you can start with what you already know to be true, defining
translate "excellent" = "doubleplusgood"
(suspend your disbelief for a moment, will you).
Yes this is a valid definition. But it doesn't refer to dictionary
, so we can address that by defining
translate "excellent" = matchup "excellent" dictionary
matchup "excellent" dict = "doubleplusgood"
Except that it is too specific of course. So we generalize a little bit as
translate excellent = matchup excellent dictionary
matchup excellent dict = "doubleplusgood"
Now the matchup
is just cheating. We can try make it do some actual work as
matchup "excellent"
[("doubleplusgood",[ "excellent", "fabulous", "fantastic", "best"])]
=
"doubleplusgood"
All these variations so far are just writing down what you already had.
But we had it generalized before, so
matchup excellent
[("doubleplusgood",[ excellent, "fabulous", "fantastic", "best"])]
=
"doubleplusgood"
and that's an error now. We can't have two occurrences of the same variable in our arguments. It's forbidden in Haskell. So it must be
matchup excellent1
[("doubleplusgood",[ excellent2, "fabulous", "fantastic", "best"])]
=
"doubleplusgood"
We are definitely going somewhere with this. But wait, we didn't use the two variables at all. They are supposed to have the same value, aren't they. So let's write that down:
matchup excellent1
[("doubleplusgood",[ excellent2, "fabulous", "fantastic", "best"])]
| excellent1 == excellent2
=
"doubleplusgood"
Well but what's with all these other entries in the synonyms list? And their "translation"?
matchup excellent1
[(doubleplusgood,[ excellent2, fabulous, fantastic, best])]
| excellent1 == excellent2
=
doubleplusgood
Now this is a bona fide Haskell definition. Almost. Why should the list of synonyms have this fixed length? Why would there be just one entry in the dictionary?
We proceed by wishful thinking (Thank You) and write
matchup excellent1 (
(doubleplusgood, synonyms)
:
more )
| present excellent1 synonyms
=
doubleplusgood
And now we must also define this present
. But first, what should we do with more
? Under what condition?
matchup excellent1 (
(doubleplusgood, synonyms)
:
more )
| present excellent1 synonyms
=
doubleplusgood
| otherwise
=
somethingelse excellent1 more
But what should somethingelse
do? Isn't it exactly what matchup
is doing?
etc. etc. etc.
I think You can continue from here.