Category Archives: Computing

The forgotten paradigm

Programming paradigms, ways of thinking about and structuring code has been around since the dawn of time – since man started to think of the goto statement as a bad thing and structured programming made its way into the spotlight. The major paradigms are

  • Imperative
  • Functional
  • Declarative

Of the first two, examples are everywhere. Imperative languages are everywhere. Examples are almost all popular languages: C, C++, C#, Java, Python, Perl, Ruby, the list goes on and on. Functional languages are less common, though increasing in popularity since they offer good ways of handling concurrency and parallellism. Examples are Haskell, F#, Clojure and LISP.

But what of the third? Can you name a declarative language?

When you program in a declarative style, you tell the computer what you want instead of telling it what to do. There are very few declarative languages, though one has reached a ubiquitous status, SQL. Think about it, apart from T-SQL, the bastard lovechild of imperative and declarative database access, SQL will never require you to tell it how to get its data, you tell it the rules that the data you want back will obey.

What if you could do the same sort of problem solving in your code?

Business rules

As an example, think about how to go about implementing business rules. If there is one thing that software development has taught me it’s that the people who dream up rules for calculating prices have no end to their imagination. Say that you have built a system for shipping. There’s a fixed cost onto which a variable cost per mile is added to give the total shipping cost.

Fair enough, given the information you make very simple function to get the price. Then, as things are bound to do, a new rule enters. If you ship over a certain distance, you pay only 75% of the fixed cost. Fine, a simple if-statement will fix that.

Then the next change request. If you ship to a certain destination the variable price is lower. And if you ship more than 10 items you get a discount. If you book the shipping early it’s cheaper, if you book it late it’s more expensive.

Can you see where this is headed? Because of the permutation effects, every new case you add to this sort of computations will have to work together with every other case you have in your code, and still produce correct results.

Prolog

This is a case where logic directed programming should shine. And there exists to my knowledge only one logic based language, Prolog.

I’ve been meaning to explore this further, but it appears that Prolog is about as out of style as you can get. There seems to have been some hype during the fifth generation computer project in the eighties but since that failed horribly it seems to have taken down Prolog with it.

Also, Prolog is a decidedly comp-sciency language and quite mathematical where as far as I can tell the applications of the paradigms are in the realm of the very practical. This gap doesn’t exactly help.

Is there anyone using it? Or is there another way to express rule logic based programming in a declarative style? It would be sort of cool to have a generic rule based engine available since these sorts of problems are so common.

Advertisements
Tagged ,

One of the coolest books I have ever read

If you’re even a tiny bit like me, you want to know how things work. And I don’t mean in the sense of “I know how a car works because I can drive it”. No, I mean in the sense that you know how the car works because you understand the internal combustion engine, the clutch and the differential axle – at least on a conceptual level. I’m also going to assume you sort of like computers, at least you’re reading this using one aren’t you. Have you understood how they work on a conceptual level?

This book is about that, and boy does it do a good job. The book in question carries the unassuming name of The Elements of Computing Systems and looks about as boring as it possibly could. But the contents are a whole different matter. Reading the book and doing the exercises will have you building a computer from the ground up.

The book starts by explaining what a logical gate is and how to construct other gates from a given gate. It has you building all the necessary gates out of NAND gates. It goes ever more complex, you build multiplexers, memory, a full adder, an ALU, a CPU – all with quite excellent tools that the book has freely available on it’s companion site.

It doesn’t stop there, once the computer is done it proceeds with writing your own very simple assembler for your home-made assembly language. In which you write a simple compiler for your home-made assembly on your home made computer. In which you carry on writing a very simple operating system for your computer. If that doesn’t blow ones mind away, what will?

The thing about this book is that it will never involve anything in the realm of electrical engineering. There are no talks of transistors and other stuff, which isn’t really necessary either to grasp the concepts. Reading this book is a truly eye-opening experience, and you simply won’t look at a computer the same way again.

Oh, and did I mention that most of the book actually is quite freely available from it’s companion site?

If this isn’t enough to convince you of how awesome this book is, the things inside it will enable you to build this sort of crazy stuff:

Tagged , ,