Skip to content

Instantly share code, notes, and snippets.

@ntlk
Last active August 29, 2015 14:16
Show Gist options
  • Save ntlk/c9ff48542dceb242dc22 to your computer and use it in GitHub Desktop.
Save ntlk/c9ff48542dceb242dc22 to your computer and use it in GitHub Desktop.
Rudimentary, case insensitive grep in OCaml
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