On Sun, Nov 18, 2012 at 2:04 PM, Stefan Klinger
all-li...@stefan-klinger.de wrote:
Sounds like you would want to enumerate all possible *abstract* syntax
trees, these implicitly have exactly the necessary parentheses. I'd do
this recursively, splitting the sequence of numbers in two at all
Hi,
You can make a datatype that captures exactly the expressions you want (see
code below). Essentially you want to make sure that a subtraction or
addition never has another subtraction or addition as its left operand.
I would also like to advertise a bit and show how this type can be
Jonas Almström Duregård jonas.dureg...@chalmers.se писал(а) в своём
письме Mon, 19 Nov 2012 01:31:01 +0300:
Hi,
You can make a datatype that captures exactly the expressions you want
(see
code below). Essentially you want to make sure that a subtraction or
addition never has another
Well, at least this is a nice exercise!
I'm assuming that all operators associate to the left, and use the usual
precedence rules.
My approach would consider (1+2)+3 different from 1+(2+3), and enumerate
it again. Of course they are different computations, though
mathematically equivalent.
This smells like homework to me,
which isn't a bad thing,
it will however change the way I answer you.
Please look at http://hackage.haskell.org/packages/archive/base/latest/doc/
html/Data-List.html#v:permutations
and show us your attempts to use this function.
Timothy
-- Původní
It might be rare that a real world problem can be formulated as such
a simple mathematical challenge,
so I can't blame you for thinking about home work. I did too.
Actually it's part of a logic puzzle I'm implementing.
Attacking the problem textually, I can treat the list of infix
operators as
Instead of attacking the problem textually, try to create a datatype which
would describe your expressions, then generate all values of this
datatype, filter those you don’t need, and convert the rest into Strings.
Currently your expressions are represented by “String” — conversion is
very
Sorry! I replied without reading your message properly.
I could then work directly with parsing trees, and generate all binary
trees of fixed lengths.
But most of them would be unnecessary, so it seems like I'm attacking
it from the wrong angle.
They won’t be unnecessary if you generate them
The following algorithm generates all possible expressions and throws away
most
of unnecessary duplicates.
import qualified Data.Map as M
data Expr = Num Int
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
| Div Expr Expr
Rendering function is
Indentation messed up… I have pasted the code here: http://hpaste.org/77864
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
10 matches
Mail list logo