Lesson goal: Converting units

Previous: Simulating digital circuits (3) | Home | Next: Reasoning about points and lines (1)

You likely know about units conversion, as in "convert 4 feet into centimeters." This is a lot like the routing lesson, only instead of going from city A to B, we want to get from unit A to unit B.

To begin, the "data" we'll need for this will be conversion facts, like the fact that there's 12 inches in 1 foot, or 100 centimeters in 1 meter. Our plan is to simply give these facts to Prolog, then be able to ask it something like convert(foot,centimeter). With this, it should show us how to convert for example 4 ft into cm.

After the facts, the core conversion clause is called convert(To,From,Steps), which will work to show us how to convert a quantity in units of To into a quantity of From, putting the textual steps into Steps.

Now you try. Run the code as is, to convert inches into km. But defintely try your own conversions and add as many conversion facts as you need!

Type your code here:

See your results here: