F# (F Sharp) An Introductory Look By Kody Myers

advertisement
F# (F Sharp)
An Introductory Look
By Kody Myers
History
“Mathematicians stand on each others' shoulders and computer
scientists stand on each others' toes.”
- Richard Hamming, 1968 Turing Award recipient
Lambda Calculus (λ-calculus)
•
A formalism for representing
functions.
•
Formulated by Alonzo Church in the
1930s.
•
Formed the basis of functional
programming.
Early Functional Languages
•
Lisp – 1958
•
ISWIM (“If you See What I Mean”) – 1966
•
Metalanguage (ML) – 1973
•
Caml – 1985
•
Haskell - 1990
"There are only two kinds of
programming languages:
those people always bitch about and
those nobody uses.”
- Bjarne Stroustrup, Creator of C++
“Why Nobody Uses Functional
Languages”
•
Article appeared in the ACM
SIGPLAN Notices newsletter in 1998
by Philip Wadler.
•
Wadler was one of the principal
designers of Haskell.
[…] there are a large number of factors that hinder the widespread adoption
of functional languages. To be widely used, a language should support
interlanguage working, possess extensive libraries, be highly portable, have a
stable and easy to install implementation, come with debuggers and profilers,
be accompanied by training courses, and have a good track record on previous
projects.
-- Philip Wadler
Don Syme
•
Principal Researcher at Microsoft
Research, Cambridge, UK.
•
Was working on generics for C# for
the .NET Framework.
•
Tasked with finding a functional
language that would be a good fit
with .NET.
Haskell.NET?
We had a go doing Haskell for .NET.
We actually got a long way in doing
that, but in the end there is quite a lot
of dissonance between Haskell and
.NET.
- Don Syme
Okay, How About OCaml.NET, Then?
•
OCaml: Objective Caml
•
Dialect of Caml.
•
Descendent of Metalanguage (ML).
F# 1.0 – May 2005
•
Functional programming
•
Discriminated unions
•
Records
•
Tuples
•
Pattern matching
•
Type abbreviations
•
Object programming
•
Structs
•
Signature files
•
Scripting files
•
Imperative programming
•
Modules (no functors)
•
Nested modules
•
.NET Interoperability
F# 2.0 – April 2010
•
Active patterns
•
Units of measure
•
Sequence expressions
•
Asynchronous programming
•
Agent programming
•
Extension members
•
Named arguments
•
Optional arguments
•
Array slicing
•
Quotations
•
Native interoperability
•
Computation expressions
F# 3.0 – August 2012
•
Type providers
•
LINQ query expressions
•
CLIMutable attribute
•
Triple-quoted strings
•
Auto-properties
•
Provided units-of-measure
Present Day
F# 3.1 – October 2013
•
Named union type fields
•
Extensions to array slicing
•
Type inference enhancements
**Specification not yet finalized
Currently Active Keywords
abstract
downto
if
mutable
return
use!
and
elif
in
namespace
return!
val
as
else
inherit
new
select
void
assert
end
inline
not
static
when
base
exception
interface
null
struct
while
begin
extern
internal
of
then
with
class
false
lazy
open
to
yield
default
finally
let
or
true
yield!
delegate
for
let!
override
try
do
fun
match
private
type
done
function
member
public
upcast
downcast
global
module
rec
use
Inactive Keywords From OCaml
asr
lsl
mod
land
lsr
sig
lor
lxor
Reserved Keywords for Future Use
atomic
continue
method
sealed
break
eager
mixin
tailcall
checked
event
object
trait
component
external
parallel
virtual
const
fixed
process
volatile
constraint
functor
protected
constructor
include
pure
Introducing your
“let-saber”
“A more elegant weapon for a more civilized age.”
- Obi Wan Kenobi, Star Wars: A New Hope
“let” Us Begin
let hello = “Hello World!”
let pi = 3.1415926
let isRunning = false
let numList = [1 .. 10]
let vehicle = (2013, “Ford”, “Explorer”, true)
let userId = System.Guid.NewGuid()
Wow, Those Puns Were Terrible
•
Yeah, you’re probably right.
•
Time to lose these boring slides for awhile.
Advantages of F#
•
Highly readable, especially for developers who are already with functional
languages and .NET programming.
•
Highly writeable, especially developing with the help of the Visual Studio
IDE.
•
Highly reliable, thanks to the tried and heavily-tested libraries of the .NET
Framework.
•
Moderately portable to other, non-Windows platforms, thanks to the Mono
project.
Disadvantages of F#
•
Functional languages require an uncomfortable adjustment to the way
programmers normally think about problems.
•
Sacrifices a measure of performance in favor of reliability.
•
Hasn’t garnered a comparable amount of confidence as the other more wellestablished languages of the .NET Framework.
•
Practical solutions are limited to very specific class of problems.
The Future: F# 4.0+
•
Printf on unitized values
•
Extended #if grammar
•
Extension property initializers
•
Support for 'fixed'
•
Non-null provided types
•
Tailcall attribute
•
Class names as functions
•
Multiple interface instantiations
•
Static parameters for provided methods
•
Optional type args
•
Printf interpolation
•
Params dictionaries
A Sampling of Practical Applications
•
Unit conversion
•
Power generation load balancing and
scheduling
•
Insurance risk engine
•
Game rules engine
•
Website click prediction
•
Forensic image comparison
•
Genome sequencing
•
Game player ranking
let isFSharpUseful = true
Source:
http://fsharp.org/testimonials/
Suggested Resources
•
Try F# – tryfsharp.org
•
The F# Software Foundation – fsharp.org
•
F# for fun and profit – fsharpforfunandprofit.com
Download