summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo <hugo.hornquist@gmail.com>2016-04-27 16:47:59 +0200
committerHugo <hugo.hornquist@gmail.com>2016-04-27 16:47:59 +0200
commita41bb4868ba6090904052dd1aa3ad36df6b2b386 (patch)
treef181aaede5635fe4a4cf43d50cc08d2003051c6c
parentadded '^' as one of the operations in 'full-parser' (diff)
downloadmath-parse-a41bb4868ba6090904052dd1aa3ad36df6b2b386.tar.gz
math-parse-a41bb4868ba6090904052dd1aa3ad36df6b2b386.tar.xz
full-parse now has some comments, moved a function
-rw-r--r--full-parse.rkt21
1 files changed, 15 insertions, 6 deletions
diff --git a/full-parse.rkt b/full-parse.rkt
index 9b0210e..df1118c 100644
--- a/full-parse.rkt
+++ b/full-parse.rkt
@@ -2,21 +2,20 @@
(provide full-parse)
+; parses a function written in mathematical notation to lisp notation
+; currently handles (+ - * / ^). It does respect the order of operations
(define (full-parse str)
(define (get-general expr operator next-operation)
- ; used to add a completed part of the operation to the list of operations
- ; 5 is one of those in the addition iteration
- ; 3 and x are in the product iteration
+ ; adds a operation to the list of operations
+ ; if it's a number or a single variable then it's just added
+ ; if it's an operation then it's evaluated
(define (add-operation-to-list operation seq)
(cons (if (= (length operation) 1)
(char->wanted (car operation))
(next-operation (reverse operation)))
seq))
- (define (contains value seq)
- (memq value seq))
-
(define (inner current-term other-terms remaining-expression)
(cond
[(null? remaining-expression)
@@ -30,15 +29,23 @@
other-terms
(cdr remaining-expression))]))
+
(if (not (contains operator expr))
(next-operation expr)
(cons (char->wanted operator) (reverse (inner '() '() expr)))))
+ (define (contains value seq)
+ (memq value seq))
+
+ ; char->number if numerical
+ ; char->symbol otherwise
(define (char->wanted c)
((if (char-numeric? c)
string->number string->symbol)
(string c)))
+ ; creates a fucntion for the current operator,
+ ; with a function call for the next operatior as a parameter
(define (create-trace expr ops)
(lambda (expr)
;(display expr) (newline)
@@ -47,5 +54,7 @@
(lambda (x) x)
(create-trace expr (cdr ops))))))
+ ; start the function, with the operations
+ ; in order, from outermost to innermost.
(let ((expr (string->list str)))
((create-trace expr '(#\+ #\- #\* #\/ #\^)) expr)))