by TheLoneWolfling · 11y ago · 0 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.
Haskellers have a hard time translating an algorithm that's right in front of them
That's no surprise: it is an open problem if this is possible in general. Some time ago Pippenger [1] showed that some programs cannot be transliterated into pure strict functional languages without suffering a slowdown.
Later, Bird et al [2] show that the example given by Pippenger to show the slowdown really depends on strictness, and can be transliterated into a pure lazy functional language without asymptotic slowdown.As of May 2015, nobody knows if lazy functional language can solve all problems with the same asymptotic time complexity as stateful languages.
[1] N. Pippenger, Pure Versus Impure Lisp.
[2] R. Bird, G. Jones, O. De Moor, More haste, less speed: lazy versus eager evaluation.
Anyway, Haskell is perfectly decent at implementing your algorithm, and imperative algoritms in general. He's a more-or-less direct translation
http://lpaste.net/132519
There's extra noise related to wrapping and unwrapping mutable cells which I could tidy up if I had the time, otherwise it's essentially identical.In any case the author of the post in question, Bartosz Milewski, is an expert in C++ and not particularly an expert in Haskell, so your point seems misdirected.
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.
Although I don't know how one would go about proving or disproving if this is asymptotically better or just a (substantial) constant-factor speedup.
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.
But it's still relatively verbose, due to Python's whole "join-only-takes-strings" thing:
def valueOf(*values, base=10):
return int("".join(map(str, values)), base=10)
Not to mention it's doing a whole lot of unnecessary work. 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!
I'll also note that int() returns 0.