Hi can u explain me how drop works in Haskell

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-tf329049... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

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

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

On 2/25/07, iliali16
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

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?
**********************************************
thartman@linodewhyou:~/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]
thartman@linodewhyou:~/learning/haskell/lists$ runghc drop.hs
True
True
True
2007/2/26, iliali16
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-tf329049... 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

On 2/26/07, Thomas Hartman
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

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/000042.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
On 2/26/07, Thomas Hartman
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

in addition, a good example of how to apply quickcheck would be really awesome.
without using the standard drop <g>
On 2/26/07, Thomas Hartman
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?
**********************************************
thartman@linodewhyou:~/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] thartman@linodewhyou:~/learning/haskell/lists$ runghc drop.hs True True True
2007/2/26, iliali16
: Hi I am trying to implement the function drop in haskell the thing is that
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
I 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-tf329049... 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

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
in addition, a good example of how to apply quickcheck would be really awesome. without using the standard drop <g>
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?
**********************************************
thartman@linodewhyou:~/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] thartman@linodewhyou:~/learning/haskell/lists$ runghc drop.hs True True True
2007/2/26, iliali16
: Hi I am trying to implement the function drop in haskell the thing is
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
On 2/26/07, Thomas Hartman
wrote: that 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-tf329049...
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

iliali16
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!
I'm curious as to the call to dropN. If you're trying to do recursion then you should probably call your original drop function (maybe your drop function was supposed to be called dropN?). Anyway, I think if you're only testing a single function and you're trying to come to grips with recursion and other fundamentals of functional programming, then sidetracking to become familiar with a testing suite such as Quickcheck or HUnit might be overkill. I prefer the method used by Thomas Hartman in his reply. However, it should be tweaked a little to really solidify the definition and verification of your drop function: drop :: Integral t => t -> [a] -> [a] drop _ [] = [] -- extra drop 0 (x:xs) = x:xs drop n (x:xs) = drop (n-1) xs main = test test = do print test1 print test2 print test3 print test4 -- extra test1 = drop 3 [1,2,3] == [] test2 = drop 2 [1,2,3] == [3] test3 = drop 0 [1,2,3] == [1,2,3] test4 = drop 4 [1,2,3] == [] -- extra Making your function polymorphic over the Integral class for it's first parameter would keep it working properly whether it receives an Int or an Integer, a useful property when your function is called by other functions as it's one less thing to keep track of. The extra case in the definition keeps the function from failing if n is greater than 'length (x:xs)'. The extra test (test4) verifies this property. Fernando Gonzalez

I did do something with quickcheck, more to learn quickCheck than
because it was really justified. Maybe it's overkill, maybe not,
whichever way here's what I have.
I would like prop_DroppedListLengthShorterOrSame to only test n > 0,
but haven't gotten around to figuring out how to restrict the inputs.
thomas.
$ cat drop.hs
import Test.QuickCheck
mydrop :: Int -> [a] -> [a]
mydrop _ [] = []
mydrop 0 xs = xs
mydrop n (x:xs) = mydrop (n-1) xs
-- Check that the length of a dropped list is always shorter or the same.
-- Anything greater than 0 should result in a shorter list, and a zero
-- argument should result in a list of the same length.
-- Note that ys has to be qualified as a list of *something* -- char,
Int, but something
-- Otherwise, won't compile
prop_DroppedListLengthShorterOrSame n ys = length (mydrop n ys) <= length( ys )
where types = ys ::[Int]
prop_DroppedListLengthSameIf0 ys = length (mydrop 0 ys) == length( ys )
where types = ys ::[Int]
sanitycheck = do print ( mydrop 3 [1,2,3] == [] )
print ( mydrop 2 [1,2,3] == [3] )
print ( mydrop 0 [1,2,3] == [1,2,3] )
print ( mydrop 4 [1,2,3] == [] )
main = do quickCheck prop_DroppedListLengthShorterOrSame
quickCheck prop_DroppedListLengthSameIf0
sanitycheck
$ runghc drop.hs
OK, passed 100 tests.
OK, passed 100 tests.
True
True
True
True
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.6
thartman@linodewhyou:~/learning/haskell/lists$
2007/2/28, Fernando Gonzalez
iliali16
writes: Hi I am trying to implement the function drop in haskell the thing is that
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
I 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!
I'm curious as to the call to dropN. If you're trying to do recursion then you should probably call your original drop function (maybe your drop function was supposed to be called dropN?).
Anyway, I think if you're only testing a single function and you're trying to come to grips with recursion and other fundamentals of functional programming, then sidetracking to become familiar with a testing suite such as Quickcheck or HUnit might be overkill. I prefer the method used by Thomas Hartman in his reply.
However, it should be tweaked a little to really solidify the definition and verification of your drop function:
drop :: Integral t => t -> [a] -> [a] drop _ [] = [] -- extra drop 0 (x:xs) = x:xs drop n (x:xs) = drop (n-1) xs
main = test test = do print test1 print test2 print test3 print test4 -- extra
test1 = drop 3 [1,2,3] == [] test2 = drop 2 [1,2,3] == [3] test3 = drop 0 [1,2,3] == [1,2,3] test4 = drop 4 [1,2,3] == [] -- extra
Making your function polymorphic over the Integral class for it's first parameter would keep it working properly whether it receives an Int or an Integer, a useful property when your function is called by other functions as it's one less thing to keep track of.
The extra case in the definition keeps the function from failing if n is greater than 'length (x:xs)'. The extra test (test4) verifies this property.
Fernando Gonzalez
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (7)
-
Antonio Cangiano
-
Chris Eidhof
-
Fernando Gonzalez
-
iliali16
-
Steve Downey
-
Thomas Hartman
-
Yitzchak Gale