create account

F# Basic Syntax Cheat Sheet by woz.software

View this thread on: hive.blogpeakd.comecency.com
· @woz.software ·
$1.34
F# Basic Syntax Cheat Sheet
![](http://i.imgur.com/GB4RoBB.jpg)

I have not had much time for my [Roguelike](https://steemit.com/programming/@woz.software/building-a-roguelike-in-f-from-scratch-take-items) so thought I would throw together a quick cheat sheet of some basic F# syntax to help people understand some of the code

```
// Declare value binding

let unitValue = ()          // unit
let anInt = 42              // int value
let aString = "blah"        // string value
let aFloat = 4.2            // float value
let aDouble = 4.2m          // double value

// Declare functions

let noArgs () = 42          // No such thing as no arg function unit -> int
let add1 x = x + 1          // One arg function
let add x y = x + y         // Two arg function

// Generic functions

let inline genAdd x y = x + y 

// Function calling

let result0 = noArgs ()     // = 42
let result1 = add1 41       // = 42
let result2 = add 40 2      // = 42

// Error add takes ints as first use was ints
//let error = add 4.0 5.0

// Can use for anything with + as inlined
let intAdd = genAdd 5 6
let floatAdd = genAdd 5.0 6.0

// Partial application

let anotherAdd1 = add 1     // = int -> int
let result = anotherAdd1 41 // = 42

// Value piping

let piped1 = 40 |> add 2    // = 42
let piped2 = add <| 2 <| 40 // = 42

// Tuples

let tuple = (4, "2")        // (int * string)
let four = fst tuple        // int
let two = snd tuple         // string
let (first, second) = tuple // Extract both into bindings
let (f, _) = tuple          // Extract the first value
let (_, S) = tuple          // Extract the second value

// Records

type record = {id: int; name: string}
let aRecord = {id = 5; name = "Bill"}
let id = aRecord.id         // = 5
let name = aRecord.name     // = "Bill"
let updated = {aRecord with name = "Bob"}

// Unions

type nullable =
    | Value of int
    | Null

let aValue = Value 5        // Value of 5
let nothing = Null          // Null (Not null as in C but type)

// Pattern matching

let extract x =
    match x with
    | Value y -> y
    | Null -> 0

let extractedValue = extract aValue // = 5
let extractedNull = extract nothing // = 0
```

Feel free to ask any questions

Happy coding

Woz
👍  , , , , , , ,
properties (23)
authorwoz.software
permlinkf-basic-syntax-cheat-sheet
categoryprogramming
json_metadata{"tags":["programming","computer","technology","howto","tutorial"],"image":["http://i.imgur.com/GB4RoBB.jpg"],"links":["https://steemit.com/programming/@woz.software/building-a-roguelike-in-f-from-scratch-take-items"],"app":"steemit/0.1","format":"markdown"}
created2017-09-01 09:25:21
last_update2017-09-01 09:25:21
depth0
children6
last_payout2017-09-08 09:25:21
cashout_time1969-12-31 23:59:59
total_payout_value1.062 HBD
curator_payout_value0.278 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,184
author_reputation2,321,910,395,519
root_title"F# Basic Syntax Cheat Sheet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id13,533,866
net_rshares359,195,091,698
author_curate_reward""
vote details (8)
@drifter1 ·
$0.02
Awesome! Nice job!
That needs a resteem...
And I'm waiting for the next Roquelike Tutorial ;)
👍  
properties (23)
authordrifter1
permlinkre-wozsoftware-f-basic-syntax-cheat-sheet-20170901t093434147z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-01 09:34:33
last_update2017-09-01 09:34:33
depth1
children2
last_payout2017-09-08 09:34:33
cashout_time1969-12-31 23:59:59
total_payout_value0.020 HBD
curator_payout_value0.001 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length93
author_reputation98,202,866,830,354
root_title"F# Basic Syntax Cheat Sheet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id13,534,435
net_rshares6,072,153,650
author_curate_reward""
vote details (1)
@woz.software · (edited)
$0.03
Yep, things been getting busy at work. A re-write of our core product I have been working on for many many years ships November :O  Big cognative load as 300000+ lines of C# and JS front end.

Also finally planned our trip back to UK to see friends and family, not been home in 6 years. Also going to visit Chernobyl as a Detour, somewhere I have wanted to see for soooooo long

So as soon as get a gap will dive back in.
👍  
properties (23)
authorwoz.software
permlinkre-drifter1-re-wozsoftware-f-basic-syntax-cheat-sheet-20170901t095153651z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-01 09:51:54
last_update2017-09-01 09:53:06
depth2
children1
last_payout2017-09-08 09:51:54
cashout_time1969-12-31 23:59:59
total_payout_value0.024 HBD
curator_payout_value0.008 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length421
author_reputation2,321,910,395,519
root_title"F# Basic Syntax Cheat Sheet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id13,535,648
net_rshares9,364,880,337
author_curate_reward""
vote details (1)
@drifter1 ·
Oh, that was the reason!
Good luck in your re-coding and have fun on your trip back home :)
properties (22)
authordrifter1
permlinkre-wozsoftware-re-drifter1-re-wozsoftware-f-basic-syntax-cheat-sheet-20170901t120106381z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-01 12:01:06
last_update2017-09-01 12:01:06
depth3
children0
last_payout2017-09-08 12:01:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length91
author_reputation98,202,866,830,354
root_title"F# Basic Syntax Cheat Sheet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id13,545,257
net_rshares0
@shantamcbain ·
$0.03
Great job. Sorry I am not familiar with F# what language is it. My experience is limited to Perl, pascal, small amounts of python. The latter for control and monitoring systems.
👍  
properties (23)
authorshantamcbain
permlinkre-wozsoftware-f-basic-syntax-cheat-sheet-20170902t175247739z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-02 17:52:45
last_update2017-09-02 17:52:45
depth1
children1
last_payout2017-09-09 17:52:45
cashout_time1969-12-31 23:59:59
total_payout_value0.020 HBD
curator_payout_value0.005 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length177
author_reputation1,455,168,455,192
root_title"F# Basic Syntax Cheat Sheet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id13,670,730
net_rshares6,796,406,029
author_curate_reward""
vote details (1)
@woz.software ·
It is an ML based language but runs on .NET (Also mono so Linux and mac)

Full static typing so everything you see knows the types and flags errors. 

Haskell has some roots in ML but then added all the category theory head spin stuff.

Really like the language.  Really terse but expressive. Take a look at my Building a Roguelike series and you will see it used in anger :)
👍  
properties (23)
authorwoz.software
permlinkre-shantamcbain-re-wozsoftware-f-basic-syntax-cheat-sheet-20170902t215305264z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-02 21:53:06
last_update2017-09-02 21:53:06
depth2
children0
last_payout2017-09-09 21:53:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length375
author_reputation2,321,910,395,519
root_title"F# Basic Syntax Cheat Sheet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id13,687,556
net_rshares719,798,482
author_curate_reward""
vote details (1)
@yash0108 · (edited)
$0.02
good job
follow u
👍  
properties (23)
authoryash0108
permlinkre-wozsoftware-f-basic-syntax-cheat-sheet-20170902t082608458z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-02 08:26:12
last_update2017-09-02 08:26:45
depth1
children0
last_payout2017-09-09 08:26:12
cashout_time1969-12-31 23:59:59
total_payout_value0.018 HBD
curator_payout_value0.005 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length17
author_reputation3,386,741,504,526
root_title"F# Basic Syntax Cheat Sheet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id13,628,161
net_rshares6,694,378,701
author_curate_reward""
vote details (1)