61 points · by andrzejsz · 11y ago · 70 comments
That being said, it's still fast enough for this example.
Also, Python solution along the same lines:
import itertoolsdef
def valueOf(*values, base=10):
v = 0
for v in itertools.accumulate(values, lambda a,b: a*base+b):
pass
return v
def solve():
for s,e,n,d,m,o,r,y in itertools.permutations(range(10), r=8):
if valueOf(s,e,n,d) + valueOf(m,o,r,e) == valueOf(m,o,n,e,y):
yield (s,e,n,d),(m,o,r,e),(m,o,n,e,y)
(If anyone knows of a better way to do valueOf, let me know)That's what I've noticed with Haskellers. They have a tendency to write really slow code and don't have a clear idea of the execution model behind what they write down.
A while ago I asked in #haskell for translations into Haskell of the following C++ code:
This should be easy. The algorithm is right in front of you. There should not be a lot of thinking in transcribing an algorithm into Haskell. But most of them wrote down something with a fold which turned what is an O(m*n) algorithm in time and O(n) in space into something like O(m^n) in space and time. The only one who got close to the C++ implementation was one who used a lot of State monads.
Then, of course, there was someone who pointed me out to a radically different algorithm written in Haskell that I think is only O(n+m) in both space and time. I haven't been able to understand this other algorithm yet.
Certain highly stateful algorithms may be difficult to translate into Haskell, in the same way that certain highly structural or lazy algorithms may be difficult to translate to C++.
I'm curious if your time complexity analyses are accurate, given that you (admittedly) did not understand the faster algorithm. You could be getting tripped up by laziness.
In general, I've found that most production Haskell code is quite fast, in both big-O and absolute terms.
Of course, this isn’t unique to Haskellers. ;)
Granted, reasoning about performance in Haskell is still a relatively young art, and there aren’t many of us who are experienced with it.
http://en.wikipedia.org/wiki/Edit_distance and http://blog.notdot.net/2010/07/Damn-Cool-Algorithms-Levensht...
You can be sure if you read the generated Core and/or assembly code.
But that's not the point. The point is that Haskellers have a hard time translating an algorithm that's right in front of them and instead of writing down an O(m*n) algorithm they instinctively do O(m^n). This whole "many-worlds" approach of the original article is also leading to a very inefficient algorithm.
The abstractions Haskellers prefer lead them towards slow code.
For details, look at the first code block in the "The Client Side" section, here: http://bartoszmilewski.com/2015/05/18/using-monads-in-c-to-s...
However, this doesn't mean we have to punt the easy cases just because there are hard cases.
That no one seems to have produced (or pointed at) an asymptotically better solution in any language, I think that's some small evidence that there isn't such a solution and substantially more evidence that it's not easy to find.
import numpy as np
def valueOf(*v):
return sum( v * np.power(10,range(len(v)))[::-1] )
> valueOf( 2,3,4 ) # => 234
Also, in python 2.7, I don't think you can put a named variable proceeding a wildcard? At least, that doesn't work for me, so I left it out.To put it simply, I don't see an improvement with that version.
And Python 3 has some improvements w.r.t. wildcard arguments.
import itertools as it
a = 'dog'
b = 'food'
c = 'tasty'
letters = sorted(set(a+b+c))
for combo in it.permutations('0123456789', len(letters)):
mapping = dict(zip(letters, combo))
if len(c) > max(len(a), len(b)) and mapping[c[0]] is '0':
continue
f = lambda string: int(''.join(map(mapping.get, string)))
n1, n2, n3 = map(f, [a, b, c])
if n1 + n2 == n3:
print( mapping )
print( n1, n2, n3 )
break
seems haskell friendly given all the mappings, but not in the way that the blogpost did it. but also not with that big nested loop either.Why would you write an algorithm in C++ if the runtime will be bad?
ETA since he mentions the implementation is a translation of this Haskell implementation: http://blog.jle.im/entry/unique-sample-drawing-searches-with... presumably the uniqueness is done by updating the list of remaining possibilities for selection each time a new value is selected.
from functools import reduce
def valueOf(*values, base=10):
return reduce(lambda x,y: x*base+y, values)That being said, that's much cleaner. Thanks!
"In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data." -- https://en.wikipedia.org/wiki/Functional_programming
So the answer to the question "Who said you can't have state in functional programming?" is: "Every introduction to functional programming ever."
This is why people hate Haskell and make fun of Haskellers. In their quest to appear more clever than the rest of us, they engage in utterly disingenous rhetoric. Which is too bad, because I'm pretty sure they are actually more clever than the rest of us--certainly more clever than me--and don't need this nonsense.
Haskell is an amazingly cool language, and I've talked to people at meetups who are using it for interesting things, and learning a bit of it myself has been an interesting and educational challenge. But getting around to learning it took five years longer than it should have because I was so put off by the rhetorical nonsense the language community engages in.
5 years?