In order to calculate the area of square and circle, I defined the following type:
type Square = {width: float; length: float;} with
member this.area = this.width * this.length
member this.perimeter = (this.width + this.length) * 2.
type Circle = {r:float} with
member this.area = System.Math.PI * this.r * this.r
member this.perimeter = 2. * System.Math.PI * this.r
let s1 = {width = 3.; length = 4.}
let c1 = {r = 8.3}
printfn "%A" s1
printfn "The area of s1 is: %A" s1.area
printfn "The perimeter of s1 is: %A" s1.perimeter
printfn "%A" c1
printfn "The area of c1 is: %A" c1.area
printfn "The perimeter of c1 is: %A" c1.perimeter
When I read this article: http://fsharpforfunandprofit.com/posts/type-extensions/
It states:
So, a plea for those of you new to functionally programming. Don't use methods at all if you can, especially when you are learning. They are a crutch that will stop you getting the full benefit from functional programming.
Then what's the functional way to solve this problem? or what's the idomatic F# way?
Edit:
After reading the "The F# Component Design Guidelines" (curtsy to @V.B.), and @JacquesB's comment, I consider that implement the member method within the type is the most simple, intrinsic way:
type Square2 (width: float, length: float) =
member this.area = width * length
member this.perimeter = (width + length) * 2.
(This is almost identical with my original Square
type -- this Square2
only saves seveal this.
prefix as in this.width
, this.length
.)
Again, the The F# Component Design Guidelines is quite useful.
Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program.
A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.
The only required elements of a method declaration are the method's return type, name, a pair of parentheses, () , and a body between braces, {} . More generally, method declarations have six components, in order: Modifiers—such as public , private , and others you will learn about later.
Functional programming is based on mathematical functions. Some of the popular functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, etc. Pure Functional Languages − These types of functional languages support only the functional paradigms. For example − Haskell.
A more functional way to do this would be to create a Shape
discriminated union, where Square
and Circle
would be its cases. Then create functions area
and perimeter
, taking Shape
and using pattern matching:
type Shape =
| Square of Width: float * Length: float
| Circle of R: float
let area = function
| Square (width, length) -> width * length
| Circle r -> System.Math.PI * r * r
let perimeter = function
| Square (width, length) -> (width + length) * 2.
| Circle r -> 2. * System.Math.PI * r
let s1 = Square(Width = 3., Length = 4.)
let c1 = Circle(R = 8.3)
printfn "%A" s1
printfn "The area of s1 is: %A" (area s1)
printfn "The perimeter of s1 is: %A" (perimeter s1)
printfn "%A" c1
printfn "The area of c1 is: %A" (area c1)
printfn "The perimeter of c1 is: %A" (perimeter c1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With