Created
February 12, 2009 05:32
-
-
Save mattpodwysocki/62502 to your computer and use it in GitHub Desktop.
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
#light | |
open System | |
module List = | |
let to_string (xs:char list) : string = | |
new string(Array.of_list xs) | |
module Char = | |
let digitToInt : char -> int = function | |
| c when Char.IsDigit c -> int c - int '0' | |
| c when c >= 'a' && c <= 'f' -> int c - int 'a' + 10 | |
| c when c >= 'A' && c <= 'F' -> int c - int 'A' + 10 | |
| c -> failwith ("Char.digitToInt: not a digit " + string c) | |
let intToDigit : int -> char = function | |
| i when i >= 0 && i <= 9 -> char (int '0' + i) | |
| i when i >= 10 && i <= 15 -> char (int 'a' - 10 + i) | |
| i -> failwith ("Char.intToDigit: not a digit " + string i) | |
// Existing solution | |
let rec loop (acc:int) : char list -> int = function | |
| [] -> acc | |
| x::xs -> let acc' = acc * 10 + Char.digitToInt x | |
loop acc' xs | |
let asInt : string -> int = Seq.to_list >> loop 0 | |
// asInt Fold | |
let asInt_fold : string -> int = | |
let rec asInt_aux : char list -> int = function | |
| ('-'::xs) -> - asInt_aux xs | |
| xs -> let step x acc = 10 * x + Char.digitToInt acc | |
List.fold_left step 0 xs | |
Seq.to_list >> asInt_aux | |
// asInt_Either | |
let asInt_Either : string -> Choice<string, int> = | |
let rec asInt_aux = function | |
| ('-'::xs) -> match asInt_aux xs with | |
| Choice2_1 err -> Choice2_1 err | |
| Choice2_2 v -> Choice2_2 (-v) | |
| xs -> let step (Choice2_2 acc) c = | |
match Char.IsDigit c with | |
| false -> Choice2_1 ("non-digit " + string c) | |
| true -> Choice2_2 (10 * acc + Char.digitToInt c) | |
List.fold_left step (Choice2_2 0) xs | |
Seq.to_list >> asInt_aux | |
// Concat | |
let concat' xs = List.fold_right (@) [] xs | |
// Takewhile | |
let rec takeWhile' (p:'a -> bool) : 'a list -> 'a list = function | |
| [] -> [] | |
| x::xs -> if p x then x :: takeWhile' p xs | |
else [] | |
// Takewhile fold | |
let takeWhile'' (p:'a -> bool) : 'a list -> 'a list = | |
let step x ys = if p x then [x] @ ys else [] | |
List.fold_right step [] | |
let groupBy' (eq:'a -> 'a -> bool) (xs:'a list) : ('a list) list = | |
let step x : 'a list list -> 'a list list = function | |
| [] -> [[x]] | |
| y::ys -> if eq x (List.hd y) then [x::y] @ ys | |
else [x] :: y :: ys | |
List.fold_right step xs [] | |
let any' (eq:'a -> bool) (xs:'a list) : bool = | |
let step x acc = acc || eq x | |
List.fold_right step xs false | |
let isSpace = System.Char.IsWhiteSpace | |
let words' (s:string) : string list = | |
let words'' : char list -> char list list = function | |
| [] -> [[]] | |
| s -> let step c acc = | |
let (a::as') = acc | |
if isSpace c then [] :: acc | |
else (c::a)::as' | |
List.fold_right step s [[]] | |
s |> Seq.to_list |> words'' |> List.map List.to_string | |
let unlines' (xs:string list) : string = | |
let step = ((@) << ((@) (Seq.to_list "\r\n"))) | |
List.fold_right step (List.map Seq.to_list xs) [] |> List.to_string | |
let rec repeat x = x :: repeat x | |
let cycle' xs = List.fold_right (@) (repeat xs) [] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment