Re: [Haskell-cafe] Hi can u explain me how drop works in Haskell
ok maybe i should have read ahead. but still, i can see how to apply hunit, but not quickcheck. but quickcheck seems more powerful. On 2/26/07, Steve Downey <[EMAIL PROTECTED]> wrote: in addition, a good example of how to apply quickcheck would be really awesome. without using the standard drop On 2/26/07, Thomas Hartman <[EMAIL PROTECTED]> wrote: > Here's my, probably very obvious, contribution. > > What I'd like feedback on is > > 1) code seem ok? (hope so!) > 2) What do you think of the tests I did to verify that this > behaves the way I want? Is there a better / more idiomatic way to do > this? > > ** > > [EMAIL PROTECTED]:~/learning/haskell/lists$ cat drop.hs > mydrop :: Int -> [Int] -> [Int] > mydrop 0 xs = xs > mydrop n xs = mydrop (n-1) (tail xs) > > main = test > test = do print test1 > print test2 > print test3 > > test1 = mydrop 3 [1,2,3] == [] > test2 = mydrop 2 [1,2,3] == [3] > test3 = mydrop 0 [1,2,3] == [1,2,3] > [EMAIL PROTECTED]:~/learning/haskell/lists$ runghc drop.hs > True > True > True > > 2007/2/26, iliali16 <[EMAIL PROTECTED]>: > > > > Hi I am trying to implement the function drop in haskell the thing is that > I > > I have been trying for some time and I came up with this code where I am > > trying to do recursion: > > > > drop :: Integer -> [Integer] -> [Integer] > > drop 0 (x:xs) = (x:xs) > > drop n (x:xs) > > |n < lList (x:xs) = dropN (n-1) xs : > > |otherwise = [] > > > > So I want to understand how would this work and what exacttly should I put > > as an answer on line 4 couse that is where I am lost. I know I might got > the > > base case wrong as well but I don't know what to think for it. I have done > > the lList as a function before writing this one. Thanks to those who can > > help me understand this. Thanks alot in advance! Have a nice day! > > -- > > View this message in context: > http://www.nabble.com/Hi-can-u-explain-me-how-drop-works-in-Haskell-tf3290490.html#a9152251 > > Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. > > > > ___ > > 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] Hi can u explain me how drop works in Haskell
in addition, a good example of how to apply quickcheck would be really awesome. without using the standard drop On 2/26/07, Thomas Hartman <[EMAIL PROTECTED]> wrote: Here's my, probably very obvious, contribution. What I'd like feedback on is 1) code seem ok? (hope so!) 2) What do you think of the tests I did to verify that this behaves the way I want? Is there a better / more idiomatic way to do this? ** [EMAIL PROTECTED]:~/learning/haskell/lists$ cat drop.hs mydrop :: Int -> [Int] -> [Int] mydrop 0 xs = xs mydrop n xs = mydrop (n-1) (tail xs) main = test test = do print test1 print test2 print test3 test1 = mydrop 3 [1,2,3] == [] test2 = mydrop 2 [1,2,3] == [3] test3 = mydrop 0 [1,2,3] == [1,2,3] [EMAIL PROTECTED]:~/learning/haskell/lists$ runghc drop.hs True True True 2007/2/26, iliali16 <[EMAIL PROTECTED]>: > > Hi I am trying to implement the function drop in haskell the thing is that I > I have been trying for some time and I came up with this code where I am > trying to do recursion: > > drop :: Integer -> [Integer] -> [Integer] > drop 0 (x:xs) = (x:xs) > drop n (x:xs) > |n < lList (x:xs) = dropN (n-1) xs : > |otherwise = [] > > So I want to understand how would this work and what exacttly should I put > as an answer on line 4 couse that is where I am lost. I know I might got the > base case wrong as well but I don't know what to think for it. I have done > the lList as a function before writing this one. Thanks to those who can > help me understand this. Thanks alot in advance! Have a nice day! > -- > View this message in context: http://www.nabble.com/Hi-can-u-explain-me-how-drop-works-in-Haskell-tf3290490.html#a9152251 > Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. > > ___ > 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] Hi can u explain me how drop works in Haskell
I'd heard of quick check, but haven't got my head around it. This seems like a good place to start. I understand you have to build an invariant and then you can automate against it, eg "reverse of reverse is your original string" prop_RevRev xs = reverse (reverse xs) == xs where types = xs::[Int] (from http://www.kimbly.com/blog/42.html) but what would be your invariant with the drop function? At first I thought prop_HeadConsDropped xs = (head xs) : (drop 1 xs) == xs where types = xs::[a] But I think then you have the issue of head nil being an error. So, is there a better strategy? Or is using quickcheck here overkill? 2007/2/26, Antonio Cangiano <[EMAIL PROTECTED]>: On 2/26/07, Thomas Hartman <[EMAIL PROTECTED]> wrote: > Here's my, probably very obvious, contribution. > > What I'd like feedback on is > > 1) code seem ok? (hope so!) Hi Thomas, tail [] raises an error, therefore your code will fail when n > length xs ( e.g. mydrop 3 [1,2] will raise an exception, where [] is the expected result). Your function is also limited to list of Int only (mydrop :: Int -> [Int] -> [Int]). > 2) What do you think of the tests I did to verify that this > behaves the way I want? Is there a better / more idiomatic way to do > this? You may be interested in the following projects: QuickCheck: http://www.cs.chalmers.se/~rjmh/QuickCheck/ HUnit: http://sourceforge.net/projects/hunit Regards, Antonio -- http://antoniocangiano.com Zen and the Art of Ruby Programming ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Hi can u explain me how drop works in Haskell
On 2/26/07, Thomas Hartman <[EMAIL PROTECTED]> wrote: Here's my, probably very obvious, contribution. What I'd like feedback on is 1) code seem ok? (hope so!) Hi Thomas, tail [] raises an error, therefore your code will fail when n > length xs ( e.g. mydrop 3 [1,2] will raise an exception, where [] is the expected result). Your function is also limited to list of Int only (mydrop :: Int -> [Int] -> [Int]). 2) What do you think of the tests I did to verify that this behaves the way I want? Is there a better / more idiomatic way to do this? You may be interested in the following projects: QuickCheck: http://www.cs.chalmers.se/~rjmh/QuickCheck/ HUnit: http://sourceforge.net/projects/hunit Regards, Antonio -- http://antoniocangiano.com Zen and the Art of Ruby Programming ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Hi can u explain me how drop works in Haskell
Here's my, probably very obvious, contribution. What I'd like feedback on is 1) code seem ok? (hope so!) 2) What do you think of the tests I did to verify that this behaves the way I want? Is there a better / more idiomatic way to do this? ** [EMAIL PROTECTED]:~/learning/haskell/lists$ cat drop.hs mydrop :: Int -> [Int] -> [Int] mydrop 0 xs = xs mydrop n xs = mydrop (n-1) (tail xs) main = test test = do print test1 print test2 print test3 test1 = mydrop 3 [1,2,3] == [] test2 = mydrop 2 [1,2,3] == [3] test3 = mydrop 0 [1,2,3] == [1,2,3] [EMAIL PROTECTED]:~/learning/haskell/lists$ runghc drop.hs True True True 2007/2/26, iliali16 <[EMAIL PROTECTED]>: Hi I am trying to implement the function drop in haskell the thing is that I I have been trying for some time and I came up with this code where I am trying to do recursion: drop :: Integer -> [Integer] -> [Integer] drop 0 (x:xs) = (x:xs) drop n (x:xs) |n < lList (x:xs) = dropN (n-1) xs : |otherwise = [] So I want to understand how would this work and what exacttly should I put as an answer on line 4 couse that is where I am lost. I know I might got the base case wrong as well but I don't know what to think for it. I have done the lList as a function before writing this one. Thanks to those who can help me understand this. Thanks alot in advance! Have a nice day! -- View this message in context: http://www.nabble.com/Hi-can-u-explain-me-how-drop-works-in-Haskell-tf3290490.html#a9152251 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ 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] Hi can u explain me how drop works in Haskell
On 2/25/07, iliali16 <[EMAIL PROTECTED]> wrote: Hi I am trying to implement the function drop in haskell the thing is that I I have been trying for some time and I came up with this code where I am trying to do recursion: drop :: Integer -> [Integer] -> [Integer] drop 0 (x:xs) = (x:xs) drop n (x:xs) |n < lList (x:xs) = dropN (n-1) xs : |otherwise = [] drop :: Integer -> [a] -> [a] drop n xs | n < 1 = xs drop _ [] = [] drop n (_:xs) = drop (n-1) xs Line 1: It specifies that drop will accept an Integer and a list, and return a list; Line 2: If n < 1, the function will return the list as it is (this pattern is matched if you're dropping 0 or -2 elements, for example); Line 3: No matter what Integer has been passed to the function, if the list passed is empty, an empty list will be returned as well; Line 4: Dropping n elements from a list is equivalent to dropping n-1 elements from the tail (xs) of that same list. HTH Antonio -- http://antoniocangiano.com Zen and the Art of Ruby Programming ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Hi can u explain me how drop works in Haskell
Hi Iliali, You wrote: Hi I am trying to implement the function drop in haskell Chris Eidhof wrote: you're almost there... In case this is homework, you may also want to look at: http://www.haskell.org/haskellwiki/Homework_help Regards, Yitz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Hi can u explain me how drop works in Haskell
Hey, you're almost there: drop :: Integer -> [a] -> [a] drop 0 xs = xs drop n (x:xs) = drop (n-1) xs Your version fails when trying to do drop 10 [1..10]. My version fails when trying to do drop 10 [1..9], so you might want to try to see if you can come up with a solution for that! Good luck, -chris On 25 Feb, 2007, at 18:43 , iliali16 wrote: Hi I am trying to implement the function drop in haskell the thing is that I I have been trying for some time and I came up with this code where I am trying to do recursion: drop :: Integer -> [Integer] -> [Integer] drop 0 (x:xs) = (x:xs) drop n (x:xs) |n < lList (x:xs) = dropN (n-1) xs : |otherwise = [] So I want to understand how would this work and what exacttly should I put as an answer on line 4 couse that is where I am lost. I know I might got the base case wrong as well but I don't know what to think for it. I have done the lList as a function before writing this one. Thanks to those who can help me understand this. Thanks alot in advance! Have a nice day! -- View this message in context: http://www.nabble.com/Hi-can-u- explain-me-how-drop-works-in-Haskell-tf3290490.html#a9152251 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ 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