From a41bb4868ba6090904052dd1aa3ad36df6b2b386 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 27 Apr 2016 16:47:59 +0200 Subject: full-parse now has some comments, moved a function --- full-parse.rkt | 21 +++++++++++++++------ 1 file 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))) -- cgit v1.2.3