[Haskell-cafe] Is "take" behaving correctly?
derek.a.elkins at gmail.com
Tue Sep 11 22:04:11 EDT 2007
On Tue, 2007-09-11 at 16:48 -0700, Don Stewart wrote:
> > On 9/11/07, Don Stewart <dons at galois.com> wrote:
> > byorgey:
> > > On 9/11/07, PR Stanley <firstname.lastname@example.org> wrote:
> > >
> > > Hi
> > > 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
> > And we'd call it unsafeTake, just like unsafeFromJust and unsafeTail :-)
> > -- Don
> > Hmm, that's funny, I don't recall ever hearing of those functions... =)
> > ooo, Don has a shiny new e-mail address!
> And a shiny new job in a shiny new city :-)
In a new country.
More information about the Haskell-Cafe