Upgrade to Pro — share decks privately, control downloads, hide ads and more …

F# - An Introduction

F# - An Introduction

An introduction to F#

Avatar for Patrick McDonald

Patrick McDonald

August 12, 2014

Other Decks in Technology

Transcript

  1. HISTORY OF F# Originated from ML Influenced by OCaml, C#,

    Haskell, etc. Came out of Microsoft Research Designed by Don Syme ( ) @dsyme 1.0 in 2005, 2.0 shipped with Visual Studio 2010 Latest version, 3.1.1, was an out of band release Maintained by the F# Software Foundation (FSSF)
  2. F# IS Statically and strongly typed Functional first Uses type

    inference Open source (MIT License) Targets CLR and Mono Runs on Windows, OS X, Linux, iOS, Android, Windows Phone
  3. LET Set a variable l e t x = 4

    2 / / v a l x : i n t = 4 2 l e t s = " H e l l o w o r l d " / / v a l s : s t r i n g = " H e l l o w o r l d " l e t p i = 3 . 1 4 1 5 9 / / v a l p i : f l o a t = 3 . 1 4 1 5 9 Declare a function l e t s q u a r e x = x * x / / v a l s q u a r e : x : i n t - > i n t l e t a d d x y = x + y / / v a l a d d : x : i n t - > y : i n t - > i n t l e t m u l t i p l y = f u n x y - > x * y / / v a l m u l t i p l y : x : i n t - > y : i n t - > i n t
  4. LET Recursive functions l e t r e c f

    a c t o r i a l n = i f n = 0 t h e n 1 e l s e n * f a c t o r i a l ( n - 1 ) l e t r e c g c d a b = i f a = 0 t h e n b e l s e i f a < b t h e n g c d ( b % a ) a e l s e g c d ( a % b ) b
  5. FUNCTION CURRYING Every function takes one argument and returns one

    value l e t f x y = . . . / / v a l f : x : i n t - > y : i n t - > i n t takes a parameter x that returns a functon that takes a parameter y l e t f 2 = f 2 / / v a l f 2 : ( i n t - > i n t )
  6. FUNCTION CURRYING l e t a d d 2 =

    a d d 2 / / v a l a d d 2 : ( i n t - > i n t ) l e t a d d 2 = ( + ) 2 / / v a l a d d 2 : ( i n t - > i n t )
  7. TYPES TUPLES A tuple is a grouping of two or

    more unnamed but ordered values, possibly of different types. l e t o r i g i n = 0 . 0 , 0 . 0 / / v a l o r i g i n : f l o a t * f l o a t = ( 0 . 0 , 0 . 0 ) l e t f o r t y T w o = S y s t e m . I n t 3 2 . T r y P a r s e ( " 4 2 " ) ; ; / / v a l f o r t y T w o : b o o l * i n t = ( t r u e , 4 2 ) l e t a = f s t ( 1 . 0 , 2 . 0 ) l e t b = s n d ( 1 . 0 , 2 . 0 ) l e t x , y , z = ( 1 . 0 , 2 . 0 , 3 . 0 )
  8. RECORDS A record can be thought of as a simple

    immutable class, or a tuple with named fields t y p e P e r s o n = { F i r s t N a m e : s t r i n g ; L a s t N a m e : s t r i n g ; A g e : i n t } l e t j o h n = { F i r s t N a m e = " J o h n " ; L a s t N a m e = " D o e " ; A g e = 5 4 } l e t j o h n s T w i n J o e = { j o h n w i t h F i r s t N a m e = " J o e " }
  9. TYPES COLLECTIONS List l e t e m p t

    y = [ ] l e t p e o p l e 2 = " A l i c e " : : " B o b " : : " C a r o l " : : [ ] l e t p e o p l e = [ " A l i c e " ; " B o b " ; " C a r o l " ] l e t l 1 = [ 1 . . 1 0 ] l e t l = [ f o r x i n 1 . . 1 0 - > x * x ] l e t l = L i s t . m a p ( f u n x - > x * x ) [ 1 . . 1 0 ]
  10. TYPES COLLECTIONS Seq l e t s = s e

    q { 1 . . 2 0 } l e t d o m i n o e s = s e q { f o r x i n 0 . . 6 d o f o r y i n 0 . . x d o y i e l d x , y } f o r d i n d o m i n o e s d o p r i n t f n " % A " d
  11. TYPES COLLECTIONS Array l e t s q u a

    r e s = [ | f o r x i n 0 . . 9 - > x * x | ] l e t s q u a r e s 2 = A r r a y . i n i t 1 0 ( f u n x - > x * x ) Set l e t f i r s t T e n P r i m e s = s e t [ 2 , 3 , 5 , 7 , 1 1 , 1 3 , 1 7 , 1 9 ] l e t s u i t s = s e t [ " s p a d e s " , " c l u b s " , " h e a r t s " , " d i a m o n d s " ] Map l e t t w i t t e r s = M a p . o f L i s t [ " P a t r i c k " , " @ p a d d y m c d o n a l d " ; " A n d r e a " , " @ s i l v e r s p o o n " ; " R i c h a r d " , " @ r i c h a r d a d a l t o n " ]
  12. PATTERN MATCHING Match expressions l e t r e c

    f a c t o r i a l n = m a t c h n w i t h | 0 - > 1 | _ - > n * f a c t o r i a l ( n - 1 ) l e t r e c s u m l i s t = m a t c h l i s t w i t h | [ ] - > 0 | h e a d : : t a i l - > h e a d + s u m t a i l l e t r e c l e n g t h = f u n c t i o n | [ ] - > 0 | _ : : t a i l - > 1 + l e n g t h t a i l l e t r e c g c d a b = m a t c h a , b w i t h | 0 , _ - > b | _ w h e n a < b - > g c d ( b % a ) a | _ - > g c d ( a % b ) b
  13. DISCRIMINATED UNIONS t y p e C o l o

    u r = | R e d | G r e e n | B l u e t y p e R e s u l t = | S u c c e s s | E r r o r o f s t r i n g t y p e B i n a r y T r e e < ' T > = | L e a f | N o d e o f B i n a r y T r e e < ' T > * ' T * B i n a r y T r e e < ' T > l e t r e c l e n g t h = f u n c t i o n | L e a f - > 0 | N o d e ( l e f t , _ , r i g h t ) - > 1 + ( l e n g t h l e f t ) + ( l e n g t h r i g h t )
  14. DISCRIMINATED UNIONS t y p e V e c t

    o r = V e c t o r o f f l o a t * f l o a t l e t m a g n i t u d e ( V e c t o r ( x , y ) ) = s q r t ( x * x + y * y ) m a g n i t u d e ( V e c t o r ( 3 . 0 , 4 . 0 ) ) / / t y p e V e c t o r = | V e c t o r o f f l o a t * f l o a t / / v a l m a g n i t u d e : V e c t o r - > f l o a t / / v a l i t : f l o a t = 5 . 0
  15. PIPELINING = Function Currying + Pipeline operator |> l e

    t ( | > ) x f = f x s q r t 2 5 . 0 / / i s t h e s a m e a s 2 5 . 0 | > s q r t
  16. PIPELINING L i s t . m a p (

    f u n x - > x * x ) x s / / b e c o m e s x s | > L i s t . m a p ( f u n x - > x * x ) L i s t . f i l t e r ( f u n x - > x > 0 ) x s / / b e c o m e s x s | > L i s t . f i l t e r ( f u n x - > x > 0 ) Putting it together x s | > L i s t . f i l t e r ( f u n x - > x > 0 ) | > L i s t . m a p ( f u n x - > x * x ) | > L i s t . s u m | > s q r t