Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional way to write these methods in F#

Tags:

idioms

f#

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:

  • Methods don't play well with type inference
  • Methods don't play well with higher order functions

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.

like image 529
Nick Avatar asked May 11 '15 14:05

Nick


People also ask

What is the method of function?

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.

What is function method in Java?

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.

How do you declare a method?

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.

What is functional programming example?

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.


1 Answers

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)
like image 119
svick Avatar answered Sep 29 '22 15:09

svick