Functional programmers write equations more and then write diagrams. The game is called equational reasoning, and basically it includes
The idea is that you write really simple code that is “clearly correct”, then you use equational reasoning to turn it into something cleaner and / or will work better. The master of this art is an Oxford professor named Richard Byrd.
For example, if I want to simplify a schema expression
(append (list x) l)
I will receive equal equals for equals, like crazy. Using the definition of list , I get
(append (cons x '()) l)
Giving the body of the add, I
(if (null? (cons x '())) l (cons (car (cons x '())) (append (cdr (cons x '())) l)))
Now I have these algebraic laws:
(null? (cons ab)) ==
and substituting equalities for equals, I get
(if
With a different law (if #f e1 e2) == e2 I get
(cons x (append '() l))
And if I use the append definition again, I get
(cons xl)
which I proved equal
(append (list x) l)
Norman ramsey
source share