Grep

Grep

Hard

Introduction

You have taken a job at a local library helping organize their collection of old books. The student patrons are often hunting for half-remembered quotes to cite in their term papers. Rather than manually read every book from cover to cover, you decide to build a small tool to scan them, looking for these partial quotes.

Instructions

Search files for lines matching a search string and return all matching lines.

The Unix grep command searches files for lines that match a regular expression. Your task is to implement a simplified grep command, which supports searching for fixed strings.

The grep command takes three arguments:

  1. The string to search for.
  2. Zero or more flags for customizing the command's behavior.
  3. One or more files to search in.

It then reads the contents of the specified files (in the order specified), finds the lines that contain the search string, and finally returns those lines in the order in which they were found. When searching in multiple files, each matching line is prepended by the file name and a colon (':').

Flags

The grep command supports the following flags:

  • -n Prepend the line number and a colon (':') to each line in the output, placing the number after the filename (if present).
  • -l Output only the names of the files that contain at least one matching line.
  • -i Match using a case-insensitive comparison.
  • -v Invert the program -- collect all lines that fail to match.
  • -x Search only for lines where the search string matches the entire line.

IO process

Unlike other exercises, grep requires interacting with the "external world": reading files, handling system errors and printing to stdout.

In Lean, interacting with the system is usually done in the IO monad, which allows for side effects to occur in a contained environment without affecting the pure state of most other functions.

Reading arguments

Lean allows for a file to be called as a script, either directly as a standalone script via lean --run or with the use of lake.

A lean project has one point of entry, a function called main, that takes a List String with all arguments passed to it. In order to simulate running Grep.lean as an executable, arguments are passed in the same way in this exercise.

Reading files

Lean offers functions to manipulate files in the IO.FS monad. For example, IO.FS.readFile may be used to read the contents of a file in a given path, as a String.

Writing output

Instead of returning a value, this exercise requires results are written to standard output or standard error. There are functions in Lean for this purpose.

Handling system errors

Unlike errors in Except, IO exceptions are actually runtime exceptions. You may use try/catch to handle them:

try
    /- 
        code that may possibly raise an exception 
    -/
catch msg =>
    /-
        code that is executed only if an exception was thrown
    -/
Edit via GitHub The link opens in a new window or tab
Lean Exercism

Ready to start Grep?

Sign up to Exercism to learn and master Lean with 100 exercises, and real human mentoring, all for free.