I started learning Haskell and found a nice exercise. It's the following:
grouping: Int -> [Student]->[(Team, Student)]
grouping teamNumber = zip ys
where ...
So, the exercise wants that i try to fill the rest. The function should do the following:
Example : grouping 2 ['Mark','Hanna','Robert','Mike','Jimmy'] = [(1,'Mark'),(2,'Hanna'),(1,'Robert'),(2,'Mike'),(1,'Jimmy')]
.
So, we are building teams which consists of two Students, and the last Student 'Jimmy' has no teammates.
Then, I also look up what the predefined function zip
does. It gets two list arguments and connects each element of the lists to a tuple to build a list of tuples.
My idea: 1) I try to build two functions "grab" and "infinite". They are looking as following:
grap :: Int -> [a] -> [a]
grab _ [] = []
grab n (x:xs) = if n <= 0 then [] else x : grab (n-1) xs
infinite :: Num a => a -> [a]
infinite x = x : infinite(x+1)
So, what they do is: With infinite
I want to create an infinite list. And grap
should take n
elements of that. Example grap 2 (infinite 1) = [1,2]
.
I use these two in the first line of my where-declaration to fulfill the given function from above. So, I have:
grouping: Int -> [Student]->[(Team, Student)]
grouping teamNumber = zip ys
where
xs = grap teamNumber (infinite 1)
So, xs
is now my first list of zip
, especially the integer-list.
But now my question: zip
as predefined function requires also a second list, especially the list of the names of the students, but in the given function they give zip only one argument, namely the ys
as a list. How can I understand that?