Last active
August 29, 2015 14:16
-
-
Save ntlk/c9ff48542dceb242dc22 to your computer and use it in GitHub Desktop.
Rudimentary, case insensitive grep in OCaml
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 pattern = Sys.argv.(1);; | |
let filename = Sys.argv.(2);; | |
let string_contains s1 s2 = | |
try | |
let len = String.length s2 in | |
for i = 0 to String.length s1 - len do | |
let lowercase_s1 = String.lowercase s1 in | |
let lowercase_s2 = String.lowercase s2 in | |
if String.sub lowercase_s1 i len = lowercase_s2 then raise Exit | |
done; | |
false | |
with Exit -> true | |
let output s = | |
Printf.printf "%s\n" s;; | |
let chan = open_in filename in | |
try | |
while true; do | |
let line = input_line chan in | |
if string_contains line pattern then output line | |
done; | |
with End_of_file -> | |
close_in chan; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment