[Haskell-cafe] What is your favourite Haskell "aha" moment?

Henrik Nilsson Henrik.Nilsson at nottingham.ac.uk
Wed Jul 11 17:25:02 UTC 2018


Hi,

On 07/11/2018 02:46 PM, Vanessa McHale wrote:
> #2: enumerate all strings on an alphabet (this uses laziness!)
>
>
> allStrings :: [a] -> [[a]] allStrings = sequence <=< (inits . repeat)
>

Neat!

I find the following alternative appealing too, as it in essence
just states a recursive equation that says what it means for
a list to be a list of all strings over the given alphabet:

allStrings alphabet = xss
     where xss = [] : [ x : xs | xs <- xss, x <- alphabet ]

(Admittedly, one has to be careful with the ordering of the generators,
or the order in which the strings are enumerated becomes less useful.)

This capability of declaratively stating an equation that characterises
the sought answer is another perspective on why techniques like
dynamic programming is such a great fit for lazy languages, as
pointed out by Jake.

One of my favourite examples is finding a minimal length triangulation
of a polygon where an elegant solution is obtained by just
transliterating the defining equations from a classic textbook on data
structures and algorithms (Aho, Hopcroft, Ullman 1983).

Attribute grammar evaluation is another great application of laziness
in a similar vein.

Best,

/Henrik




This message and any attachment are intended solely for the addressee
and may contain confidential information. If you have received this
message in error, please contact the sender and delete the email and
attachment. 

Any views or opinions expressed by the author of this email do not
necessarily reflect the views of the University of Nottingham. Email
communications with the University of Nottingham may be monitored 
where permitted by law.






More information about the Haskell-Cafe mailing list