Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 11, 2016
0 parents
commit 5eaaecd
Showing
211 changed files
with
9,212 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#light | ||
|
||
/// Analyze a string for duplicate words | ||
let wordCount text = | ||
let words = String.split [' '] text | ||
let wordSet = Set.of_list words | ||
let nWords = words.Length | ||
let nDups = words.Length - wordSet.Count | ||
(nWords,nDups) | ||
|
||
let showWordCount text = | ||
let nWords,nDups = wordCount text | ||
printfn "--> %d words in the text" nWords | ||
printfn "--> %d duplicate words" nDups | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let (nWords,nDups) = wordCount "All the king's horses and all the king's men";; | ||
|
||
nWords;; | ||
|
||
nDups;; | ||
|
||
nWords - nDups;; | ||
|
||
showWordCount "Couldn't put Humpty together again";; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/// Analyze a string for duplicate words | ||
let wordCount text = | ||
let words = String.split [' '] text in | ||
let wordSet = Set.of_list words in | ||
let nWords = words.Length in | ||
let nDups = words.Length - wordSet.Count in | ||
(nWords,nDups) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#light | ||
|
||
let powerOfFourPlusTwo n = | ||
let n = n * n | ||
let n = n * n | ||
let n = n + 2 | ||
n;; | ||
|
||
let powerOfFourPlusTwo n = | ||
let n1 = n * n | ||
let n2 = n1 * n1 | ||
let n3 = n2 + 2 | ||
n3;; | ||
|
||
let powerOfFourPlusTwoTimesSix n = | ||
let n3 = | ||
let n1 = n * n | ||
let n2 = n1 * n1 | ||
n2 + 2 | ||
let n4 = n3 * 6 | ||
n4;; | ||
|
||
let invalidFunction n = | ||
let n3 = | ||
let n1 = n + n | ||
let n2 = n1 * n1 | ||
n1 * n2 | ||
let n4 = n1 + n2 + n3 // Error! n3 is in scope, but n1 and n2 are not! | ||
n4;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#light | ||
|
||
String.split;; | ||
|
||
String.split [' '] "hello world";; | ||
|
||
String.split ['a';'e';'i';'o';'u'] "hello world";; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#light | ||
|
||
Set.of_list ["b";"a";"b";"b";"c" ];; | ||
|
||
Set.to_list (Set.of_list ["abc"; "ABC"]);; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#light | ||
|
||
/// Analyze a string for duplicate words | ||
let wordCount text = | ||
let words = String.split [' '] text | ||
let wordSet = Set.of_list words | ||
let nWords = words.Length | ||
let nDups = words.Length - wordSet.Count | ||
(nWords,nDups) | ||
|
||
let site1 = ("www.cnn.com",10) | ||
let site2 = ("news.bbc.com",5) | ||
let site3 = ("www.msnbc.com",4) | ||
let sites = (site1,site2,site3) | ||
|
||
fst site1;; | ||
|
||
let relevance = snd site1;; | ||
|
||
relevance;; | ||
|
||
let url, relevance = site1;; | ||
|
||
let site1,site2,site3 = sites;; | ||
|
||
let showResults (nWords,nDups) = | ||
printfn "--> %d words in the text" nWords | ||
printfn "--> %d duplicate words" nDups;; | ||
|
||
let showWordCount text = showResults (wordCount text);; | ||
|
||
let a,b = (1,2,3);; /// Error! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#light | ||
|
||
open System.Windows.Forms | ||
|
||
let form = new Form(Visible=true,TopMost=true,Text="Welcome to F#") | ||
|
||
let textB = new RichTextBox(Dock=DockStyle.Fill, Text="Here is some initial text") | ||
form.Controls.Add(textB) | ||
|
||
open System.IO | ||
open System.Net | ||
|
||
/// Get the contents of the URL via a web request | ||
let http(url: string) = | ||
let req = System.Net.WebRequest.Create(url) | ||
let resp = req.GetResponse() | ||
let stream = resp.GetResponseStream() | ||
let reader = new StreamReader(stream) | ||
let html = reader.ReadToEnd() | ||
resp.Close() | ||
html | ||
|
||
let google = http("http://www.google.com") | ||
textB.Text <- http("http://news.bbc.co.uk") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#light | ||
|
||
open System.Windows.Forms;; | ||
|
||
let form = new Form(Visible=true,TopMost=true,Text="Welcome to F#");; | ||
|
||
let textB = new RichTextBox(Dock=DockStyle.Fill, Text="Here is some initial text");; | ||
form.Controls.Add(textB);; | ||
|
||
open System;; | ||
open System.IO;; | ||
open System.Net;; | ||
|
||
let req = WebRequest.Create("http://www.microsoft.com");; | ||
|
||
let resp = req.GetResponse();; | ||
|
||
let stream = resp.GetResponseStream();; | ||
|
||
let reader = new StreamReader(stream);; | ||
|
||
let html = reader.ReadToEnd();; | ||
|
||
textB.Text <- html;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Example01 | ||
Listing 2-1, pp. 7 | ||
|
||
Example02 | ||
Listing 2-2, pp. 9 | ||
|
||
Example03 | ||
Bindings, pp. 11 | ||
|
||
Example04 | ||
Calling functions, pp. 14 | ||
|
||
Example05 | ||
Using Data Structures, pp. 16 | ||
|
||
Example06 | ||
Using Tuples, pp. 17 | ||
|
||
Example07 | ||
Listing 2-3, pp. 20 | ||
|
||
Example08 | ||
Fetching a Web Page, pp. 24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#light | ||
|
||
2147483647+1;; | ||
|
||
let doubleAndAdd a b = a * a + b;; | ||
|
||
let doubleAndAdd (a:float) b = a * a + b;; | ||
|
||
let doubleAndAdd (a:float) (b:float) : float = a * a + b;; | ||
|
||
let encode (n: int32) = | ||
if (n >= 0 && n <= 0x7F) then [ n ] | ||
elif (n >= 0x80 && n <= 0x3FFF) then [ (0x80 ||| (n >>> 8)) &&& 0xFF; | ||
(n &&& 0xFF) ] | ||
else [ 0xC0; ((n >>> 24) &&& 0xFF); | ||
((n >>> 16) &&& 0xFF); | ||
((n >>> 8) &&& 0xFF); | ||
(n &&& 0xFF) ];; | ||
|
||
encode 32;; | ||
|
||
encode 320;; | ||
|
||
encode 32000;; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#light | ||
|
||
"MAGIC"B;; | ||
|
||
let dir = @"c:\Program Files";; | ||
|
||
let s = "All the kings horses | ||
and all the kings men";; | ||
|
||
let s = "Couldn't put Humpty";; | ||
|
||
s.Length;; | ||
|
||
s.[13];; | ||
|
||
let s = "Couldn't put Humpty";; | ||
|
||
s.[13] <- 'h';; /// Error! | ||
"Couldn't put Humpty" + " " + "together again";; | ||
|
||
let buf = new System.Text.StringBuilder();; | ||
|
||
buf.Append("Humpty Dumpty");; | ||
|
||
buf.Append(" sat on the wall");; | ||
|
||
buf.ToString();; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#light | ||
|
||
let oddPrimes = [3; 5; 7; 11] | ||
let morePrimes = [13; 17] | ||
let primes = 2 :: (oddPrimes @ morePrimes) | ||
|
||
let people = [ "Adam"; "Dominic"; "James" ];; | ||
|
||
people;; | ||
|
||
"Chris" :: people;; | ||
|
||
people;; | ||
|
||
let printFirst primes = | ||
match primes with | ||
| h :: t -> printfn "The first prime in the list is %d" h | ||
| [] -> printfn "No primes found in the list";; | ||
|
||
printFirst oddPrimes;; | ||
|
||
List.hd [5; 4; 3];; | ||
|
||
List.tl [5; 4; 3];; | ||
|
||
List.map (fun x -> x*x) [1; 2; 3];; | ||
|
||
List.filter (fun x -> x % 3 = 0) [2; 3; 5; 7; 9];; | ||
|
||
type 'a option = | ||
| None | ||
| Some of 'a;; | ||
|
||
|
||
let people = [ ("Adam", None); | ||
("Eve" , None); | ||
("Cain", Some("Adam","Eve")); | ||
("Abel", Some("Adam","Eve")) ];; | ||
|
||
let showParents (name,parents) = | ||
match parents with | ||
| Some(dad,mum) -> printfn "%s has father %s, mother %s" name dad mum | ||
| None -> printfn "%s has no parents!" name;; | ||
|
||
showParents ("Adam",None);; | ||
|
||
open System.IO;; | ||
|
||
/// Get the contents of the URL via a web request | ||
let http(url: string) = | ||
let req = System.Net.WebRequest.Create(url) | ||
let resp = req.GetResponse() | ||
let stream = resp.GetResponseStream() | ||
let reader = new StreamReader(stream) | ||
let html = reader.ReadToEnd() | ||
resp.Close() | ||
html | ||
|
||
let fetch url = | ||
try Some(http(url)) | ||
with :? System.Net.WebException -> None | ||
|
||
match (fetch "http://www.nature.com") with | ||
| Some(text) -> printfn "text = %s" text | ||
| None -> printfn "**** no web page found";; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#light | ||
|
||
let round x = | ||
if x >= 100 then 100 | ||
elif x < 0 then 0 | ||
else x;; | ||
|
||
let round x = | ||
match x with | ||
| _ when x >= 100 -> 100 | ||
| _ when x < 0 -> 0 | ||
| _ -> x;; | ||
|
||
let round2 (x,y) = | ||
if x >= 100 || y >= 100 then 100,100 | ||
elif x < 0 || y < 0 then 0,0 | ||
else x,y;; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#light | ||
|
||
let rec factorial n = if n <= 1 then 1 else n * factorial (n-1);; | ||
|
||
factorial 5;; | ||
|
||
let rec length l = | ||
match l with | ||
| [] -> 0 | ||
| h :: t -> 1 + length t;; | ||
|
||
open System.IO | ||
|
||
/// Get the contents of the URL via a web request | ||
let http(url: string) = | ||
let req = System.Net.WebRequest.Create(url) | ||
let resp = req.GetResponse() | ||
let stream = resp.GetResponseStream() | ||
let reader = new StreamReader(stream) | ||
let html = reader.ReadToEnd() | ||
resp.Close() | ||
html;; | ||
|
||
let rec repeatFetch url n = | ||
if n > 0 then | ||
let html = http url | ||
printfn "fetched <<< %s >>> on iteration %d" html n | ||
repeatFetch url (n-1);; | ||
|
||
let rec badFactorial n = if n <= 0 then 1 else n * badFactorial n;; | ||
|
||
let rec even n = (n = 0u) || odd(n-1u) | ||
and odd n = (n <> 0u) && even(n-1u);; | ||
|
||
let even (n:uint32) = (n % 2u) = 0u;; | ||
let odd (n:uint32) = (n % 2u) = 1u;; | ||
|
Oops, something went wrong.