I'm not an English native, so if there are any mistakes, please correct me.
I have written a program that takes an arithmetic expression and reduces it. It takes a list and returns a list like this one: '((a + 2) * b)
.
The user can only use addition and multiplication.
I'm trying to create a GUI that will take the list, reduce it and return the result. But the text field only returns a string value. I want to convert the string value in a list like the precedent example.
If the user writes (a + (b * 1))
, how do I convert it into a list (a + (b * 1))
?
Here is what I have done.
(define (convert string)
(map string->symbol ; convert each substring into a symbol
(string-split string))) ; split the string by its spaces
; Crée une fenêtre "Simplifier"
(define frame (new frame% [label "Simplifier"]))
; Crée un conteneur "panel"
(define panel (new vertical-panel% [parent frame]))
; Crée un message dans le conteneur "panel)
(define msg (new message%
[parent panel]
[label "Donnez une expression à simplifier.
N'oubliez pas les parenthèses!"]))
; Crée un champ de texte
(define text-field (new text-field%
(label "Expression : ")
(parent panel)))
; Crée un message qui affichera le résultat
(define message (new message%
(parent panel)
(auto-resize #t)
(label " ")))
; Faire le bouton "Validez"
(new button% [parent panel]
[label "Validez"]
; Procédure Callback pour un clique sur le bouton "Validez":
[callback (lambda (button event)
(define text (simplifier (convert (send text-field get-value))))
(send message set-label (slist->string text)))])
; Affiche la fenêtre
(send frame show #t)
Here the result of convert:
> (convert "(a + b)")
'(|(a| + |b)|)
Can I have a suggestion? Thanks in advance.
sorry I didn't keep my promise to update. Here how I made it work.
; Convert a list in a list of string
(define list->slist (lambda (l)
(cond
((null? l) '())
((list? (car l)) (cons (list->slist (car l)) (list->slist (cdr l))))
((Cte? (car l)) (cons (number->string (car l)) (list->slist (cdr l))))
(#t (cons (symbol->string (car l)) (list->slist (cdr l))))
)))
; Convert a list of string in string
(define convert (lambda (slst)
(cond
((null? slst) "")
((list? (car slst)) (string-append "(" (convert (car slst)) " " (convert (cdr slst)) ")"))
(#t (string-append (car slst) " " (convert (cdr slst))))
)))
It works for me. I can't see any simpler way. Thanks for the help.