A SkillsCast for this session is not available.
"Why is functional programming so scary?"
Yes, functors, monads etc are mysterious. But we're not going to talk about them anyway, so don’t worry. But if they were renamed, they wouldn’t be scary. Also, OO has really scary buzzwords too, with polymorphism, contravariance, SOLID, IoC, etc. etc
“hello world" in F#
Things to notice about this. E.g. no containing class. Spaces not parens.
Typesafe print string, type inference, etc.
[interactive: play with printf -- see type errors]
Some basic F# syntax
Indentation, let for values and functions. Use Phil’s C# light syntax as an intro
[interactive: define some simple functions, get comfortable with whitespace]
10 min guide to functional programming
F# is "pipes" and "transformation" oriented. Composition as key principle.
Introduce pipes.
Introduce map, filter, reduce and simple higher order functions.
Introduction to partial application in conjunction with map (aha! so this is why F# has whitespace between params rather than parens!)
Function composition. Everything is an expression (omg even switches?)
[interactive: play with lists and partials]
Simple exercises:
- Write wrappers for System.String replace, startsWith, contains,
split, trim
- Use partial application to filter and map lists of strings
- Find the longest string that contains both “h” and “e”
- Split a string into words and find the most frequent word
"Refactoring to HOFs"
Examples of refactoring loops, etc to use hofs using fold, reduce
[interactive: get comfortable with hofs as parameters]
"Why C# is not a reasonable language"
Why reasoning about code is important.
Show examples from C# and guess whether they will work just by looking at the code.
Compare with examples from F# .
Introduce immutability, values have to be initialized, lack of nulls, everything is an expression.
mutability as a code smell.
[interactive: get comfortable with immutability, initialization, expressions]
Pattern matching and collections
More on collection classes
Intro to pattern matching with lists, and also as a “switch” statement.
interactive: get comfortable with list vs seq vs array vs map.
interactive: write a roman numeral parser
Session 1 recap
Should be now be comfortable with
- syntax & whitespace.
- creating simple functions
- piping & composition
- map/filter
- partials, simple hofs.
- not getting freaked out by type errors
Most important, F# should be demystified now -- just a “normal” language, and no mention of monads anywhere. But should be a few aha moments -- e.g. partial application.
YOU MAY ALSO LIKE:
Introduction to F#
Scott Wlaschin
Scott has over 20 years experience in software development, design and architecture, covering all aspects of business software.