Re: [Haskell-cafe] Empty Input list
Look also at safe package http://hackage.haskell.org/package/safe 2012/3/13 Chris Wong chrisyco+haskell-c...@gmail.com On Tue, Mar 13, 2012 at 12:24 PM, Chris Smith cdsm...@gmail.com wrote: On Mon, Mar 12, 2012 at 3:14 PM, Kevin Clees k.cl...@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Empty Input list
Kevin Clees k.cl...@web.de writes: Now my function looks like this: tmp:: [(Int, Int)] - Int - (Int, Int) tmp [] y = (0,0) ^ tmp xs y = xs !! (y-1) If the function returns (0,0) it will blocked by another function. Personally, I think using special values like this is a code smell, and indicates poor design. There are many implicit assumptions, for instance that (0,0) isn't already a member of the input list, and that this is correctly handled by surrounding functions. Generally, it's much more desirable to encode this in the types, so I would vastly prefer the Maybe solution in almost all cases, which makes these assumptions explicit. -k -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Empty Input list
Dear Haskell friends, what can I do, if a function gets an empty input list? I want, that it only returns nothing. This is my source code: tmp:: [(Int, Int)] - Int - (Int, Int) tmp (x:xs) y | y == 1 = x | y 1 = tmp xs (y-1) If this function gets an empty list, he throws Exception: sortAlgo.hs:(18,1)-(21,44): Non-exhaustive patterns in function Main.tmp *Main tmp [(1,2),(3,2)] 1 (1,2) *Main tmp [(1,2),(3,2)] 2 (3,2) *Main tmp [] 1 *** Exception: sortAlgo.hs:(20,1)-(22,44): Non-exhaustive patterns in function Main.listElementIntInt Thank you for any help ! Best regards Kevin ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Empty Input list
On Mon, Mar 12, 2012 at 2:41 PM, Kevin Clees k.cl...@web.de wrote: what can I do, if a function gets an empty input list? I want, that it only returns nothing. This is my source code: tmp:: [(Int, Int)] - Int - (Int, Int) tmp (x:xs) y | y == 1 = x | y 1 = tmp xs (y-1) It's not clear what you mean by returns nothing when the result is (Int, Int)... there is no nothing value of that type. But you can add another equation to handle empty lists one you decide what to return in that case. For example, after (or before) the existing equation, add: tmp [] y = (-1, -1) Or, you may want to use a Maybe type for the return... which would mean there *is* a Nothing value you can return: tmp:: [(Int, Int)] - Int - Maybe (Int, Int) tmp (x:xs) y | y == 1 = Just x | y 1 = tmp xs (y-1) tmp [] y = Nothing Does that help? -- Chris Smith ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Empty Input list
Oh, and just to point this out, the function you're writing already exists in Data.List. It's called (!!). Well, except that it's zero indexed, so your function is more like: tmp xs y = xs !! (y-1) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Empty Input list
Hey Chris, thank you for your help! Your last comment with the (!!)-thing was a very good idea! Now my function looks like this: tmp:: [(Int, Int)] - Int - (Int, Int) tmp [] y = (0,0) tmp xs y = xs !! (y-1) If the function returns (0,0) it will blocked by another function. If I want to use the maybe return, I get some new trouble that I don't like - so I chose that way :) Thank you again Kevin ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Empty Input list
On Mon, Mar 12, 2012 at 3:14 PM, Kevin Clees k.cl...@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 -- Chris Smith ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Empty Input list
On Tue, Mar 13, 2012 at 12:24 PM, Chris Smith cdsm...@gmail.com wrote: On Mon, Mar 12, 2012 at 3:14 PM, Kevin Clees k.cl...@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe