LOST ::::::: rec Lost ..... lists, paterns and averages

I am looking to do 3 things… I have tried, retried and want to kill myself right now… I would just like some help in what would be the best way… If we can talk in private that would be good because i need to understand what im doing wrong —
so I have a list with and topic goes to n
(topic1:hello; topic1subtitle1:5; topic1subtitle:7; etc… ) I have been able to present this list…
Now i am trying to get the average of
topic1subtitle1;5;topic1subtitle:7
and the maximum average from a list so i can present the result…
Ive tried converting list to array, trying to create a seperate list, but i am still getting nowhere
and the more i try the worst things get

I would like to help but cannot understand clearly what the question is. Are you trying to compute the average value in a list of numbers? In an association list? Please make your goal clear and I will be glad to think about a solution.

to make it easier here is the what is required.
The data was given in a file and I am able to print out the entire file with list
“read_lines filetemp
|> List.iter(Printf.printf " |%s\n”);
but now no matter what I try I can’t get the rest to work…

So correct me if I’m wrong but you’re trying to take data structured as follows:

week<number>: <year> ;  [<day>:<value> ...]

Find which year the average of day 1 and day 2 is the highest?

In that you probably want to split the problem up.

(using Core will probably also help here for utility functions)

First parse your file, the easiest way to do that is use the String.split line ~on:';' to split it up into the coumns, then you can split on : to get the values of each of the fields (you’ll need to use Int.of_string to get the values). I’d recommend reading the file into a structure like:

type line = {
  year: int
; week: int
; days: int list
}

Then you can iterate through a list of these lines and get what calculation you want done.

Hello Cjen
Yes, that is exactly that…
but 2 things:
Error: This expression has type string but an expression was expected of type
int
and It’s mandatory to use lists

So if its of type string you will need to do the Int.of_string on it.

So one thing you could do then is List.fold to iterate over all the lines, it allows you to keep some values between iterations (i.e. your maximum average for all the previous lines).

Also as a general problem solving point, if you break the problem down into individual parts it should then be easier to solve (in this case, split into parsing what the file actually is, then doing the calculation).

I’ll try that and put the code here if i can get past it…
this is so confusing for me

This is my code
What am I doing wrong.
I am trying to get the average of the 1st line
so that I can then do the List.fold
let average= let average int_of_string(a) int_of_string(b) =
a /. b
;;

let val j =
match j with
| (x,y,z) -> z

read_lines weektemp
|> List.iter(fun average -> Printf.printf “%.f\n”) j;

Hi, this code doesn’t make sense. I recommend stepping back from the code for a minute and writing down just a step-by-step plan of how to solve the problem in general. Then you can get back to translating that into OCaml.

1 Like