algorithms

Failing a Generator on First Failure

There are occasions when working with some generative predicate where you don't know when you need to quit the generation until some condition fails. Trouble is, you'll fail with no chance of future success but you're now lost in an infinite loop as the generator keeps on trying. In this post we'll work through a blog pagination example and show how to use a dirty little trick with a cut.

Basic Idea Of Recursion

Want to do something a bunch of times in Prolog? Recursion is how we do it. In this post we'll look at the most basic ideas of recursion and talk through a couple of examples.

Prolog: Forwards and Backwards

A key idiom in Prolog is the idea that it runs both forwards and backwards. If you've never programmed in Prolog before, this can be mind-blowing! You can write one "function" and get many different uses out of it. In this post we'll take a look at what this means as a teaser that might tempt you into giving Prolog a go.

Functional Prolog: Map, Filter and Reduce

Prolog programs have both logical and procedural meanings. In this post we'll take a look at procedural ideas more commonly associated with functional programming than Prolog, namely: map, filter and reduce (foldl and foldr). We'll code them and then query them.

Reflexive, Symmetric and Transitive Relations in Prolog

When we start doing knowledge representation in Prolog, we start needing to describe the properties of relations so we can infer more than is in our recorded data. Symmetry, reflexivity and transitivity are the three main relationship properties you'll end up using. In this interactive post we take a look at how they can be encoded.

Getting JSON data from an API in SWI-Prolog

Some folks don’t make their data available through RDF formats, or nice SPARQL endpoints, instead they provide a (REST/RESTFUL) API and will return JSON data for your request. It can be a little tricky figuring out how to get this data into your SWI-Prolog program. So in this post I demonstrate with a simple example.

Functional Iteration: Better than Pythonic

Make use of functional programming techniques to improve your iteration in Python. By abstracting the function from the iteration you’ll have greater code reuse, easier parallelisation, and lower memory usage.