How to resolve the algorithm Arithmetic evaluation step by step in the PicoLisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic evaluation step by step in the PicoLisp programming language
Table of Contents
Problem Statement
For those who don't remember, mathematical precedence is as follows:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Arithmetic evaluation step by step in the PicoLisp programming language
Source code in the picolisp programming language
(de ast (Str)
(let *L (str Str "")
(aggregate) ) )
(de aggregate ()
(let X (product)
(while (member (car *L) '("+" "-"))
(setq X (list (intern (pop '*L)) X (product))) )
X ) )
(de product ()
(let X (term)
(while (member (car *L) '("*" "/"))
(setq X (list (intern (pop '*L)) X (term))) )
X ) )
(de term ()
(let X (pop '*L)
(cond
((num? X) X)
((= "+" X) (term))
((= "-" X) (list '- (term)))
((= "(" X) (prog1 (aggregate) (pop '*L)))) ) )
: (ast "1+2+3*-4/(1+2)")
-> (+ (+ 1 2) (/ (* 3 (- 4)) (+ 1 2)))
: (ast "(1+2+3)*-4/(1+2)")
-> (/ (* (+ (+ 1 2) 3) (- 4)) (+ 1 2))
You may also check:How to resolve the algorithm Percolation/Bond percolation step by step in the Haskell programming language
You may also check:How to resolve the algorithm Universal Turing machine step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Weird numbers step by step in the C++ programming language
You may also check:How to resolve the algorithm Weird numbers step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Animate a pendulum step by step in the Common Lisp programming language