[Haskell-cafe] Is "take" behaving correctly?
byorgey at gmail.com
Tue Sep 11 19:38:18 EDT 2007
On 9/11/07, PR Stanley <prstanley at ntlworld.com> wrote:
> take 1000 [1..3] still yields [1,2,3]
> I thought it was supposed to return an error.
> Any ideas?
> Thanks, Paul
If for some reason you want a version that does return an error in that
situation, you could do something like the following:
take' n _ | (n <= 0) = 
take' n  | (n > 0) = error "take': list too short"
| otherwise = 
take' n (x:xs) = x : take' (n-1) xs
I'm not sure why you'd want that, though. The standard implementation
gracefully handles all inputs, and usually turns out to be what you want.
Really, if I were you, instead of making a version take' as above, I would
just use the standard take but check for the length of the list in the
places where it matters.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe