[Haskell-beginners] Are these soloutions all valid and a good use of Haskell

Frerich Raabe raabe at froglogic.com
Mon Nov 10 10:43:56 UTC 2014


On 2014-11-10 11:16, Karl Voelker wrote:
> On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote:
>> What do you experts think of the different ways ?
> 
> 2 and 4 are quite similar, and both fine. 3 is not so good: guards
> provide weaker guarantees than patterns, and head and tail are partial
> functions.

In addition to what Karl wrote, I'd like to suggest not using the semicolon 
all the time -- it's not needed and just adds noise.

> All three implementations have in common that they do their own
> recursion. It would be a good exercise to try implementing last as a
> fold - in other words, letting the standard library do the recursion for
> you.

Right - I suggest trying to express the problem with the most abstract
function first, then consider using a fold, then use manual recursion. in
your particular case you could exploit that for non-empty lists, getting
the last element of a list is the same as getting the first element of
a reversed list (and there are ready-made functions for reversing a list
and getting the first element of a list).

-- 
Frerich Raabe - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing


More information about the Beginners mailing list