Learn how to perform conditional assignment using if-then-else expressions.
- [Instructor] Almost every programming language has the concept of if/else branches. It's obviously one of the most fundamental things in programming, so here we're going to demonstrate the F# if/else expression, in the context of something that's going to produce a prompt saying there are six items, or there is one item, and obviously intelligently decide whether it needs to pluralize the 'is' and the 'are', and the item/items. So we're defining something called 'prompt', it takes a number of items, and then we need to say either is, an item, or are, and items.
And as soon as you have a little grouping of items like that you're starting to think about tupples, or 'tuples', and sure enough we're binding a tuple here. And the if and else is: if n is one, then return the is and the item, in all other cases return are and items. So at the end of that process if n was one, s1 would be is, and s2 would be item, and if n was any other value, s1 would be are, and s2 would be items. And then we use something we looked at before, printf, to build ourselves a little string that makes use of those values.
Let's try this out. Now, my suffix test is one, and by the way you saw I used printf, I probably should have used printfn to get a new line, so let's just correct that, send it back to F# interactive, and you see I got that there, so I need to try it again. There is one item. Try that with some other value; there are 100 items. So that's the if/then, and you almost always in F# have an if and an else branch, because if you're assigning something somewhere else, you obviously need to assign it in all circumstances, otherwise what are s1 and s2 going to get? There are exceptions to that, in so-called imperative code, where you're not assigning values, you're doing something, but it's a bit of an antipattern, you almost always, in F# want to be saying if and else.
- Defining values and calling functions in F#
- Defining and identifying discriminated unions
- Working with if-else expressions
- Writing unit test
- Using type providers to access data
- Analyzing data with collection functions
- Plotting data using the R type provider
- Using railway-oriented programming to handle errors
- Integrating with Twitter
- Deploying an F# application to Azure
Skill Level Intermediate
1. Get Started with F#
2. Build a Simple Parser with Unit Testing
3. Use F# CSV Type Provider to Get Data
4. Analyze Data with F# Collection Functions
5. Use RStats Provider and ggplot2 to Plot Data
6. Use BoxKite with Twitter
7. Deploy a Working Bot
Next steps1m 46s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.