
I tried and so far I have this : last5 = foldl(\acc x -> if x=[] then x else acc xs) [] xs But now I get parse error on the -> part. Roelof Roelof Wobben schreef op 10-11-2014 13:47:
Thanks all,
I will try to make a fold solution as soon as I see that functional explained in the learnyouahaskell.
Roelof
Frerich Raabe schreef op 10-11-2014 11:43:
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).
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners