I'm not an English native, so if there are any mistakes, please correct me. I have written a program that take an arithmetic expression and reduce it. It takes a list and return a list like this one: '((a + 2) * b)
. The user can only use addition and multiplication.
I'm trying to create a GUI which will take the list, reduce it and return the result. But the text field only returns a string value. I want to convert the sting value in a list like the precedent example. If the user write (a + (b * 1))
, how do I convert it in 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 is the result of convert:
> (convert "(a + b)")
'(|(a| + |b)|)
Here is a modification of that I have done.
(new button% [parent panel]
[label "Validez"]
; Procédure Callback pour un clique sur le bouton "Validez":
[callback (lambda (button event)
(define text (simplifier (read (open-input-string (send text-field get-value)))))
(send message set-label (slist->string text)))])
It works when I test in the interpreter.
> (read (open-input-string "(a + (b + c))")) : '(a + (b + c))
'(a + (b + c))
But on the window, I write the same thing, and it returns this :
symbol→string: contract violation expected: symbol? Given: '(b + c)
Can I have a suggestion? Thanks in advance.