[Haskell-cafe] Can I check the length in a guarded expresion

Bob Ippolito bob at redivi.com
Sun Nov 9 18:45:38 UTC 2014


You can do that. If you're getting an error, it's from a mistake on some
other line. That said, you shouldn't write it that way. length requires a
traversal of the entire list, so if the list is 1000 elements long it will
take 1001 steps to compute that result when two steps would do. `length
(take 2 a) == 1` would be a constant time way to get the result you want,
although likely not the most elegant way to solve this exercise.

Perhaps this is the sort of question better suited for the
haskell-beginners mailing list:
https://www.haskell.org/mailman/listinfo/beginners

On Sun, Nov 9, 2014 at 10:33 AM, Roelof Wobben <r.wobben at home.nl> wrote:

> Hello,
>
> I try to make a guarded solution to find the last item in a list.
>
> I thought I could do something like this
>
> | length a == 1 = a
>
> But then I see a lot of error messages.
>
> Is there another way I can check the length of a list with guards ?
>
> Roelof
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141109/dd1fa438/attachment.html>


More information about the Haskell-Cafe mailing list