concepts of programming languages
play

Concepts of programming languages F# Tim Zoet, Zino Onomiwo, - PowerPoint PPT Presentation

[Faculty of Science Information and Computing Sciences] 1 Concepts of programming languages F# Tim Zoet, Zino Onomiwo, Martijn Boom, Rik van Toor [Faculty of Science Information and Computing Sciences] 2 Background Don Syme


  1. [Faculty of Science Information and Computing Sciences] 1 Concepts of programming languages F# Tim Zoet, Zino Onomiwo, Martijn Boom, Rik van Toor

  2. [Faculty of Science Information and Computing Sciences] 2 Background ▶ Don Syme ▶ Microsoft Research ▶ Recent development by the F# Software Foundation ▶ Microsoft develops the Visual F# tools

  3. [Faculty of Science Information and Computing Sciences] 3 What is F# ▶ A fjrst-class member of the .NET Framework. ▶ Strongly related to ML and OCaml. ▶ A hybrid language ▶ Integration within Visual Studio

  4. [Faculty of Science Information and Computing Sciences] 4 Programming in F# Hello World let a_string = "World" printfn "Hello %s" a_string

  5. [Faculty of Science Information and Computing Sciences] 5 Types and variables type name = string let author: name = "infinite monkey" What features regarding types are supported in F#?

  6. [Faculty of Science Information and Computing Sciences] 6 Tuples, structures and more. Tuples: type tuple = int * string let tuple = (1, “hi!”) // Comma separated Records type Person = {Name:string; Age:int; Status:string} // named fields, semicolon separated let celebrity = {Name=”Gordon”, Age=49, Status=”single”}

  7. [Faculty of Science Information and Computing Sciences] 7 Tuples, structures and more. Union type Person = | DogPerson of string let martijn = DogPerson “Martijn” | CatPerson of string

  8. [Faculty of Science Information and Computing Sciences] 8 Tuples, structures and more. Recursive structures and unions: type 'a union = A 'a | B 'a type 'a tree = |EmptyTree | Node of 'a * 'a tree * 'a tree

  9. [Faculty of Science Information and Computing Sciences] 9 Tuples, structures and more. Structures: type Cat = struct new (n, f, e) = {Name = n; Fur = f; Env = e} end let cat: Cat = new Cat("Ottoman", RED, BLOCKCHAIN) val Name: name; val Fur: color val Env: environment

  10. [Faculty of Science Information and Computing Sciences] 10 Lists and arrays Arrays Fixed size and mutable let arr = [|1; 2; 3; 4; 5|] let arr2 = [|1.0; 2; 3|] // Gives an error let arr3 = [|for i in 1 .. 100 -> i * (i+1)|] printfn “%d” arr.[0] // prints 2 printfn “%A” array // print [1; 2; 3; 4; 5]

  11. [Faculty of Science Information and Computing Sciences] 11 Lists and arrays Lists singly linked list and immutable let lissy = [1; 2; 3] let lissy2= 1::[2 … 8999] @ [1] let lissy3= [for i in 1 .. 100 -> i * (i+1)] let rec sum list = match list with | head :: tail -> head + sum tail | [] -> 0

  12. [Faculty of Science Information and Computing Sciences] 12 Functions let add x y = x + y let sumOfSquares n = [1..n] |> List.map square |> List.sum let adderGenerator = fun x -> (fun y -> x + y) sumOfSquares 100

  13. [Faculty of Science Information and Computing Sciences] 13 Classes type Exam(student, subject, grade) = member this.Student = student member this.Subject = subject member this.Grade = grade// public member let stressLevel = 100 //private member new Exam(“Socrates”, “Philosophy for dummies”, 10)

  14. [Faculty of Science Information and Computing Sciences] 14 Pattern matching match x with | case1 -> … | case2 -> … | _ -> …

  15. [Faculty of Science Information and Computing Sciences] 15 let patternMatcher input = match input with | “a” -> printfn “Case 1” | “b” -> printfn “Case 2” | _ -> printfn “Default” patternMatcher “a” patternMatcher “b” patternMatcher “c”

  16. [Faculty of Science Information and Computing Sciences] 16 let listSummary list = match list with | [] -> printfn "empty array" | [x] -> printfn "one element: %s" x | [x;y] -> printfn "two elements” | _ -> printfn "multiple elements"

  17. [Faculty of Science Information and Computing Sciences] 17 Async Ruin code in parallel let motivationLevel (lvl, target) = async { do ! Async.Sleep percentage } [(101, “Tim”), (65, “Rik”)] |> List.map motivationLevel |> Async.Parallel |> Async.RunSynchronously printfn "Motivating %s till %i \%" target lvl printfn "%s is %i \% motivated" target lvl

  18. [Faculty of Science Information and Computing Sciences] 18 [<Measure>] type cm [<Measure>] type meter let cmPerMeter = 10<cm/meter> let distanceToHomeCm = 100<cm> let distanceToHomeM = distanceToHomeCm * cmPerMeter let otherConvert v: float<_> = match v with | :? float<cm> -> v * cmPerMeter | :? float<meter> -> v<meter>

  19. [Faculty of Science Information and Computing Sciences] 19 Type inference with generalize to generic types ▶ Using amas-Milner’s Algorithm ▶ Look at the literals ▶ Look at the functions and other values something interacts ▶ Look at any explicit type constraints ▶ If there are no constraints anywhere, automatically

  20. [Faculty of Science Information and Computing Sciences] 20 Relation to other languages

  21. [Faculty of Science Information and Computing Sciences] 21 Compared to C# public static IEnumerable<T> QuickSort<T>( this IEnumerable<T> values) where T: IComparable { if (values == null || !values.Any()) return new List<T>(); var pivot = values.First(); var rest = values.Skip(1); var largerElements = rest.Where(i => i.CompareTo(pivot) >= 0).QuickSort(); return smallerElements.Concat( new List<T> { pivot }).Concat(largerElements); } ▶ Concise var smallerElements = rest.Where(i => i.CompareTo(pivot) < 0).QuickSort();

  22. [Faculty of Science Information and Computing Sciences] 22 let rec quicksort = function | [] -> [] | pivot::rest -> List.partition ((>=) pivot) rest List.concat [quicksort smaller; [pivot]; quicksort larger] let smaller,larger = ▶ Little coding noise ▶ Pattern matching ▶ Functional List module

  23. [Faculty of Science Information and Computing Sciences] 23 Type inference let AStringFunction = "It is implied I return a string" let AStringFunction :string = "It is made explicit I return a string" string AnotherStringFunction(){ return "I'm a string function as well"; }

  24. [Faculty of Science StateAbbrev of string type UKAddress = {Street:StreetAddress; PostCode of string type UKPostCode = Region:ZipAndState} type USAddress = {Street:StreetAddress; {State:StateAbbrev; Zip:ZipCode } type ZipAndState = type StateAbbrev = Information and Computing ZipCode of string type ZipCode = Line3:string } type StreetAddress = {Line1:string; Line2:string; Domain-driven design 24 Sciences] Region:UKPostCode}

  25. [Faculty of Science Information and Computing Sciences] 25 Domain-driven design type InternationalAddress = { Street:StreetAddress; Region:string; CountryName:string} type Address = USAddress | UKAddress | InternationalAddress

  26. [Faculty of Science Information and Computing Sciences] 26 Interoperability with C# namespace interoperability.fsharp type Beverage (brand:string, volume:int) = member this.Brand = brand member this.Volume = volume

  27. [Faculty of Science static void Main(string[] args) } } Console.WriteLine(coke.name + “ “ + coke.Volume); Beverage coke = new Beverage("Coca Cola", 330); //from record to class { { Information and Computing class Program { namespace interoperability.csharp using interoperability.fsharp; 27 Sciences] }

  28. [Faculty of Science { } } } Console.WriteLine(coke.name + “ “ + coke.Volume); Beverage coke = new Beverage("Coca Cola", 330); //from record to class static void Main(string[] args) Information and Computing { class Program { namespace interoperability.csharp using interoperability.fsharp; 28 Sciences] ▶ output = “Coca Cola 330”

  29. [Faculty of Science Information and Computing Sciences] 29 namespace interoperability.fsharp type Beverage (brand:string, volume:int) = member this.Brand = brand member this.Volume = volume module BeverageTasks = let drink (x:Beverage) = Beverage(x.Brand, 0)

  30. [Faculty of Science { } } Console.WriteLine(coke.name + “ “ + coke.Volume); coke = BeverageTasks.drink(coke) //from module to static class Beverage coke = new Beverage("Coca Cola", 330); //from record to class static void Main(string[] args) Information and Computing { class Program { namespace interoperability.csharp using interoperability.fsharp; 30 Sciences] }

  31. [Faculty of Science //from record to class } } } Console.WriteLine(coke.name + “ “ + coke.Volume); coke = BeverageTasks.drink(coke); //from module to static class Beverage coke = new Beverage("Coca Cola", 330); { Information and Computing static void Main(string[] args) { class Program { namespace interoperability.csharp using interoperability.fsharp; 31 Sciences] output = “Coca Cola 0”

  32. [Faculty of Science Information and Computing Sciences] 32 namespace interoperability.fsharp type Beverage (brand:string, volume:int) = member this.Brand = brand member this.Volume = volume module BeverageTasks = let drink (x:Beverage) = Beverage(x.Brand, 0) let mutable message = "Wasn't I supposed to be functional?"

Recommend


More recommend