Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
Created February 24, 2022 12:28
Show Gist options
  • Save ebresafegaga/cc5105be0b1dcf588a0dff7c644e86bf to your computer and use it in GitHub Desktop.
Save ebresafegaga/cc5105be0b1dcf588a0dff7c644e86bf to your computer and use it in GitHub Desktop.
Valid Number leetcode problem at https://leetcode.com/problems/valid-number/
let (>=>) p1 p2 input k = p1 input (fun input -> p2 input k)
let (>=>>) p1 p2 input = p1 input (fun input -> p2 input List.isEmpty)
let digit input k =
let rec loop input =
match input with
| [] -> k []
| x :: input when Char.IsDigit x -> loop input
| input -> k input
match input with
| [] -> false
| x :: input when Char.IsDigit x -> loop input
| _ :: _ -> false
let char ch input k =
match input with
| x :: input when x = ch -> k input
| _ -> false
let dot = char '.'
let alt this that input k = this input k || that input k
let (<|>) = alt
let opt parser input k = parser input k || k input
let sign = char '+' <|> char '-'
let exponent = char 'e' <|> char 'E' >=> opt sign >=> digit
let numExp = digit >=> opt exponent
let integer = opt sign >=> digit >=>> opt exponent
let decimal = opt sign >=> opt digit >=> char '.' >=>> opt numExp
// https://leetcode.com/problems/valid-number/
let isNumber (s : string) =
let s = s.ToCharArray () |> List.ofArray
integer s || decimal s
let numbers = ["2"; "0089"; "-0.1"; "+3.14"; "4."; "-.9"; "2e10"; "-90E3"; "3e+7"; "+6e-1"; "53.5e93"; "-123.456e789"]
List.forall isNumber numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment