57 people like it.

Wicked way to solve quadratic equation using list of operators

This is to demonstrate that: (1) there are many ways to solve the same problems; (2) operators can be grouped together into data structures and act as data; (3) you can have fun in F# in many ways.

1: 
2: 
3: 
4: 
5: 
let solve a b c =
  let D = b*b-4.*a*c in
  [(+);(-)] |> List.map (fun f -> (f -b (sqrt D))/2./a)

solve 1.0 2.0 -3.0
val solve : a:float -> b:float -> c:float -> float list

Full name: Script.solve
val a : float
val b : float
val c : float
val D : float
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val f : (float -> float -> float)
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
Raw view New version

More information

Link:http://fssnip.net/38
Posted:6 years ago
Author:Dmitry Soshnikov
Tags: lists , learning f# , wicked