3 ms·
We've all written code like this: class Foo method bar(x) switch x case even? case odd? This is closed. What if I want to
by swannodette 15y ago
We've all written code like this:
class Foo
method bar(x)
switch x
case even?
case odd?
This is closed. What if I want to add a new case without modifying this class? I could subclass but then I have to know a lot about how Foo works. Perhaps bars has assumptions about whether it needs to run before or after I run my subclassed version, and on and on. This kind of nastiness pervades too much code.
method bar Foo even?(x)
method bar Foo odd?(x)
...
As far as logic programming - I think the paradigm is so little known that people fail to see how useful it is. Yet many non-trivial programs use ... relational databases!
How would you even begin to express the following idea in JavaScript?
http://stackoverflow.com/questions/6713424/how-do-i-express-this-logic/6719627#6719627 http://stackoverflow.com/questions/6713424/how-do-i-express-...
Note that the idea is actually quite simple and useful!
Another example is to produce all words that form a specific telephone number:
https://gist.github.com/1107653 https://gist.github.com/1107653
Of particular interest is the encode function, the relevant bit:
(run* [q]
(exist [x y xo yo]
(appendo x y s) ;; infer all possible lists that could concat
;; to form s
(wordo x xo) (wordo y yo) ;; convert to words
(project [xo yo]
(== q (apply str xo yo)))) ;; create the combined string
Note I don't have to mention indices, for loops, list comprehensions or any other iterative machinery.
All the other solutions I saw had a lot of boilerplate to express this simple idea of inferring the lists that could be concatenated to form s.