SlideShare a Scribd company logo
1 of 34
Introduction to F# Talbott Crowell
Functional programming has been around a long time Not new Long history Functional programming is safe A concern as we head toward manycore and cloud computing Functional programming is on the rise Why another language?
1930’s: lambda calculus (roots) 1956: IPL (Information Processing Language) “the first functional language 1958: LISP “a functional flavored language” 1962: APL (A Programming Language) 1973: ML (Meta Language) 1983: SML (Standard ML) 1987: Caml (Categorical Abstract Machine Language ) and Haskell 1996: OCaml (Objective Caml) Functional programming has been around a long time
Side effects Modifies some state Has observable interaction with calling functions  Has observable interaction with the outside world Example: a function or method with no return value Most functional languages encourage programmers to avoid side effects Haskell (a “pure” functional language) restricts side effects with a static type system Functional programming is safe
Language Evolution (Simon Payton-Jones) C#, VB, Java, C are imperative programming languages.  Very useful but can change the state of the world at anytime creating side effects. Nirvana! Useful and Safe F# Haskell is Very Safe, but not very useful.  Used heavily in research and academia, but rarely in business . http://channel9.msdn.com/posts/Charles/Simon-Peyton-Jones-Towards-a-Programming-Language-Nirvana/
Long relegated to academia Industrial and commercial applications Scheme, Erlang, OCaml, Haskell F# at Grange Insurance, Ohio: http://bit.ly/GrangeFSharp F# used by XBox Live Trueskill:  http://bit.ly/trueskill DSL’s Mathematica XSLT Excel can be viewed as a functional programming language Functional programming on the rise
Functional language developed by Microsoft Research By Don Syme and his team, who productized Generics Based on OCaml (influenced by C# and Haskell) History 2002: F# language design started 2005 January: F# 1.0.1 releases to public Not a product.  Integration with VS2003 Works in .NET 1.0 through .NET 2.0 beta, Mono 2005 November: F# 1.1.5 with VS 2005 RTM support 2009 October: VS2010 Beta 2, CTP for VS2008 & Non-Windows users 2010: F# is “productized” and baked into VS 2010 What is F#
Interactive Scripting Good for prototyping Succinct = Less code Type Inference Strongly typed, strict Automatic generalization (generics for free) Few type annotations 1st class functions (currying, lazy evaluations) Pattern matching Key Characteristics of F#
Multi-Paradigm Functional Programming Imperative Programming Object Oriented Programming Language Oriented Programming Task Oriented Programming F# is not just Functional
Dynamically compiles code on the fly Example: Demo F# Interactive open System.Drawing;;  open System.Windows.Forms;;  let myform = new Form(Text="Hello Everybody", Visible=true);;  myform.BackColor <- Color.Blue;; let mybutton = new Button(Text = "Click Me");; myform.Controls.Add(mybutton);;  mybutton.BackColor <- Color.White;;  mybutton.Click.Add(fun _ -> MessageBox.Show("Clicked") |> ignore);;
Type inference Expressions F# Basics let x = 5 let y = 5.0  let files = Directory.GetFiles(@"C:magesriginal") let x = 5 * 5 let y = 5.0 / 3.0 let width = image.Width / 8
Function Anonymous functions F# Functions let sqr x = x * x sqr 5 (fun x -> x * x) 5
The |> Combinator “Pipe Forward”  Example F# Combinators  x |> f          is the same as        f x   let sqr x = x * x sqr x   5 |> sqr
Partial functions Don’t have to pass all parameters at once Curried Functions letDivideBy y x = x / y let result = DivideBy 2.0 3.0 valDivideBy : float -> float -> float val result : float = 1.5 let HalfOf = DivideBy 2.0 let result2 = HalfOf 3.0 valHalfOf : (float -> float) val result2 : float = 1.5
Pass curried function to another function Power of Curried Functions letDivideBy y x = x / y letnumbers = [1.0; 2.0; 3.0] let result3 = List.map (DivideBy 2.0) numbers valDivideBy : float -> float -> float val numbers : float list = [1.0; 2.0; 3.0] val result3 : float list = [0.5; 1.0; 1.5]
Compare with VB Function DivideBy(ByVal y As Single, _ ByVal x As Single) As Single     Return x / y End Function Sub Main()     Dim numbers = New Single() {1.0, 2.0, 3.0}     Dim result3 As New List(Of Single)     For Each number In numbers         result3.Add(DivideBy(2.0, number))     Next End Sub
used to group values together without creating a class or struct Tuple Tuple as a function parameter #light open System let swap (x, y) = (y, x) let rotate (x, y, z) = (y, z, x) let circleDetails radius =     let area = Math.PI * (radius * radius)     let circumference = 2.0 * Math.PI * radius     (area, circumference) let result1 = swap(3, 2) let result2 = rotate(5, 6, 7) let details = circleDetails 4.0 let (a, c) = details Tuple as a return value Tuple as left side of an assignment
Similar to Object Oriented inheritance Useful in pattern matching Discriminated Unions type Suit =   | Spades   | Hearts   | Clubs   | Diamonds let suits = [Spades; Hearts; Clubs; Diamonds]
Discriminated Unions cont. type Suit =   | Spades   | Hearts   | Clubs   | Diamonds type Card =   | Ace of Suit   | RankCard of int * Suit   | Jack of Suit   | Queen of Suit   | King of Suit   | Joker let card1 = Ace(Diamonds) let card2 = RankCard(3, Spades) let card3 = Joker
Used often in F# programs Syntax (source: http://en.wikibooks.org/wiki/F_Sharp_Programming/Pattern_Matching_Basics ) Pattern Matching
Mouse event handler in DirectX Demo Checking mouse button and shift key modifiers Pattern Matching Example match b.Button, Form.ModifierKeys with    | MouseButtons.Left, Keys.Shift -> view.AdjustZoom(dx,dy)   | MouseButtons.Left, _ ->  view.AdjustYawPitchRoll(dx,dy)   | _                    ->         view.AdjustFocus(dx,dy)
FSharp Samples v0.2  The DirectX Demo More F# Interactive http://code.msdn.microsoft.com/fsharpsamples
The Power Wall Manycore FUD Free lunch is over Parallel programming in .NET Where F# fits into this future F# and Impact on the Future
The Power Wall: CPU Clock Speed From Katherine Yelick’s “Multicore: Fallout of a Hardware Revolution”
Fear, Uncertainty, and Doubt Talbott’s FUD slide: why you should be concerned Moore’s Law Predicted in 1965 that transistor density in semiconductor chips would double every 18 months Clock speed on chips plateaued in 2005 at 3 GHz due to the “Power Wall” Intel and AMD have now begun doubling cores every 18 months to keep up with Moore’s Law AMD Opteron Istanbul and Intel Xeon have 6 cores per socket used in Servers Dell Studio XPS (2009 price of $750) comes with Intel i7 with 4 cores with 2 way hyper-threading (8 virtual cores) Intel Larrabee on the horizon with 32 cores with 4 way hyper-threading (128 virtual cores) FUD
Source: Wikipedia entry for Larrabee (GPU) Intel Larrabee
Free lunch is over Programs are not doubling in speed every couple of years for free anymore We need to start writing code to take advantage of many cores Currently painful and problematic to take advantage of many cores because of shared memory, locking, and other imperative programming techniques Problem
Libraries and Framework Improvements TPL (Included in .NET 4.0) PLINQ Programming Approaches Functional programming Immutable data No side effects Task oriented programming Solution
Parallel Programming in the .NET Framework 4 Beta 2
“F# is, technically speaking, neutral with respect to concurrency - it allows the programmer to exploit the many different techniques for concurrency and distribution supported by the .NET platform”  F# FAQ:  http://bit.ly/FSharpFAQ Functional programming is a primary technique for minimizing/isolating mutable state Asynchronous workflows make writing parallel programs in a “natural and compositional style” F# and Multi-Core Programming
Problem Resize a ton of images Demo of Image Processor let files = Directory.GetFiles(@"C:magesriginal") for file in files do     use image = Image.FromFile(file)     use smallImage = ResizeImage(image)     let destFileName = DestFileName("s1", file) smallImage.Save(destFileName)
Asynchronous Workflows let FetchAsync(file:string) = async {         use stream = File.OpenRead(file)         let! bytes = stream.AsyncRead(intstream.Length)         use memstream = new MemoryStream(bytes.Length) memstream.Write(bytes, 0, bytes.Length)         use image = Image.FromStream(memstream)         use smallImage = ResizeImage(image)         let destFileName = DestFileName("s2", file) smallImage.Save(destFileName)     } let tasks = [for file in files -> FetchAsync(file)] let parallelTasks = Async.Parallel tasks Async.RunSynchronouslyparallelTasks
Almost 2x on 2 cores About 3.5x faster on 8 cores Demo Asynchronous Workflow
Thank you for attending Intro to F#Questions? F# User Group http://fsug.org Twitter: @FSUG Talbott Crowell, ThirdM.com http://talbottc.spaces.live.com Twitter: @Talbott

More Related Content

Similar to Introduction to F#

Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpMichael Stal
 
Exploring SharePoint with F#
Exploring SharePoint with F#Exploring SharePoint with F#
Exploring SharePoint with F#Talbott Crowell
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingVijaySharma802
 
Plug-in Architectures
Plug-in ArchitecturesPlug-in Architectures
Plug-in Architectureselliando dias
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010Satish Verma
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharpDaniele Pozzobon
 
Boo Manifesto
Boo ManifestoBoo Manifesto
Boo Manifestohu hans
 
Core .NET Framework 4.0 Enhancements
Core .NET Framework 4.0 EnhancementsCore .NET Framework 4.0 Enhancements
Core .NET Framework 4.0 EnhancementsRobert MacLean
 
Programing paradigm & implementation
Programing paradigm & implementationPrograming paradigm & implementation
Programing paradigm & implementationBilal Maqbool ツ
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Hermann Hueck
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1SURBHI SAROHA
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLVijaySharma802
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesEelco Visser
 
.NET 4 Demystified - Sandeep Joshi
.NET 4 Demystified - Sandeep Joshi.NET 4 Demystified - Sandeep Joshi
.NET 4 Demystified - Sandeep JoshiSpiffy
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMCoding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMRaveen Perera
 

Similar to Introduction to F# (20)

Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
F# Tutorial @ QCon
F# Tutorial @ QConF# Tutorial @ QCon
F# Tutorial @ QCon
 
Exploring SharePoint with F#
Exploring SharePoint with F#Exploring SharePoint with F#
Exploring SharePoint with F#
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Plug-in Architectures
Plug-in ArchitecturesPlug-in Architectures
Plug-in Architectures
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010
 
IN4308 1
IN4308 1IN4308 1
IN4308 1
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
Boo Manifesto
Boo ManifestoBoo Manifesto
Boo Manifesto
 
Core .NET Framework 4.0 Enhancements
Core .NET Framework 4.0 EnhancementsCore .NET Framework 4.0 Enhancements
Core .NET Framework 4.0 Enhancements
 
Erlang, an overview
Erlang, an overviewErlang, an overview
Erlang, an overview
 
Programing paradigm & implementation
Programing paradigm & implementationPrograming paradigm & implementation
Programing paradigm & implementation
 
Srgoc dotnet_new
Srgoc dotnet_newSrgoc dotnet_new
Srgoc dotnet_new
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIML
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific Languages
 
.NET 4 Demystified - Sandeep Joshi
.NET 4 Demystified - Sandeep Joshi.NET 4 Demystified - Sandeep Joshi
.NET 4 Demystified - Sandeep Joshi
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMCoding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBM
 

More from Talbott Crowell

Top 3 Mistakes when Building
Top 3 Mistakes when BuildingTop 3 Mistakes when Building
Top 3 Mistakes when BuildingTalbott Crowell
 
Building high performance and scalable share point applications
Building high performance and scalable share point applicationsBuilding high performance and scalable share point applications
Building high performance and scalable share point applicationsTalbott Crowell
 
Road to the Cloud - Extending your reach with SharePoint and Office 365
Road to the Cloud - Extending your reach with SharePoint and Office 365Road to the Cloud - Extending your reach with SharePoint and Office 365
Road to the Cloud - Extending your reach with SharePoint and Office 365Talbott Crowell
 
Custom Development for SharePoint
Custom Development for SharePointCustom Development for SharePoint
Custom Development for SharePointTalbott Crowell
 
Custom Development in SharePoint – What are my options now?
Custom Development in SharePoint – What are my options now?Custom Development in SharePoint – What are my options now?
Custom Development in SharePoint – What are my options now?Talbott Crowell
 
Developing a Provider Hosted SharePoint app
Developing a Provider Hosted SharePoint appDeveloping a Provider Hosted SharePoint app
Developing a Provider Hosted SharePoint appTalbott Crowell
 
Developing a provider hosted share point app
Developing a provider hosted share point appDeveloping a provider hosted share point app
Developing a provider hosted share point appTalbott Crowell
 
PowerShell and SharePoint @spsnyc July 2012
PowerShell and SharePoint @spsnyc July 2012PowerShell and SharePoint @spsnyc July 2012
PowerShell and SharePoint @spsnyc July 2012Talbott Crowell
 
PowerShell and SharePoint
PowerShell and SharePointPowerShell and SharePoint
PowerShell and SharePointTalbott Crowell
 
Automating PowerShell with SharePoint
Automating PowerShell with SharePointAutomating PowerShell with SharePoint
Automating PowerShell with SharePointTalbott Crowell
 
SharePoint Saturday Boston 2010
SharePoint Saturday Boston 2010SharePoint Saturday Boston 2010
SharePoint Saturday Boston 2010Talbott Crowell
 
Automating SQL Server Database Creation for SharePoint
Automating SQL Server Database Creation for SharePointAutomating SQL Server Database Creation for SharePoint
Automating SQL Server Database Creation for SharePointTalbott Crowell
 
Architecting Solutions for the Manycore Future
Architecting Solutions for the Manycore FutureArchitecting Solutions for the Manycore Future
Architecting Solutions for the Manycore FutureTalbott Crowell
 

More from Talbott Crowell (17)

Top 7 mistakes
Top 7 mistakesTop 7 mistakes
Top 7 mistakes
 
Top 3 Mistakes when Building
Top 3 Mistakes when BuildingTop 3 Mistakes when Building
Top 3 Mistakes when Building
 
Building high performance and scalable share point applications
Building high performance and scalable share point applicationsBuilding high performance and scalable share point applications
Building high performance and scalable share point applications
 
Road to the Cloud - Extending your reach with SharePoint and Office 365
Road to the Cloud - Extending your reach with SharePoint and Office 365Road to the Cloud - Extending your reach with SharePoint and Office 365
Road to the Cloud - Extending your reach with SharePoint and Office 365
 
Custom Development for SharePoint
Custom Development for SharePointCustom Development for SharePoint
Custom Development for SharePoint
 
Custom Development in SharePoint – What are my options now?
Custom Development in SharePoint – What are my options now?Custom Development in SharePoint – What are my options now?
Custom Development in SharePoint – What are my options now?
 
Developing a Provider Hosted SharePoint app
Developing a Provider Hosted SharePoint appDeveloping a Provider Hosted SharePoint app
Developing a Provider Hosted SharePoint app
 
Developing a provider hosted share point app
Developing a provider hosted share point appDeveloping a provider hosted share point app
Developing a provider hosted share point app
 
Introduction to F# 3.0
Introduction to F# 3.0Introduction to F# 3.0
Introduction to F# 3.0
 
PowerShell and SharePoint @spsnyc July 2012
PowerShell and SharePoint @spsnyc July 2012PowerShell and SharePoint @spsnyc July 2012
PowerShell and SharePoint @spsnyc July 2012
 
PowerShell and SharePoint
PowerShell and SharePointPowerShell and SharePoint
PowerShell and SharePoint
 
Welcome to windows 8
Welcome to windows 8Welcome to windows 8
Welcome to windows 8
 
Automating PowerShell with SharePoint
Automating PowerShell with SharePointAutomating PowerShell with SharePoint
Automating PowerShell with SharePoint
 
F# And Silverlight
F# And SilverlightF# And Silverlight
F# And Silverlight
 
SharePoint Saturday Boston 2010
SharePoint Saturday Boston 2010SharePoint Saturday Boston 2010
SharePoint Saturday Boston 2010
 
Automating SQL Server Database Creation for SharePoint
Automating SQL Server Database Creation for SharePointAutomating SQL Server Database Creation for SharePoint
Automating SQL Server Database Creation for SharePoint
 
Architecting Solutions for the Manycore Future
Architecting Solutions for the Manycore FutureArchitecting Solutions for the Manycore Future
Architecting Solutions for the Manycore Future
 

Recently uploaded

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 

Recently uploaded (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 

Introduction to F#

  • 1. Introduction to F# Talbott Crowell
  • 2. Functional programming has been around a long time Not new Long history Functional programming is safe A concern as we head toward manycore and cloud computing Functional programming is on the rise Why another language?
  • 3. 1930’s: lambda calculus (roots) 1956: IPL (Information Processing Language) “the first functional language 1958: LISP “a functional flavored language” 1962: APL (A Programming Language) 1973: ML (Meta Language) 1983: SML (Standard ML) 1987: Caml (Categorical Abstract Machine Language ) and Haskell 1996: OCaml (Objective Caml) Functional programming has been around a long time
  • 4. Side effects Modifies some state Has observable interaction with calling functions Has observable interaction with the outside world Example: a function or method with no return value Most functional languages encourage programmers to avoid side effects Haskell (a “pure” functional language) restricts side effects with a static type system Functional programming is safe
  • 5. Language Evolution (Simon Payton-Jones) C#, VB, Java, C are imperative programming languages. Very useful but can change the state of the world at anytime creating side effects. Nirvana! Useful and Safe F# Haskell is Very Safe, but not very useful. Used heavily in research and academia, but rarely in business . http://channel9.msdn.com/posts/Charles/Simon-Peyton-Jones-Towards-a-Programming-Language-Nirvana/
  • 6. Long relegated to academia Industrial and commercial applications Scheme, Erlang, OCaml, Haskell F# at Grange Insurance, Ohio: http://bit.ly/GrangeFSharp F# used by XBox Live Trueskill: http://bit.ly/trueskill DSL’s Mathematica XSLT Excel can be viewed as a functional programming language Functional programming on the rise
  • 7. Functional language developed by Microsoft Research By Don Syme and his team, who productized Generics Based on OCaml (influenced by C# and Haskell) History 2002: F# language design started 2005 January: F# 1.0.1 releases to public Not a product. Integration with VS2003 Works in .NET 1.0 through .NET 2.0 beta, Mono 2005 November: F# 1.1.5 with VS 2005 RTM support 2009 October: VS2010 Beta 2, CTP for VS2008 & Non-Windows users 2010: F# is “productized” and baked into VS 2010 What is F#
  • 8. Interactive Scripting Good for prototyping Succinct = Less code Type Inference Strongly typed, strict Automatic generalization (generics for free) Few type annotations 1st class functions (currying, lazy evaluations) Pattern matching Key Characteristics of F#
  • 9. Multi-Paradigm Functional Programming Imperative Programming Object Oriented Programming Language Oriented Programming Task Oriented Programming F# is not just Functional
  • 10. Dynamically compiles code on the fly Example: Demo F# Interactive open System.Drawing;; open System.Windows.Forms;; let myform = new Form(Text="Hello Everybody", Visible=true);; myform.BackColor <- Color.Blue;; let mybutton = new Button(Text = "Click Me");; myform.Controls.Add(mybutton);; mybutton.BackColor <- Color.White;; mybutton.Click.Add(fun _ -> MessageBox.Show("Clicked") |> ignore);;
  • 11. Type inference Expressions F# Basics let x = 5 let y = 5.0 let files = Directory.GetFiles(@"C:magesriginal") let x = 5 * 5 let y = 5.0 / 3.0 let width = image.Width / 8
  • 12. Function Anonymous functions F# Functions let sqr x = x * x sqr 5 (fun x -> x * x) 5
  • 13. The |> Combinator “Pipe Forward” Example F# Combinators x |> f is the same as f x let sqr x = x * x sqr x 5 |> sqr
  • 14. Partial functions Don’t have to pass all parameters at once Curried Functions letDivideBy y x = x / y let result = DivideBy 2.0 3.0 valDivideBy : float -> float -> float val result : float = 1.5 let HalfOf = DivideBy 2.0 let result2 = HalfOf 3.0 valHalfOf : (float -> float) val result2 : float = 1.5
  • 15. Pass curried function to another function Power of Curried Functions letDivideBy y x = x / y letnumbers = [1.0; 2.0; 3.0] let result3 = List.map (DivideBy 2.0) numbers valDivideBy : float -> float -> float val numbers : float list = [1.0; 2.0; 3.0] val result3 : float list = [0.5; 1.0; 1.5]
  • 16. Compare with VB Function DivideBy(ByVal y As Single, _ ByVal x As Single) As Single Return x / y End Function Sub Main() Dim numbers = New Single() {1.0, 2.0, 3.0} Dim result3 As New List(Of Single) For Each number In numbers result3.Add(DivideBy(2.0, number)) Next End Sub
  • 17. used to group values together without creating a class or struct Tuple Tuple as a function parameter #light open System let swap (x, y) = (y, x) let rotate (x, y, z) = (y, z, x) let circleDetails radius = let area = Math.PI * (radius * radius) let circumference = 2.0 * Math.PI * radius (area, circumference) let result1 = swap(3, 2) let result2 = rotate(5, 6, 7) let details = circleDetails 4.0 let (a, c) = details Tuple as a return value Tuple as left side of an assignment
  • 18. Similar to Object Oriented inheritance Useful in pattern matching Discriminated Unions type Suit = | Spades | Hearts | Clubs | Diamonds let suits = [Spades; Hearts; Clubs; Diamonds]
  • 19. Discriminated Unions cont. type Suit = | Spades | Hearts | Clubs | Diamonds type Card = | Ace of Suit | RankCard of int * Suit | Jack of Suit | Queen of Suit | King of Suit | Joker let card1 = Ace(Diamonds) let card2 = RankCard(3, Spades) let card3 = Joker
  • 20. Used often in F# programs Syntax (source: http://en.wikibooks.org/wiki/F_Sharp_Programming/Pattern_Matching_Basics ) Pattern Matching
  • 21. Mouse event handler in DirectX Demo Checking mouse button and shift key modifiers Pattern Matching Example match b.Button, Form.ModifierKeys with | MouseButtons.Left, Keys.Shift -> view.AdjustZoom(dx,dy) | MouseButtons.Left, _ -> view.AdjustYawPitchRoll(dx,dy) | _ -> view.AdjustFocus(dx,dy)
  • 22. FSharp Samples v0.2 The DirectX Demo More F# Interactive http://code.msdn.microsoft.com/fsharpsamples
  • 23. The Power Wall Manycore FUD Free lunch is over Parallel programming in .NET Where F# fits into this future F# and Impact on the Future
  • 24. The Power Wall: CPU Clock Speed From Katherine Yelick’s “Multicore: Fallout of a Hardware Revolution”
  • 25. Fear, Uncertainty, and Doubt Talbott’s FUD slide: why you should be concerned Moore’s Law Predicted in 1965 that transistor density in semiconductor chips would double every 18 months Clock speed on chips plateaued in 2005 at 3 GHz due to the “Power Wall” Intel and AMD have now begun doubling cores every 18 months to keep up with Moore’s Law AMD Opteron Istanbul and Intel Xeon have 6 cores per socket used in Servers Dell Studio XPS (2009 price of $750) comes with Intel i7 with 4 cores with 2 way hyper-threading (8 virtual cores) Intel Larrabee on the horizon with 32 cores with 4 way hyper-threading (128 virtual cores) FUD
  • 26. Source: Wikipedia entry for Larrabee (GPU) Intel Larrabee
  • 27. Free lunch is over Programs are not doubling in speed every couple of years for free anymore We need to start writing code to take advantage of many cores Currently painful and problematic to take advantage of many cores because of shared memory, locking, and other imperative programming techniques Problem
  • 28. Libraries and Framework Improvements TPL (Included in .NET 4.0) PLINQ Programming Approaches Functional programming Immutable data No side effects Task oriented programming Solution
  • 29. Parallel Programming in the .NET Framework 4 Beta 2
  • 30. “F# is, technically speaking, neutral with respect to concurrency - it allows the programmer to exploit the many different techniques for concurrency and distribution supported by the .NET platform” F# FAQ: http://bit.ly/FSharpFAQ Functional programming is a primary technique for minimizing/isolating mutable state Asynchronous workflows make writing parallel programs in a “natural and compositional style” F# and Multi-Core Programming
  • 31. Problem Resize a ton of images Demo of Image Processor let files = Directory.GetFiles(@"C:magesriginal") for file in files do use image = Image.FromFile(file) use smallImage = ResizeImage(image) let destFileName = DestFileName("s1", file) smallImage.Save(destFileName)
  • 32. Asynchronous Workflows let FetchAsync(file:string) = async { use stream = File.OpenRead(file) let! bytes = stream.AsyncRead(intstream.Length) use memstream = new MemoryStream(bytes.Length) memstream.Write(bytes, 0, bytes.Length) use image = Image.FromStream(memstream) use smallImage = ResizeImage(image) let destFileName = DestFileName("s2", file) smallImage.Save(destFileName) } let tasks = [for file in files -> FetchAsync(file)] let parallelTasks = Async.Parallel tasks Async.RunSynchronouslyparallelTasks
  • 33. Almost 2x on 2 cores About 3.5x faster on 8 cores Demo Asynchronous Workflow
  • 34. Thank you for attending Intro to F#Questions? F# User Group http://fsug.org Twitter: @FSUG Talbott Crowell, ThirdM.com http://talbottc.spaces.live.com Twitter: @Talbott