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
// Tail recursive computation of List's length | |
let length list = | |
// Auxiliary function to compute length | |
// It store intermediate result in acc. | |
let rec lengthAux acc list = | |
match list with | |
| [] -> acc | |
| _::tail -> lengthAux (acc+1) tail | |
lengthAux 0 list // invoking lengthAux with acc = 0 |
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
let rec (|%|) a n = | |
match a, n with | |
|_ when a < n -> a | |
|_ when a>=n -> (|%|) (a-n) n | |
//Resulting signature: val ( |%| ) : int -> int -> int |
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
let isprime n = | |
let rec isprimeAux i n = | |
i > n/2 || (n%i <> 0 && (isprimeAux (i+1) n)) | |
isprimeAux 2 n | |
//> output: | |
// | |
//val isprime : int -> bool | |
// | |
//> isprime 10;; |
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
let names = ["alice"; "bob"; "carol"] | |
let name = List.tryFind (fun s -> length s < 4) names | |
match name with | |
| Some n -> printfn "%s" n | |
| None -> printfn "They all have long names" | |
// partial application | |
let doubled = Seq.map (fun v -> v *2) values |
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
let avg list = | |
let rec avgAux sum length list = | |
match list with | |
| [] -> sum/length | |
| x::xs -> avgAux (x+sum) (length + 1) xs | |
avgAux 0 0 list |
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
let countElements list = | |
let rec countElementsAux sum length l= | |
match l with | |
| [] -> (sum/length, 0) // computing average from sum and length accumulator | |
| x :: xs -> | |
let (avg,count) = countElementsAux (x+ sum) (length+1) xs | |
if x < avg then (avg,count) else (avg,count+1) | |
let (avg, count) = countElementsAux 0 0 list | |
count |
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
// Recursive solution | |
let eulerProblem2Answer'' = | |
let maxF = 4000000 | |
let rec computeSum f1 f2 sum = | |
match f1 + f2 with | |
| n -> computeSum f2 n sum | |
| n when n%2 = 0 -> if (n<maxF) then (computeSum f2 n (sum+n)) else sum | |
computeSum 0 1 0 |
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
let eulerProblem2Answer = | |
(0,1) | |
|> Seq.unfold (fun (f1,f2) -> Some(f2, (f2, f1+f2))) // Fibonacci Sequence | |
|> Seq.takeWhile(fun x -> x < 4000000) // Taking upto Max Fibbonacci | |
|> Seq.filter (fun x -> x%2=0) // Filtering only even numbers | |
|> Seq.sum // computing sum |
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
let eulerProblem2Answer' = | |
(0,1) | |
|> Seq.unfold (fun (f1,f2) -> Some(f2, (f2, f1+f2))) | |
|> Seq.takeWhile(fun x -> x < 4000000) | |
|> Seq.sumBy (fun x -> if (x%2)=0 then x else 0) // using SumBy with a projection |
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
let length list = | |
List.fold | |
(fun acc _ -> acc+1) | |
0 | |
list |