SSOPLIF
SSOPLIF

Reputation: 321

Parsing nested list F# confused

I have a text file of rain measured over 3 years, where each number after the year corresponds to the month. For example, in

2002    1.17    0.78    7.11    5.17    5.84    4.29    1.12    4.06    1.9 2.07    1.47    1.53
2001    1.24    3.22    1.61    3.33    6.55    2.4 3.5 1.32    3.9 6.04    1.69    1.13
2000    1.277   1.4 1.17    5.74    6.48    4.81    4.07    3.19    6.2 1.95    2.65    1.7

In 2002, Average rainfall in Feb was 0.78.

I made a list of tuples, in the format (year,values,average,min,max) where years is int, values is a float list, average is an int that averages all of 'values', min is an int holding the smallest 'value' and max.

My question:

I am able to print the smallest value from all the tuples using List.minBy fourth mylist and the year it came from (because its single element), but how do I correspond that number to the month it came from?

I have

let TupleWithSmallestValue= List.minBy min biglist
 let (year,_,_,min,_) = TupleWithSmallestValue
 printfn"Min rainfall:\t%A; %A" min year

and I was thinking something along the lines of :

List.map (fun (year,(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec), avg, min, max) -> somehow print the min and the month it came from )

But I know that's wrong and I'm trying to address each value to make it correspond to a month, just like I did with my example above. If my question is clear enough, how do I do this? I am a C++ programmer but I like the style of this language. Just missing some basics.

Upvotes: 2

Views: 210

Answers (1)

rkrahl
rkrahl

Reputation: 1177

Something like this should work:

 let months = [ "Jan"; "Feb"; ...; "Dec" ]
 let (year, values, _, min, _) = TupleWithSmallestValue
 let month = values |> List.mapi (fun i x -> (i, x)) 
                    |> List.find (fun (m, v) -> v = min) 
                    |> fst 
                    |> List.nth months

Upvotes: 1

Related Questions