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
// Length of a List | |
let length list = List.fold (fun acc _ -> acc+1) 0 list | |
// Nth Element of a List | |
let rec nth list index = | |
match list with | |
| x::xs when index > 0 -> nth xs (index-1) | |
| x::xs when index = 0 -> x | |
| _ -> failwith "Index out of range" |
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 last' list = | |
match list with | |
| [] -> failwith "Empty List" | |
| x::[] -> x | |
| _::xs -> last' xs |
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 last'' list = list |> List.rev |> List.head |
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 last''' list = list |> List.reduce(fun acc x -> x) |
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 rev (s:string) = new string(s.ToCharArray() |> Array.rev) | |
let isPalindrome (text:string) = | |
text = rev text |
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 max_list l = (List.reduce max) l;; |
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 fold op acc l = | |
match l with | |
| [] -> acc | |
| h :: t -> fold op (op acc h) t | |
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
// problem definition : https://www.spoj.com/problems/ADDREV/ | |
open System | |
let parseTuple() : int*int = | |
let line = Console.ReadLine().Split() | |
line.[0] |> int , line.[1] |> int | |
let reverseInt n = | |
let rec reverseInt' n acc = | |
let acc' = acc + (n%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
//Problem Statement : https://www.spoj.com/problems/GCD2/ | |
open System | |
let parseLine() = | |
let line = System.Console.ReadLine().Split() | |
line.[0] |> int, line.[1] |> string | |
let rec gcd a b = | |
match b with | |
| 0 -> a | |
| _ -> gcd b (a%b) |
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
open System | |
let time f x = | |
let timer = System.Diagnostics.Stopwatch() | |
timer.Start() | |
try | |
f x | |
finally | |
timer.Stop() | |
printfn "Took %dms" timer.ElapsedMilliseconds |