I am very new to haskell, and it's by far the hardest language I found to wrap my mind around. I've found a past paper online and I decided to try to implement this in haskell for practice.
I have a list of integers like the following in myFile.txt:
5
3 30
10 120
80 96
95 96
98 98
Where the First number in the list has an integer to tell me how many tests will follow. In this case, 5 will follow.
I am trying to return a single number(eg: 20) for each test(eg: 3 30) that signifies the total multiples that are in a particular range.
As an example of the first test, 3 30:
I need to find the multiples of each number from 2 to the first number (In this case, 2 and 3), up to the value 30. In this case:
Multiples of 2: [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
Multiples of 3: [3,6,9,12,15,18,21,24,27,30]
I then find what are similar and then count all unique values:
[2,3,4,6,8,9,10,12,14,15,16,18,20,21,22,24,26,27,28,30]
The Size of the final list in number 2 is 20, and that's the value I would like to return
With that being said, I have ideas on how to do this implementation. My plans to do this implementation are:
- Populate a single list with values that are multiples of numbers that are lesser and equal to the first number received.
- Take the populated list, and group it.
- Take the Length of group, and print the value.
To Start, I've done the following to read all the input, store into a list and pass it to a function to print it to confirm that I've received the right value:
main = do
let myList = []
handle <- openFile "myFile.txt" ReadMode
contents <- hGetContents handle
let singlewords = words contents
myList = f singlewords
printMyVals myList
hClose handle
f :: [String] -> [Integer]
f = map read
printMyVals :: [Integer] -> IO()
printMyVals myList =
print myList
At this point, I am stuck.
I am trying to figure out how I can iterate the values of my list, and use those values to populate another list with it's multiples, like stated above.
Any help on this?