Home > Archive > Prolog > December 2007 > Calculator on list
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Calculator on list
|
|
|
| How can I buid calculator on list for example [1, *, 2, +, 8], result
is N = 1 * 2 + 8 = 10.
Now I wrote this code:
wynik([W],W).
wynik([E,D|T], W) :-
wynik(T, W2),
W =.. [D,E,W2].
but it doesn't count a result like N = 10 but N = 1 * 2 + 8.
Help me with this problem.
| |
| winterer@email.com 2007-11-28, 4:25 am |
| With your code, you only construct an expression, but do not evaluate
it.
Of course, you should use a wrapper predicate for that, to make life
easier. (think 1+2*8 vs 1*2+8)
Good luck with you homework.
| |
| Chip Eastham 2007-11-28, 7:10 pm |
| On Nov 27, 5:26 pm, kabum <r.kabul...@gmail.com> wrote:
> How can I buid calculator on list for example [1, *, 2, +, 8], result
> is N = 1 * 2 + 8 = 10.
> Now I wrote this code:
>
> wynik([W],W).
>
> wynik([E,D|T], W) :-
>
> wynik(T, W2),
> W =.. [D,E,W2].
>
> but it doesn't count a result like N = 10 but N = 1 * 2 + 8.
>
> Help me with this problem.
A simple improvement you can try with your "calculator
on list" predicate is to add as a subgoal X is W.
is/2 will attempt to evaluate the expression W.
Of course a great deal more logic may be
needed to make the "parsing" of your calculator
handle more complicated expressions.
regards, chip
| |
| Chip Eastham 2007-11-28, 7:10 pm |
| On Nov 28, 10:51 am, Chip Eastham <hardm...@gmail.com> wrote:
> On Nov 27, 5:26 pm, kabum <r.kabul...@gmail.com> wrote:
>
>
>
>
>
>
>
> A simple improvement you can try with your "calculator
> on list" predicate is to add as a subgoal X is W.
>
> is/2 will attempt to evaluate the expression W.
>
> Of course a great deal more logic may be
> needed to make the "parsing" of your calculator
> handle more complicated expressions.
>
> regards, chip
Here's how your code, with the added subgoal, works
with Amzi! Prolog:
?- wynik([1,*,2,+,8],W), X is W.
W = 1*(2 + 8)
X = 10
yes
| |
|
| It's good idea but I just start programming in prolog and I haven't
got a lot of experience but I learn very quickly :-)
Thanks for help!!!
| |
|
| I'm trying change my code to not div by zero but I'cant do it. And I
have other problem.
When I put on the list
[2,+,3,*,6] then
W = 2+3*6
X = 20 and this is OK,
but when I put on the list
[2,*,3,+,6] then
W = 2*(3+6)
X = 18 and this is bad because X = 12.
I don't know how do it to work good...
| |
| Chip Eastham 2007-12-03, 4:34 am |
| On Dec 2, 3:00 pm, kabum <r.kabul...@gmail.com> wrote:
> I'm trying change my code to not div by zero but I'cant do it. And I
> have other problem.
> When I put on the list
>
> [2,+,3,*,6] then
> W = 2+3*6
> X = 20 and this is OK,
>
> but when I put on the list
> [2,*,3,+,6] then
> W = 2*(3+6)
> X = 18 and this is bad because X = 12.
>
> I don't know how do it to work good...
Part of the challenge is to clearly define what "work good"
should mean in your context.
If the operations and operands were arranged in reverse
Polish notation, then they would naturally form a stack
that could be evaluated recursively.
So one might approach your circumstance with an idea of
converting the "infix" operator notation that you have
employed in your examples to "prefix" (RPN) forms. In
effect you want [2,+,3,*,6] to become [+,2,*,3,6], but
[2,*,3,+,6] to become [+,*,2,3,6].
For a more general perspective on parsing with operator
precedence considerations, you might start with this
Wikipedia article and let it suggest to you additional
background knowledge that you might need to fill in:
[Operator-precedence parsing]
http://en.wikipedia.org/wiki/Operator-precedence_parser
regards, chip
| |
|
| I don't know how do it... It's to complicated for me!
| |
|
| for me... my program works good, when I have: [2,*,3,+,6]
First it should do: "2*3" , than "...+6"
just like in math, operation with higher priority ( such as "*" or
"/")
than lower priority: "+"or "-"
if we have [2,+,3,*,6]
it should and it returns 20
because it first multiply 3 and 6 (higher priority - 3*6)
than it add 2 so it counts well !!
Have I made it clearllier ??
As I included earlier i'm a rookie, so i'd be very greatful if Anybody
could help me !!
| |
|
| On Mon, 3 Dec 2007 14:18:28 -0800 (PST), kabum
<r.kabulski@gmail.com> wrote:
>for me... my program works good, when I have: [2,*,3,+,6]
>First it should do: "2*3" , than "...+6"
>just like in math, operation with higher priority ( such as "*" or
>"/")
>than lower priority: "+"or "-"
>
>if we have [2,+,3,*,6]
>it should and it returns 20
>because it first multiply 3 and 6 (higher priority - 3*6)
>than it add 2 so it counts well !!
>
>Have I made it clearllier ??
>
>As I included earlier i'm a rookie, so i'd be very greatful if Anybody
>could help me !!
Couldn't you ask your professor for help?... Even if this is a
homework (that you should do yourself) he could provide some
insight.
By teh way, problem is not well defined. What it means
"calculator"?.. How to calculate and what?... Consider priorities
like in "normal" arithmetics, or do calculations without priorities
(Smalltalk does this). Or this is RPN?...
A.L.
| |
|
| Oh right! Now I try write my program and when I get finish code I
return to help!
Thanks
| |
| Chip Eastham 2007-12-04, 8:08 am |
| On Dec 3, 5:18 pm, kabum <r.kabul...@gmail.com> wrote:
> for me... my program works good, when I have: [2,*,3,+,6]
> First it should do: "2*3" , than "...+6"
> just like in math, operation with higher priority ( such as "*" or
> "/")
> than lower priority: "+"or "-"
>
> if we have [2,+,3,*,6]
> it should and it returns 20
> because it first multiply 3 and 6 (higher priority - 3*6)
> than it add 2 so it counts well !!
>
> Have I made it clearllier ??
>
> As I included earlier i'm a rookie, so i'd be very greatful if Anybody
> could help me !!
Do you plan to allow parentheses and/or brackets?
You will need to manage the evaluations so that
they are performed within any matching pairs of
parentheses or brackets before doing those
outside.
Other operations that you may want to consider
at some point are exponentiation and unary minus
(negation).
Without those additional elements, you should do
the multiplications and/or divisions from left
to right, then additions and/or subtractions
from left to right.
regards, chip
| |
|
| On Tue, 4 Dec 2007 00:52:38 -0800 (PST), kabum
<r.kabulski@gmail.com> wrote:
>Oh right! Now I try write my program and when I get finish code I
>return to help!
>
>Thanks
Quote myselfL:
"By the way, problem is not well defined. What it means
"calculator"?.. How to calculate and what?... Consider priorities
like in "normal" arithmetics, or do calculations without priorities
(Smalltalk does this). Or this is RPN?..."...
What do you want to program?...
A.L.
|
|
|
|
|