the f# programming languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · contents introduce...
TRANSCRIPT
![Page 1: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/1.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
The F# Programming Language
Mónika MészárosE-mail: [email protected]
Department of Programming Languages and CompilersELTE
May 22, 2009
Mónika Mészáros The F# Programming Language
![Page 2: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/2.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Outline of the presentation
1 Introduction to F#
2 Characteristics of F#
3 Standard Developer Tools
4 Language Basics
5 Lists
6 Higher-Order Functions
7 Tuples
8 Records
Mónika Mészáros The F# Programming Language
![Page 3: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/3.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Outline of the presentation
1 Introduction to F#
2 Characteristics of F#
3 Standard Developer Tools
4 Language Basics
5 Lists
6 Higher-Order Functions
7 Tuples
8 Records
Mónika Mészáros The F# Programming Language
![Page 4: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/4.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Outline of the presentation
1 Introduction to F#
2 Characteristics of F#
3 Standard Developer Tools
4 Language Basics
5 Lists
6 Higher-Order Functions
7 Tuples
8 Records
Mónika Mészáros The F# Programming Language
![Page 5: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/5.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Outline of the presentation
1 Introduction to F#
2 Characteristics of F#
3 Standard Developer Tools
4 Language Basics
5 Lists
6 Higher-Order Functions
7 Tuples
8 Records
Mónika Mészáros The F# Programming Language
![Page 6: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/6.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Outline of the presentation
1 Introduction to F#
2 Characteristics of F#
3 Standard Developer Tools
4 Language Basics
5 Lists
6 Higher-Order Functions
7 Tuples
8 Records
Mónika Mészáros The F# Programming Language
![Page 7: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/7.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Outline of the presentation
1 Introduction to F#
2 Characteristics of F#
3 Standard Developer Tools
4 Language Basics
5 Lists
6 Higher-Order Functions
7 Tuples
8 Records
Mónika Mészáros The F# Programming Language
![Page 8: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/8.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Outline of the presentation
1 Introduction to F#
2 Characteristics of F#
3 Standard Developer Tools
4 Language Basics
5 Lists
6 Higher-Order Functions
7 Tuples
8 Records
Mónika Mészáros The F# Programming Language
![Page 9: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/9.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Outline of the presentation
1 Introduction to F#
2 Characteristics of F#
3 Standard Developer Tools
4 Language Basics
5 Lists
6 Higher-Order Functions
7 Tuples
8 Records
Mónika Mészáros The F# Programming Language
![Page 10: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/10.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Introduction to F#
F# is aFunctionalImperative andObject-oriented
programming language.
F# is "OCaml for .NET"
Mónika Mészáros The F# Programming Language
![Page 11: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/11.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Introduction to F#
F# is aFunctionalImperative andObject-oriented
programming language.
F# is "OCaml for .NET"
Mónika Mészáros The F# Programming Language
![Page 12: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/12.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Notable features
Strongly typed
Type inference
Performance profile like that of C#
Easy access to entire range of powerful .NET libraries
Speed of native code execution on the concurrent,portable, and distributed .NET Framework
Option of a top-rate Visual Studio integration
Cross-compiling core shared with the OCaml language
Mónika Mészáros The F# Programming Language
![Page 13: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/13.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Notable features
Strongly typed
Type inference
Performance profile like that of C#
Easy access to entire range of powerful .NET libraries
Speed of native code execution on the concurrent,portable, and distributed .NET Framework
Option of a top-rate Visual Studio integration
Cross-compiling core shared with the OCaml language
Mónika Mészáros The F# Programming Language
![Page 14: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/14.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Notable features
Strongly typed
Type inference
Performance profile like that of C#
Easy access to entire range of powerful .NET libraries
Speed of native code execution on the concurrent,portable, and distributed .NET Framework
Option of a top-rate Visual Studio integration
Cross-compiling core shared with the OCaml language
Mónika Mészáros The F# Programming Language
![Page 15: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/15.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Notable features
Strongly typed
Type inference
Performance profile like that of C#
Easy access to entire range of powerful .NET libraries
Speed of native code execution on the concurrent,portable, and distributed .NET Framework
Option of a top-rate Visual Studio integration
Cross-compiling core shared with the OCaml language
Mónika Mészáros The F# Programming Language
![Page 16: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/16.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Notable features
Strongly typed
Type inference
Performance profile like that of C#
Easy access to entire range of powerful .NET libraries
Speed of native code execution on the concurrent,portable, and distributed .NET Framework
Option of a top-rate Visual Studio integration
Cross-compiling core shared with the OCaml language
Mónika Mészáros The F# Programming Language
![Page 17: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/17.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Notable features
Strongly typed
Type inference
Performance profile like that of C#
Easy access to entire range of powerful .NET libraries
Speed of native code execution on the concurrent,portable, and distributed .NET Framework
Option of a top-rate Visual Studio integration
Cross-compiling core shared with the OCaml language
Mónika Mészáros The F# Programming Language
![Page 18: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/18.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Notable features
Strongly typed
Type inference
Performance profile like that of C#
Easy access to entire range of powerful .NET libraries
Speed of native code execution on the concurrent,portable, and distributed .NET Framework
Option of a top-rate Visual Studio integration
Cross-compiling core shared with the OCaml language
Mónika Mészáros The F# Programming Language
![Page 19: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/19.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Other features
An F# program consists of type, class and functiondefinitions and expressions
Computation means evaluation of all the expressions oneby one
F# uses strict evaluation
F# is not pure (programs may contain side-effects)
Off-side rule only in "lightweight" syntax, which can beturned on by #light ("hash-light") compiler directive (it isrecommended to keep #light on)
Mónika Mészáros The F# Programming Language
![Page 20: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/20.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Other features
An F# program consists of type, class and functiondefinitions and expressions
Computation means evaluation of all the expressions oneby one
F# uses strict evaluation
F# is not pure (programs may contain side-effects)
Off-side rule only in "lightweight" syntax, which can beturned on by #light ("hash-light") compiler directive (it isrecommended to keep #light on)
Mónika Mészáros The F# Programming Language
![Page 21: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/21.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Other features
An F# program consists of type, class and functiondefinitions and expressions
Computation means evaluation of all the expressions oneby one
F# uses strict evaluation
F# is not pure (programs may contain side-effects)
Off-side rule only in "lightweight" syntax, which can beturned on by #light ("hash-light") compiler directive (it isrecommended to keep #light on)
Mónika Mészáros The F# Programming Language
![Page 22: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/22.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Other features
An F# program consists of type, class and functiondefinitions and expressions
Computation means evaluation of all the expressions oneby one
F# uses strict evaluation
F# is not pure (programs may contain side-effects)
Off-side rule only in "lightweight" syntax, which can beturned on by #light ("hash-light") compiler directive (it isrecommended to keep #light on)
Mónika Mészáros The F# Programming Language
![Page 23: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/23.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Other features
An F# program consists of type, class and functiondefinitions and expressions
Computation means evaluation of all the expressions oneby one
F# uses strict evaluation
F# is not pure (programs may contain side-effects)
Off-side rule only in "lightweight" syntax, which can beturned on by #light ("hash-light") compiler directive (it isrecommended to keep #light on)
Mónika Mészáros The F# Programming Language
![Page 24: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/24.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Standard Developer Tools
Basically, standard developer environments for F# are asfollows:
F# Interactive
Microsoft Visual Studio integration via an Add-In
Mónika Mészáros The F# Programming Language
![Page 25: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/25.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Standard Developer Tools
Basically, standard developer environments for F# are asfollows:
F# Interactive
Microsoft Visual Studio integration via an Add-In
Mónika Mészáros The F# Programming Language
![Page 26: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/26.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
F# Interactive: Read-Eval-Print
In spirit of LISP or Haskell functional programming languages,F# also offers an opportunity for interactive softwaredevelopment.This is called F# Interactive or FSI for short.
Console application
Every feature is available
Ideal for brainstorming
Structure and behavior of programs can be analyzed
Expressions must be terminated with ";;"
Runs over Mono
Mónika Mészáros The F# Programming Language
![Page 27: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/27.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
F# Interactive: Read-Eval-Print
In spirit of LISP or Haskell functional programming languages,F# also offers an opportunity for interactive softwaredevelopment.This is called F# Interactive or FSI for short.
Console application
Every feature is available
Ideal for brainstorming
Structure and behavior of programs can be analyzed
Expressions must be terminated with ";;"
Runs over Mono
Mónika Mészáros The F# Programming Language
![Page 28: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/28.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
F# Interactive: Read-Eval-Print
In spirit of LISP or Haskell functional programming languages,F# also offers an opportunity for interactive softwaredevelopment.This is called F# Interactive or FSI for short.
Console application
Every feature is available
Ideal for brainstorming
Structure and behavior of programs can be analyzed
Expressions must be terminated with ";;"
Runs over Mono
Mónika Mészáros The F# Programming Language
![Page 29: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/29.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
F# Interactive: Read-Eval-Print
In spirit of LISP or Haskell functional programming languages,F# also offers an opportunity for interactive softwaredevelopment.This is called F# Interactive or FSI for short.
Console application
Every feature is available
Ideal for brainstorming
Structure and behavior of programs can be analyzed
Expressions must be terminated with ";;"
Runs over Mono
Mónika Mészáros The F# Programming Language
![Page 30: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/30.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
F# Interactive: Read-Eval-Print
In spirit of LISP or Haskell functional programming languages,F# also offers an opportunity for interactive softwaredevelopment.This is called F# Interactive or FSI for short.
Console application
Every feature is available
Ideal for brainstorming
Structure and behavior of programs can be analyzed
Expressions must be terminated with ";;"
Runs over Mono
Mónika Mészáros The F# Programming Language
![Page 31: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/31.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
F# Interactive: Read-Eval-Print
In spirit of LISP or Haskell functional programming languages,F# also offers an opportunity for interactive softwaredevelopment.This is called F# Interactive or FSI for short.
Console application
Every feature is available
Ideal for brainstorming
Structure and behavior of programs can be analyzed
Expressions must be terminated with ";;"
Runs over Mono
Mónika Mészáros The F# Programming Language
![Page 32: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/32.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
F# Interactive: Read-Eval-Print
In spirit of LISP or Haskell functional programming languages,F# also offers an opportunity for interactive softwaredevelopment.This is called F# Interactive or FSI for short.
Console application
Every feature is available
Ideal for brainstorming
Structure and behavior of programs can be analyzed
Expressions must be terminated with ";;"
Runs over Mono
Mónika Mészáros The F# Programming Language
![Page 33: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/33.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Some Easy Expressions
Launch the F# Interactive
Try the following expressions:
> let square x = x * x;;> square 4;;> let numbers = [1 .. 10];;> let squares = List.map square numbers;;> squares;;> List.map square numbers;;
Mónika Mészáros The F# Programming Language
![Page 34: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/34.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Some Easy Expressions
Launch the F# Interactive
Try the following expressions:
> let square x = x * x;;> square 4;;> let numbers = [1 .. 10];;> let squares = List.map square numbers;;> squares;;> List.map square numbers;;
Mónika Mészáros The F# Programming Language
![Page 35: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/35.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Some Easy Expressions
Launch the F# Interactive
Try the following expressions:
> let square x = x * x;;> square 4;;> let numbers = [1 .. 10];;> let squares = List.map square numbers;;> squares;;> List.map square numbers;;
Mónika Mészáros The F# Programming Language
![Page 36: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/36.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Some Easy Expressions
Launch the F# Interactive
Try the following expressions:
> let square x = x * x;;> square 4;;> let numbers = [1 .. 10];;> let squares = List.map square numbers;;> squares;;> List.map square numbers;;
Mónika Mészáros The F# Programming Language
![Page 37: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/37.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Some Easy Expressions
Launch the F# Interactive
Try the following expressions:
> let square x = x * x;;> square 4;;> let numbers = [1 .. 10];;> let squares = List.map square numbers;;> squares;;> List.map square numbers;;
Mónika Mészáros The F# Programming Language
![Page 38: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/38.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Some Easy Expressions
Launch the F# Interactive
Try the following expressions:
> let square x = x * x;;> square 4;;> let numbers = [1 .. 10];;> let squares = List.map square numbers;;> squares;;> List.map square numbers;;
Mónika Mészáros The F# Programming Language
![Page 39: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/39.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Some Easy Expressions
Launch the F# Interactive
Try the following expressions:
> let square x = x * x;;> square 4;;> let numbers = [1 .. 10];;> let squares = List.map square numbers;;> squares;;> List.map square numbers;;
Mónika Mészáros The F# Programming Language
![Page 40: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/40.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Visual Development: Microsoft Visual F#
Recent F# distributions (1.9.6.2 CTP, September 2008) includean Add-In for the Microsoft Visual Studio 2008 DevelopmentEnvironment.It offers the following additional advantages:
Syntax highlighting
Showing derived types in tooltips
Support for debugging
Every other service of the Visual Studio Ecosystem isavailable
Microsoft Visual Studio 2010 includes complete support
Mónika Mészáros The F# Programming Language
![Page 41: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/41.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Visual Development: Microsoft Visual F#
Recent F# distributions (1.9.6.2 CTP, September 2008) includean Add-In for the Microsoft Visual Studio 2008 DevelopmentEnvironment.It offers the following additional advantages:
Syntax highlighting
Showing derived types in tooltips
Support for debugging
Every other service of the Visual Studio Ecosystem isavailable
Microsoft Visual Studio 2010 includes complete support
Mónika Mészáros The F# Programming Language
![Page 42: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/42.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Visual Development: Microsoft Visual F#
Recent F# distributions (1.9.6.2 CTP, September 2008) includean Add-In for the Microsoft Visual Studio 2008 DevelopmentEnvironment.It offers the following additional advantages:
Syntax highlighting
Showing derived types in tooltips
Support for debugging
Every other service of the Visual Studio Ecosystem isavailable
Microsoft Visual Studio 2010 includes complete support
Mónika Mészáros The F# Programming Language
![Page 43: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/43.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Visual Development: Microsoft Visual F#
Recent F# distributions (1.9.6.2 CTP, September 2008) includean Add-In for the Microsoft Visual Studio 2008 DevelopmentEnvironment.It offers the following additional advantages:
Syntax highlighting
Showing derived types in tooltips
Support for debugging
Every other service of the Visual Studio Ecosystem isavailable
Microsoft Visual Studio 2010 includes complete support
Mónika Mészáros The F# Programming Language
![Page 44: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/44.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Visual Development: Microsoft Visual F#
Recent F# distributions (1.9.6.2 CTP, September 2008) includean Add-In for the Microsoft Visual Studio 2008 DevelopmentEnvironment.It offers the following additional advantages:
Syntax highlighting
Showing derived types in tooltips
Support for debugging
Every other service of the Visual Studio Ecosystem isavailable
Microsoft Visual Studio 2010 includes complete support
Mónika Mészáros The F# Programming Language
![Page 45: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/45.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Visual Development: Microsoft Visual F#
Recent F# distributions (1.9.6.2 CTP, September 2008) includean Add-In for the Microsoft Visual Studio 2008 DevelopmentEnvironment.It offers the following additional advantages:
Syntax highlighting
Showing derived types in tooltips
Support for debugging
Every other service of the Visual Studio Ecosystem isavailable
Microsoft Visual Studio 2010 includes complete support
Mónika Mészáros The F# Programming Language
![Page 46: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/46.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Visual Development: Microsoft Visual F#
Recent F# distributions (1.9.6.2 CTP, September 2008) includean Add-In for the Microsoft Visual Studio 2008 DevelopmentEnvironment.It offers the following additional advantages:
Syntax highlighting
Showing derived types in tooltips
Support for debugging
Every other service of the Visual Studio Ecosystem isavailable
Microsoft Visual Studio 2010 includes complete support
Mónika Mészáros The F# Programming Language
![Page 47: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/47.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
A Very Simple Visual F# Project
Create a new Project, use the F# Application template.
Insert the following into the empty code editor:
#lightlet rec factorial n =match n with| 0 -> 1| n when n > 0 -> n * (factorial (n - 1))
printfn "5! = %A" (factorial 5)System.Console.ReadKey () |> ignore
Press F5 to build and run the program.
Note: recursive functions denoted by "let rec"
Mónika Mészáros The F# Programming Language
![Page 48: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/48.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
A Very Simple Visual F# Project
Create a new Project, use the F# Application template.
Insert the following into the empty code editor:
#lightlet rec factorial n =match n with| 0 -> 1| n when n > 0 -> n * (factorial (n - 1))
printfn "5! = %A" (factorial 5)System.Console.ReadKey () |> ignore
Press F5 to build and run the program.
Note: recursive functions denoted by "let rec"
Mónika Mészáros The F# Programming Language
![Page 49: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/49.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
A Very Simple Visual F# Project
Create a new Project, use the F# Application template.
Insert the following into the empty code editor:
#lightlet rec factorial n =match n with| 0 -> 1| n when n > 0 -> n * (factorial (n - 1))
printfn "5! = %A" (factorial 5)System.Console.ReadKey () |> ignore
Press F5 to build and run the program.
Note: recursive functions denoted by "let rec"
Mónika Mészáros The F# Programming Language
![Page 50: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/50.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
A Very Simple Visual F# Project
Create a new Project, use the F# Application template.
Insert the following into the empty code editor:
#lightlet rec factorial n =match n with| 0 -> 1| n when n > 0 -> n * (factorial (n - 1))
printfn "5! = %A" (factorial 5)System.Console.ReadKey () |> ignore
Press F5 to build and run the program.
Note: recursive functions denoted by "let rec"
Mónika Mészáros The F# Programming Language
![Page 51: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/51.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Type Inference
The F# compiler figures the type information out for theprogrammer.
In case of aritmetic operators, F# defaults to int, a signed32-bit integer.
> let square x = x * x;;val square : int -> int
It is possible to add "type annotations" for functionparameters and return values.
> let concat (x: string) y = x + y;;val concat : string -> string -> string
Mónika Mészáros The F# Programming Language
![Page 52: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/52.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Type Inference
The F# compiler figures the type information out for theprogrammer.
In case of aritmetic operators, F# defaults to int, a signed32-bit integer.
> let square x = x * x;;val square : int -> int
It is possible to add "type annotations" for functionparameters and return values.
> let concat (x: string) y = x + y;;val concat : string -> string -> string
Mónika Mészáros The F# Programming Language
![Page 53: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/53.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Type Inference
The F# compiler figures the type information out for theprogrammer.
In case of aritmetic operators, F# defaults to int, a signed32-bit integer.
> let square x = x * x;;val square : int -> int
It is possible to add "type annotations" for functionparameters and return values.
> let concat (x: string) y = x + y;;val concat : string -> string -> string
Mónika Mészáros The F# Programming Language
![Page 54: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/54.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Type Inference
The F# compiler figures the type information out for theprogrammer.
In case of aritmetic operators, F# defaults to int, a signed32-bit integer.
> let square x = x * x;;val square : int -> int
It is possible to add "type annotations" for functionparameters and return values.
> let concat (x: string) y = x + y;;val concat : string -> string -> string
Mónika Mészáros The F# Programming Language
![Page 55: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/55.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Pattern Matching
Wildcard "_" matches anything.
Arbitrary expression can be executed to determine if thepattern is matched.
Dynamic type tests are possible too.Syntax:
match <expression> with| <pattern1> -> <expression1>| <pattern2> -> <expression2>...
Pattern Guards (when <logical expression> -between <pattern> and "->")
Mónika Mészáros The F# Programming Language
![Page 56: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/56.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Pattern Matching
Wildcard "_" matches anything.
Arbitrary expression can be executed to determine if thepattern is matched.
Dynamic type tests are possible too.Syntax:
match <expression> with| <pattern1> -> <expression1>| <pattern2> -> <expression2>...
Pattern Guards (when <logical expression> -between <pattern> and "->")
Mónika Mészáros The F# Programming Language
![Page 57: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/57.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Pattern Matching
Wildcard "_" matches anything.
Arbitrary expression can be executed to determine if thepattern is matched.
Dynamic type tests are possible too.Syntax:
match <expression> with| <pattern1> -> <expression1>| <pattern2> -> <expression2>...
Pattern Guards (when <logical expression> -between <pattern> and "->")
Mónika Mészáros The F# Programming Language
![Page 58: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/58.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Pattern Matching
Wildcard "_" matches anything.
Arbitrary expression can be executed to determine if thepattern is matched.
Dynamic type tests are possible too.Syntax:
match <expression> with| <pattern1> -> <expression1>| <pattern2> -> <expression2>...
Pattern Guards (when <logical expression> -between <pattern> and "->")
Mónika Mészáros The F# Programming Language
![Page 59: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/59.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Pattern Matching
Wildcard "_" matches anything.
Arbitrary expression can be executed to determine if thepattern is matched.
Dynamic type tests are possible too.Syntax:
match <expression> with| <pattern1> -> <expression1>| <pattern2> -> <expression2>...
Pattern Guards (when <logical expression> -between <pattern> and "->")
Mónika Mészáros The F# Programming Language
![Page 60: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/60.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Interoperability with .NET
F# is built on top of .NET, any .NET library can be called:
System.Console.ReadKey ()
.NET namespaces can be opened and their types arebrought into scope:
open System
Console.ReadKey ()
Mónika Mészáros The F# Programming Language
![Page 61: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/61.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Interoperability with .NET
F# is built on top of .NET, any .NET library can be called:
System.Console.ReadKey ()
.NET namespaces can be opened and their types arebrought into scope:
open System
Console.ReadKey ()
Mónika Mészáros The F# Programming Language
![Page 62: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/62.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Interoperability with .NET
F# is built on top of .NET, any .NET library can be called:
System.Console.ReadKey ()
.NET namespaces can be opened and their types arebrought into scope:
open System
Console.ReadKey ()
Mónika Mészáros The F# Programming Language
![Page 63: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/63.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Exercises
1. Write a function which determines whether theargument is odd or notHint: modulo function: %, logical values: true, falseSignature: odd : int -> bool
2. Write a function which computes xy
Rules: n0= 1, nm
= n ∗ nm−1
Signature: power : int -> int -> int
Test the functions!
http://people.inf.elte.hu/bonnie/cefp/fsharp.pdf
Mónika Mészáros The F# Programming Language
![Page 64: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/64.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Exercises
1. Write a function which determines whether theargument is odd or notHint: modulo function: %, logical values: true, falseSignature: odd : int -> bool
2. Write a function which computes xy
Rules: n0= 1, nm
= n ∗ nm−1
Signature: power : int -> int -> int
Test the functions!
http://people.inf.elte.hu/bonnie/cefp/fsharp.pdf
Mónika Mészáros The F# Programming Language
![Page 65: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/65.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Exercises
1. Write a function which determines whether theargument is odd or notHint: modulo function: %, logical values: true, falseSignature: odd : int -> bool
2. Write a function which computes xy
Rules: n0= 1, nm
= n ∗ nm−1
Signature: power : int -> int -> int
Test the functions!
http://people.inf.elte.hu/bonnie/cefp/fsharp.pdf
Mónika Mészáros The F# Programming Language
![Page 66: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/66.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Exercises
1. Write a function which determines whether theargument is odd or notHint: modulo function: %, logical values: true, falseSignature: odd : int -> bool
2. Write a function which computes xy
Rules: n0= 1, nm
= n ∗ nm−1
Signature: power : int -> int -> int
Test the functions!
http://people.inf.elte.hu/bonnie/cefp/fsharp.pdf
Mónika Mészáros The F# Programming Language
![Page 67: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/67.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Solutions
let odd n =match (n%2) with| 1 -> true| _ -> false
let rec power n m =match m with| 0 -> 1| m -> n * (power n (m-1))
Mónika Mészáros The F# Programming Language
![Page 68: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/68.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Solutions
let odd n =match (n%2) with| 1 -> true| _ -> false
let rec power n m =match m with| 0 -> 1| m -> n * (power n (m-1))
Mónika Mészáros The F# Programming Language
![Page 69: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/69.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Lists
Quick syntax introduction for using lists
Define a list:
let letters = [’e’; ’i’; ’o’; ’u’]
Attach item to front (cons):
let cons = ’a’ :: letters
Concat two lists:
let more_letters = letters @ [’y’; ’z’]
Mónika Mészáros The F# Programming Language
![Page 70: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/70.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Lists
Quick syntax introduction for using lists
Define a list:
let letters = [’e’; ’i’; ’o’; ’u’]
Attach item to front (cons):
let cons = ’a’ :: letters
Concat two lists:
let more_letters = letters @ [’y’; ’z’]
Mónika Mészáros The F# Programming Language
![Page 71: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/71.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Lists
Quick syntax introduction for using lists
Define a list:
let letters = [’e’; ’i’; ’o’; ’u’]
Attach item to front (cons):
let cons = ’a’ :: letters
Concat two lists:
let more_letters = letters @ [’y’; ’z’]
Mónika Mészáros The F# Programming Language
![Page 72: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/72.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Lists
Quick syntax introduction for using lists
Define a list:
let letters = [’e’; ’i’; ’o’; ’u’]
Attach item to front (cons):
let cons = ’a’ :: letters
Concat two lists:
let more_letters = letters @ [’y’; ’z’]
Mónika Mészáros The F# Programming Language
![Page 73: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/73.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Pattern Matching for Lists
Patterns on lists:
• [] - empty list
• x::xs - list with at least 1 element
• [x] - list with only one element
• etc.
Mónika Mészáros The F# Programming Language
![Page 74: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/74.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Exercise
3. Find the maximum of the listSignature: maximum : ’a list -> ’aHint: use the max : ’a -> ’a -> ’a function!
Mónika Mészáros The F# Programming Language
![Page 75: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/75.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Solution
let rec maximum l =match l with| [x] -> x| x::xs -> max x (maximum xs)
Mónika Mészáros The F# Programming Language
![Page 76: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/76.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Higher-Order Functions
There can be also anonymous functions ("lambdaexpressions") defined, like:
(fun x -> x % 2 = 0)
Higher order functionsexampleList.map : (’a -> ’b) -> ’a list -> ’b list
Putting them together:
> List.map (fun x -> x % 2 = 0) [1 .. 5];;val it : bool list= [false; true; false; true; false]
Mónika Mészáros The F# Programming Language
![Page 77: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/77.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Higher-Order Functions
There can be also anonymous functions ("lambdaexpressions") defined, like:
(fun x -> x % 2 = 0)
Higher order functionsexampleList.map : (’a -> ’b) -> ’a list -> ’b list
Putting them together:
> List.map (fun x -> x % 2 = 0) [1 .. 5];;val it : bool list= [false; true; false; true; false]
Mónika Mészáros The F# Programming Language
![Page 78: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/78.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Higher-Order Functions
There can be also anonymous functions ("lambdaexpressions") defined, like:
(fun x -> x % 2 = 0)
Higher order functionsexampleList.map : (’a -> ’b) -> ’a list -> ’b list
Putting them together:
> List.map (fun x -> x % 2 = 0) [1 .. 5];;val it : bool list= [false; true; false; true; false]
Mónika Mészáros The F# Programming Language
![Page 79: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/79.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Higher-Order Functions
exists : (’a -> bool) -> ’a list -> bool
let rec exists p l =match l with| [] -> false| x :: xs when p x -> true| x :: xs -> exists p xs
Mónika Mészáros The F# Programming Language
![Page 80: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/80.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Exercises
4. filter: selecting elements satisfying a propertySignature:filter : (’a -> bool) -> ’a list -> ’a list
5. map: function applied elementwise (length is preserved)Signature:map : (’a -> ’b) -> ’a list -> ’b listApply a function (first parameter) to all element in the list(second parameter)
Mónika Mészáros The F# Programming Language
![Page 81: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/81.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Exercises
4. filter: selecting elements satisfying a propertySignature:filter : (’a -> bool) -> ’a list -> ’a list
5. map: function applied elementwise (length is preserved)Signature:map : (’a -> ’b) -> ’a list -> ’b listApply a function (first parameter) to all element in the list(second parameter)
Mónika Mészáros The F# Programming Language
![Page 82: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/82.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Solutions
let rec filter p l =match l with| [] -> []| x :: xs when p x -> x :: (filter p xs)| x :: xs -> filter p xs
let rec map f l =match l with| [] -> []| x :: xs -> (f x) :: (map f xs)
Mónika Mészáros The F# Programming Language
![Page 83: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/83.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Solutions
let rec filter p l =match l with| [] -> []| x :: xs when p x -> x :: (filter p xs)| x :: xs -> filter p xs
let rec map f l =match l with| [] -> []| x :: xs -> (f x) :: (map f xs)
Mónika Mészáros The F# Programming Language
![Page 84: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/84.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
A tuple is an ordered collection of values treated like anatomic unit.
Allows to keep things organized by grouping related valuestogether without introducing a new type.
Functions can even take tuples as arguments.
Sometimes tuples are used for communication with .NETlibraries.
Mónika Mészáros The F# Programming Language
![Page 85: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/85.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Using Tuples
Definition of a tuple:
> let tuple = (1, false, "text");;val tuple : int * bool * string
Function accepting a tuple:
> let printBlogInfo (title, url)= printfn "%s blog is at ’%s’"
owner title url;;val printBlogInfo : string * string -> unit
Mónika Mészáros The F# Programming Language
![Page 86: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/86.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Using Tuples
Definition of a tuple:
> let tuple = (1, false, "text");;val tuple : int * bool * string
Function accepting a tuple:
> let printBlogInfo (title, url)= printfn "%s blog is at ’%s’"
owner title url;;val printBlogInfo : string * string -> unit
Mónika Mészáros The F# Programming Language
![Page 87: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/87.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Records are for declaring a type with public properties.
Through type inference, the compiler will figure out thetype of the record by setting its values.
Records can be "cloned".
Mónika Mészáros The F# Programming Language
![Page 88: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/88.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Basic Record Usage
Definition of a record type:
type Person ={ Name: string; DateOfBirth: System.DateTime }
Construction of record values by record labels:
> { Name = "Bill"; DateOfBirth= new System.DateTime(1962,09,02) };;
val it : Person= { Name="Bill"; DateOfBirth = 09/02/1962 }
Mónika Mészáros The F# Programming Language
![Page 89: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/89.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Basic Record Usage
Definition of a record type:
type Person ={ Name: string; DateOfBirth: System.DateTime }
Construction of record values by record labels:
> { Name = "Bill"; DateOfBirth= new System.DateTime(1962,09,02) };;
val it : Person= { Name="Bill"; DateOfBirth = 09/02/1962 }
Mónika Mészáros The F# Programming Language
![Page 90: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/90.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Cloning Records
There is a convenient syntax to clone all the values in therecord, creating a new value, with some values replaced.
type Point3D = { X: float; Y: float; Z: float }let p1 = { X = 3.0; Y = 4.0; Z = 5.0 }
> let p2 = { p1 with Y = 0.0; Z = 0.0 };;val p2 : Point3D
The definition of p2 is identical to this:
let p2 = { X = p1.X; Y = 0.0; Z = 0.0 }
This expression from does not mutate the values of a record.
Mónika Mészáros The F# Programming Language
![Page 91: The F# Programming Languageaszt.inf.elte.hu/~cefp2009/materials/warmup/... · Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records](https://reader033.vdocument.in/reader033/viewer/2022042408/5f23616436b63854e33ac62d/html5/thumbnails/91.jpg)
Contents Introduce Characteristics Developer Tools Basics Lists Higher-Order Functions Tuples Records
Dynamic Type Test via Patterns
let getType (x : obj) =match x with| :? string -> "x is a string"| :? int -> "x is an int"| :? Exception -> "x is an exception"
Mónika Mészáros The F# Programming Language