1. # Dealing with invalid Input

17 May 2014

Recently I’ve been discussing about unit-tests and contracts, and the question arose if mechanism existed that would check at compile-time whether a value satisfied a certain condition or not.

This is a difficult question and no doubt a hot topic in computer-science research.

16 May 2014

# Introduction

What are “contracts”? Contracts are the key element in the design-by-contract technique, natively supported by the Eiffel programming language.

They allow operate at many levels:

1. methods: as pre- and post-conditions
2. classes: as class invariants
3. loops: as loop variants/invariants

In this article we shall only consider method pre- and post-conditions. That means, before method execution the pre-condition is checked and after the method is finished (upon completion) the post-condition is checked.

3. # Scala Beginners: counting words and lines

07 February 2014

## A simple command-line tool

At work, I’m teaching one intern and one colleague how to program.

I gave them the following exercise:

Write an utility that takes 3 command-line parameters P1, P2 and P3. P3 is OPTIONAL (see below) P1 is always a file path/name. P2 can take the values:

• “lines”
• “words”
• “find”

Only P2 is “find”, then P3 is relevant/needed, otherwise it is not.

So, the utility does the following:

• If P2 is “rows” it says how many lines it has
• If P2 is “words” it says how many words it has (the complete file)
• If P2 is “find” it prints out the lines where P3 is present
4. # P05 - Reverse a List

17 January 2014

Clojure – recursive:

"P05 (*) Reverse a list" (loop [ret [] xs xs] (if (empty? xs) ret (recur (cons (first xs) ret) (next xs))))) read more
5. # P04 - Find the Number of Elements of a List

16 January 2014

Clojure recursive:

"P04 (*) Find the number of elements of a list." (loop [num 0 xs xs] (if (next xs) (recur (+ num 1) (next xs)) (+ num 1)))) read more
6. # P03 - Find the nᵗʰ Element of a List

16 January 2014

This problem is very nice because the Clojure and Scala implementations are practically the same. It allows to appreciate the different syntaxes better.

Below is Peter’s solution to the problem, in Clojure:

{:doc "P03 (*) Find the Kth element of a list." :pre [(seq? xs)]} (if (= 0 n) (first xs) (recur (- n 1) (next xs))) ) read more
7. # P02 - Find the Penultimate Element of a List

16 January 2014

Peter brings us this solution:

{:doc "P02 (*) Find the last but one element of a list." :pre [(seq? xs)]} (loop [ret (first xs) xs xs] (if (next xs) (recur (first xs) (next xs)) ret))) read more
8. # P01 - Find the last element of a list

16 January 2014

The first problem is to find the last element of a list.

This is Peter’s Clojure version:

"P01 (*) Find the last element of a list." (if (next input) (recur (next input)) (first input)))

And here is my Scala version: