# Programming-language examples

Here are a few things focusing on Miller's DSL as a programming language per se, outside of its normal use for streaming record-processing.

## Sieve of Eratosthenes

The [Sieve of Eratosthenes](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) is a standard introductory programming topic. The idea is to find all primes up to some *N* by making a list of the numbers 1 to *N*, then striking out all multiples of 2 except 2 itself, all multiples of 3 except 3 itself, all multiples of 4 except 4 itself, and so on. Whatever survives that without getting marked is a prime. This is easy enough in Miller. Notice that here all the work is in `begin` and `end` statements; there is no file input (so we use `mlr -n` to keep Miller from waiting for input data).

GENMD-RUN-COMMAND
cat programs/sieve.mlr
GENMD-EOF

GENMD-RUN-COMMAND
mlr -n put -f programs/sieve.mlr
GENMD-EOF

## Mandelbrot-set generator

The [Mandelbrot set](http://en.wikipedia.org/wiki/Mandelbrot_set) is also easily expressed. This isn't an important case of data processing (the use-case Miller was designed for), but it is an example of Miller as a general-purpose programming language -- a test case for the expressiveness of the language.

The (approximate) computation of points in the complex plane which are and aren't members is just a few lines of complex arithmetic (see the [Wikipedia article](https://en.wikipedia.org/wiki/Mandelbrot_set)); how to render them visually is another task.  Using graphics libraries you can create PNG or JPEG files, but another fun way to do this is by printing various characters to the screen:

GENMD-RUN-COMMAND
cat programs/mand.mlr
GENMD-EOF

At standard resolution this makes a nice little ASCII plot:

GENMD-RUN-COMMAND
mlr -n put -s iheight=25 -s iwidth=50 -f ./programs/mand.mlr
GENMD-EOF

But using a very small font size (as small as my Mac will let me go), and by choosing the coordinates to zoom in on a particular part of the complex plane, we can get a nice little picture:

GENMD-CARDIFY
#!/bin/bash
# Get the number of rows and columns from the terminal window dimensions
iheight=$(stty size | mlr --nidx --fs space cut -f 1)
iwidth=$(stty size | mlr --nidx --fs space cut -f 2)
mlr -n put \
  -s rcorn=-1.755350 -s icorn=0.014230 -s side=0.000020 -s maxits=10000 -s iheight=$iheight -s iwidth=$iwidth \
  -f programs/mand.mlr
GENMD-EOF

![pix/mand.png](pix/mand.png)
