foreach(hour; life) {/*do something here...*/}

Friday, 26 July 2013

So it seems you weren't totally put off by my first post and have decided to see what other bugs I can accidentally put in to a trivial program! Onwards and upwards...

First things first: The two version in my previous post where actually not equivalent, as Andrei kindly pointed out to me. While the first, imperative, version prints a space after the last argument, the second version does not! A rookie error from me... Following the echo spec., the arguments should be separated by spaces, but there is no mention of trailing space. Hence, we need a fixed version of the imperative code:

, if we do then we print all the arguments - barring the last one - as before, but then print the last one on its own. Note that we could have achieved the same thing with an

if

statement in the loop checking whether we are on the last element and omitting the space. I prefer the explicit separation; sometimes (normally in more complicated cases) your optimiser will too. We finish off with a newline, whether we had any arguments or not. Bug fixed!

Now things get a bit more interesting. As with many supposedly "simple" command line tools, the devil is in the options. Take a look here to peruse a normal list of options for echo. Let's make a start with the "-n" option. If you pass the argument "-n", before any non-option arguments (anything other than "-n" for us at the moment), echo will not print a newline at the end of it's output.

In order to achieve this, we're going to introduce an preliminary loop to detect the option and a boolean variable

Sunday, 7 July 2013

So... This is going to be a blog about the D programming language. I've been involved in the D community for a while over a year now; with their friendly and patient help I've managed to become quite familiar with the language. I'm not going to attempt to teach you the language in any rigorous manner, for this I would recommend either Ali Çehreli's online book Programming in D (starts from the very basics of programming. Free) or Andrei's tour de forceThe D Programming Language (for the more experienced reader. Totally worth the money). Instead I'm going to dive in to building some simple programs in D, introducing syntax and features in an entirely ad-hoc manner, the meaning of which will hopefully become obvious by context if not by my explanations. Those coming from C or a related language will likely find the whole experience familiar.

, albeight a very simplified version. We'll build on it later to get a fully-featured implementation. Firstly, let's take a line-by-line look at what we've got so far:

First, we import

write

and

writeln

from

std.stdio

. We could just write

import std.stdio;

and import the whole module, but it's nice to keep things a little more hygienic.

import std.stdio : write, writeln;

Then comes

main

, the entry point to the program. Seeing as we're writing

echo

, we're going to need the arguments to the program, so we're including the optional

string[]

argument to

main

to capture them.

void main(string[] args)

We want to print out all the arguments, barring the first argument which is always the executable name, separated by spaces. To iterate over the arguments we are using a

foreach

loop, accessing each argument in turn through the variable

arg

. In order to skip the first argument, we are actually iterating over a slice of

args

, from index

1

to the end of the array (automatically provided by

$

). This isn't affecting

args

itself, but simply providing us with a narrowed window on to it. For more on slices, see here. We then - for each

arg

-

write

the argument followed by a space.

foreach(arg; args[1..$])
{
write(arg, ' ');
}

finally, we finish things off with a new line, courtesy of

writeln

.

writeln();

Hooray! We have a fully working D program that actually does something (vaguely) useful. For those of you who found that a little too obvious, here's an equivalent D program with a lazy functional flavour: