;; Unparse numerical formula with full parentheses
;;
;; Mike O'Donnell
;; 5 October 2003; revised 8 October 2003

;; stringlist->string: stringlist -> string
;;
;; (stringlist->string strlst) appends together the strings in the list
;; strlst from left to right.

(define (stringlist->string strlst)
  (cond ((empty? strlst) "")
        (else            (string-append
                          (first strlst)
                          (stringlist->string (rest strlst))
                          )
                         )
        )
  )

;; formula->full_paren_token_list: formula -> list of strings
;;
;; (formula->full_paren_token_list fmla) unparses a formula into
;; a fully parenthesized list of tokens, in a notation given by
;; the standard grammar:
;;
;; <fmla> ::= ( <fmla> + <fmla> )
;;         |  ( <fmla> - <fmla> )
;;         |  ( <fmla> * <fmla> )
;;         |  ( <fmla> / <fmla> )
;;         |  ( <fmla> ^ <fmla> )
;;         |  fib ( <fmla> , <fmla> , <fmla> )
;;         |  <number>
;;
;; formula->token_list is an inverse to token_list->formula, which we will
;; define next week.

(define (formula->full_paren_token_list fmla)
  
  (cond ((number? fmla)               
         (list (number->string fmla)))
        
        ((and (list? fmla) (symbol=? (first fmla) '+))  
         (append (list "(")
                 (formula->full_paren_token_list (second fmla))
                 (list "+")
                 (formula->full_paren_token_list (third  fmla))
                 (list ")")
                 )
         )
        
        ((and (list? fmla) (symbol=? (first fmla) '-))   
         (append (list "(")
                 (formula->full_paren_token_list (second fmla))
                 (list "-")
                 (formula->full_paren_token_list (third  fmla))
                 (list ")")
                 )
         )
        
        ((and (list? fmla) (symbol=? (first fmla) '*)) 
         (append (list "(")
                 (formula->full_paren_token_list (second fmla))
                 (list "*")
                 (formula->full_paren_token_list (third  fmla))
                 (list ")")
                 )
         )
        
        ((and (list? fmla) (symbol=? (first fmla) '/))   
         (append (list "(")
                 (formula->full_paren_token_list (second  fmla))
                 (list "/")
                 (formula->full_paren_token_list (third   fmla))
                 (list ")")
                 )
         )
        
        ((and (list? fmla) (symbol=? (first fmla) 'expt)) 
         (append (list "(")
                 (formula->full_paren_token_list (second fmla))
                 (list "^")
                 (formula->full_paren_token_list (third  fmla))
                 (list")")
                 )
         )

        ((and (list? fmla) (symbol=? (first fmla) 'fib)) 
         (append (list "fib")
                 (list "(")
                 (formula->full_paren_token_list (second  fmla))
                 (list ",")
                 (formula->full_paren_token_list (third   fmla))
                 (list ",")
                 (formula->full_paren_token_list (fourth  fmla))
                 (list")")
                 )
         )

        )
  
  )
