Live data from Hacker News

Using Monads in C++ to Solve Constraints: 1. The List Monad

bartoszmilewski.com

21–30 of 73 posts

Re: Using Monads in C++ to Solve Constraints: 1. The List Monad

#21
post #6

Earlier quoted context omitted.

Here's one possible implementation of valueOf: 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.

That is a) hauling in an awfully big external library for something that should be relatively trivial, b) inefficient (you're replacing a single multiplication by 10 and addition per digit with O(log x) multiplications and an addition per digit, assuming np.power uses a decent exponentiation algorithm), and c) if anything, less readable than my version. To put it simply, I don't see an improvement with that version.…

If you don't need other bases you can just string join them. : P

Re: Using Monads in C++ to Solve Constraints: 1. The List Monad

#23

This method is really inefficient. There are b! / (b - n)! ways to choose n of b unique numbers, whereas this solution is O(b^n). 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.permutatio…

    from functools import reduce
    def valueOf(*values, base=10):
        return reduce(lambda x,y: x*base+y, values)

Re: Using Monads in C++ to Solve Constraints: 1. The List Monad

#24
post #7

This method is really inefficient. There are b! / (b - n)! ways to choose n of b unique numbers, whereas this solution is O(b^n). 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.permutatio…

> This method is really inefficient. 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: http://codepad.org/sQTXhqC2 This should be easy. The algorithm is right in front of you. There should not be a lot of thinkin…

There is a set of simple patterns/recipes most people use in Haskell that corresponds to their kind of math intuition Haskell seems to support. It doesn't have anything to do with efficiency though, for that you need to be an expert and you still won't be 100% sure the compiler really optimizes the execution the way you intended.

Re: Using Monads in C++ to Solve Constraints: 1. The List Monad

#25
post #17

Earlier quoted context omitted.

The Haskell Wiki has what seems like a clean and efficient implementation: https://wiki.haskell.org/Edit_distance

Yeah, it's got a completely different algorithm along the diagonal of the matrix. That's the O(m+n) algorithm. I still haven't been able to understand it. 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…

    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.

Re: Using Monads in C++ to Solve Constraints: 1. The List Monad

#26
post #24
post #7

Earlier quoted context omitted.

> This method is really inefficient. 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: http://codepad.org/sQTXhqC2 This should be easy. The algorithm is right in front of you. There should not be a lot of thinkin…

There is a set of simple patterns/recipes most people use in Haskell that corresponds to their kind of math intuition Haskell seems to support. It doesn't have anything to do with efficiency though, for that you need to be an expert and you still won't be 100% sure the compiler really optimizes the execution the way you intended.

> you still won't be 100% sure the compiler really optimizes the execution the way you intended.

You can be sure if you read the generated Core and/or assembly code.

Re: Using Monads in C++ to Solve Constraints: 1. The List Monad

#27
post #24

Earlier quoted context omitted.

There is a set of simple patterns/recipes most people use in Haskell that corresponds to their kind of math intuition Haskell seems to support. It doesn't have anything to do with efficiency though, for that you need to be an expert and you still won't be 100% sure the compiler really optimizes the execution the way you intended.

> you still won't be 100% sure the compiler really optimizes the execution the way you intended. You can be sure if you read the generated Core and/or assembly code.

And if you're willing to do that on every upgrade of the compiler or anything that may affect the compiler's exact execution path.

Re: Using Monads in C++ to Solve Constraints: 1. The List Monad

#28
post #21

Earlier quoted context omitted.

That is a) hauling in an awfully big external library for something that should be relatively trivial, b) inefficient (you're replacing a single multiplication by 10 and addition per digit with O(log x) multiplications and an addition per digit, assuming np.power uses a decent exponentiation algorithm), and c) if anything, less readable than my version. To put it simply, I don't see an improvement with that version.…

If you don't need other bases you can just string join them. : P

That's actually how I started, and `int` has a base parameter.

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.

Re: Using Monads in C++ to Solve Constraints: 1. The List Monad

#29

Earlier quoted context omitted.

> you still won't be 100% sure the compiler really optimizes the execution the way you intended. You can be sure if you read the generated Core and/or assembly code.

And if you're willing to do that on every upgrade of the compiler or anything that may affect the compiler's exact execution path.

Performance regressions in GHC are taken seriously as far as I've seen. It's not that big of a deal since you should test/profile your program on any compiler upgrade.

If you see any regressions, just post it to the ghc mailing list and it will probably be solved for you since it's much needed regression feedback/data.

Re: Using Monads in C++ to Solve Constraints: 1. The List Monad

#30

It is disingenous in the extreme to ask, "So what’s all this talk about mutation and state? Well, who said you can’t have state in functional programming?" when the very first line of the very first hit on "functional programming mutation state" is the Wikipedia article on functional programming, which says: "In computer science, functional programming is a programming paradigm—a style of building the structure and e…

"Having state" != "Changing-state and mutable data".

5 years?

Post reply on HN