reset password
Author Message
rabbott
Posts: 1649
Posted 21:02 Mar 06, 2019 |

The code.

Turns out that if you eliminate all blank lines, all comments, and all trace lines, (none of which I'm recommending) the Python and Haskel files have approximately the same number of lines of executable code.

Last edited by rabbott at 21:15 Mar 06, 2019.
rabbott
Posts: 1649
Posted 21:36 Mar 06, 2019 |

An interesting feature of the Python code is that it uses metaclasses. A metaclass is to a class as a class is to an object. 

The Python code uses classes rather than labels for the roads: A, B, C. A road segment of length n on road A is A(n), i.e., an instance of Road A. But the roads themselves are often treated as objects, e.g., when one wants to define their __str__() methods. The way to do that is to define such methods in the Metaclass.

The Haskell code uses a number of user-created operators. We can't do that in Python, but we can repurpose operators.  The function to join two Paths into a single combined Path is given to the (+) operator. Doing that required defining the __add__() method in the Path class. The (+) operator is used to join two paths in the bestPath method. If p1 and p2 are Paths, p1 + p2 is the combined path. The expression p1 + p2 is executed as p1.__add__(p2).

You can do the same thing with numbers: 

>>> (1).__add__(2)
3

(Leaving out the parentheses around the 1 produces a syntax error.)

Similarly,

>>> (1+2).__add__(3)
6

As the Python documentation says, everything is an object, even numbers.

 

Last edited by rabbott at 21:56 Mar 06, 2019.