Line Filters in OCaml
Our line filter program reads input from stdin, processes it, and then prints a derived result to stdout. In this case, we’ll create a program that capitalizes all input text.
Let’s break down the code:
We use the
open Printf
statement to bring printf functions into scope.The
process_lines
function is the core of our program:- It uses a
try-with
block to handle exceptions. - Inside an infinite loop, it reads a line from stdin using
input_line
. - It then converts the line to uppercase using
String.uppercase_ascii
. - The uppercase line is printed to stdout using
printf
. - This continues until an
End_of_file
exception is raised (which happens when there’s no more input). - Any other exceptions are caught, printed to stderr, and cause the program to exit with an error code.
- It uses a
The main part of the program simply calls
process_lines
.
To try out our line filter, first make a file with a few lowercase lines:
Then use the line filter to get uppercase lines:
This OCaml version provides the same functionality as the original example. It reads lines from standard input, converts them to uppercase, and prints the result to standard output. The main difference is in error handling: OCaml uses exceptions for end-of-file and other errors, while the original example used a specific error checking method.