0
votes

I started learning F# and am confused about the type issues. for context i am searching for the maximum volume of a cylinder given a list of tuples containing radius, height

i am getting a "This expression was expected to have type 'float' but here has type 'float*float' when i call recMax hd t1

let CylinderVolume ((radius, height) : float*float) =
height * System.Math.PI * radius * radius

let maxCylinderVolume list : float =
    match list with
        | [] -> 0.0
        | hd :: t1 ->
            let rec recMax maxSoFar items = 
                match items with
                | [] -> maxSoFar
                | hd :: t1 ->
                    if (CylinderVolume hd) > maxSoFar then
                        recMax (hd) t1
                    else
                        recMax  maxSoFar t1
            recMax hd t1
1
You want the max volume, so shouldn't it be recMax (CylinderVolume hd) t1 instead? - Asti

1 Answers

0
votes

You got this error because you call recMax with hd which is a tuple when recMax requires a float.

let maxCylinderVolume lst =
    let rec recMax maxSoFar items =
        match items with
        | [] -> maxSoFar
        | h :: t -> let volume = cylinderVolume h
                    if maxSoFar < volume 
                    then recMax volume t 
                    else recMax maxSoFar t
    recMax 0.0 lst 

But you should have a look at the List module which provides functions to do what you want more simply.

let maxCylinderVolume = List.map cylinderVolume >> List.max