[Haskell-cafe] Empty Input list
Dmitry Olshansky
olshanskydr at gmail.com
Tue Mar 13 07:29:50 CET 2012
Look also at safe package <http://hackage.haskell.org/package/safe>
2012/3/13 Chris Wong <chrisyco+haskell-cafe at gmail.com>
> On Tue, Mar 13, 2012 at 12:24 PM, Chris Smith <cdsmith at gmail.com> wrote:
> > On Mon, Mar 12, 2012 at 3:14 PM, Kevin Clees <k.clees at web.de> wrote:
> >> Now my function looks like this:
> >>
> >> tmp:: [(Int, Int)] -> Int -> (Int, Int)
> >> tmp [] y = (0,0)
> >> tmp xs y = xs !! (y-1)
> >
> > Just a warning that this will still crash if the list is non-empty by
> > the index exceeds the length. That's because your function is no
> > longer recursive, so you only catch the case where the top-level list
> > is empty. The drop function doesn't crash when dropping too many
> > elements though, so you can do this and get a non-recursive function
> > that's still total:
> >
> > tmp :: [(Int,Int)] -> Int -> (Int, Int)
> > tmp xs y = case drop (y-1) xs of
> > [] -> (0,0)
> > Just (x:_) -> x
>
> That last line should be
>
> (x:_) -> x
>
> without the "Just". Hopefully that'll save a bit of confusion.
>
> Chris
>
> > --
> > Chris Smith
> >
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-Cafe at haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
>
> _______________________________________________
> 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/20120313/6c8047d5/attachment.htm>
More information about the Haskell-Cafe
mailing list