英文:
How do I convert a list written on a text field from lstring to list?
问题
I have translated the code and provided the relevant parts. Here's the translation:
我不是英语母语者,所以如果有任何错误,请纠正我。
我编写了一个程序,它接受一个算术表达式并对其进行简化。它接受一个列表并返回一个类似于这个的列表:`'((a + 2) * b)`。
用户只能使用加法和乘法。
我正在尝试创建一个GUI,它将接受列表,对其进行简化并返回结果。但是文本字段只返回一个字符串值。我想将字符串值转换为类似于前面示例的列表。
如果用户写入`(a + (b * 1))`,我应该如何将其转换为列表`(a + (b * 1))`?
以下是我已经完成的部分。
(define (convert string)
(map string->symbol ; 将每个子字符串转换为符号
(string-split string))) ; 通过空格拆分字符串
; 创建一个名为“Simplifier”的窗口
(define frame (new frame% [label "Simplifier"]))
; 创建一个名为“panel”的容器
(define panel (new vertical-panel% [parent frame]))
; 在“panel”容器中创建一条消息
(define msg (new message%
[parent panel]
[label "Give an expression to simplify. Remember the parentheses!"]))
; 创建一个文本字段
(define text-field (new text-field%
(label "Expression : ")
(parent panel)))
; 创建一个将显示结果的消息
(define message (new message%
[parent panel]
[auto-resize #t]
[label " "]))
; 创建一个名为“Validez”的按钮
(new button% [parent panel]
[label "Validez"]
; 单击“Validez”按钮时的回调过程:
[callback (lambda (button event)
(define text (simplifier (convert (send text-field get-value))))
(send message set-label (slist->string text)))])
; 显示窗口
(send frame show #t)
以下是convert函数的结果:
> (convert "(a + b)")
'(|(a| + |b)|)
是否可以提供建议?
提前感谢您。
<details>
<summary>英文:</summary>
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.
</details>
# 答案1
**得分**: 0
I hope this code helps you
(define (convert string) (eval (read ( open-input-string (string-append "'" string) ) ) ) )
This is the behaviour. You can get each element of the list
> (display (convert "((a + 2) * b)" ) ) (display "\n")
((a + 2) * b)
> (display (list-ref (convert "((a + 2) * b)" ) 0 ) ) (display "\n")
(a + 2)
> (display (list-ref (convert "((a + 2) * b)" ) 1 ) ) (display "\n")
*
> (display (list-ref (convert "((a + 2) * b)" ) 2 ) ) (display "\n")
b
>
Try it from your interface replacing your button with this code:
; 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)))])
(display (convert (send text-field get-value) ) )
(display "\n")
(display (list-ref (convert (send text-field get-value) ) 0 ) )
(display "\n")
(display (list-ref (convert (send text-field get-value) ) 1 ) )
(display "\n")
(display (list-ref (convert (send text-field get-value) ) 2 ) )
(display "\n")
)])
and writing this expression in your interface's textfield
((a + 2) * b)
the result should be:
((a + 2) * b)
(a + 2)
*
b
Hope this helps!
<details>
<summary>英文:</summary>
I hope this code helps you
(define (convert string) (eval (read ( open-input-string (string-append "'" string) ) ) ) )
This is the behaviour. You can get each element of the list
> (display (convert "((a + 2) * b)" ) ) (display "\n")
((a + 2) * b)
> (display (list-ref (convert "((a + 2) * b)" ) 0 ) ) (display "\n")
(a + 2)
> (display (list-ref (convert "((a + 2) * b)" ) 1 ) ) (display "\n")
*
> (display (list-ref (convert "((a + 2) * b)" ) 2 ) ) (display "\n")
b
>
Try it from your interface replacing your button with this code:
; 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)))])
(display (convert (send text-field get-value) ) )
(display "\n")
(display (list-ref (convert (send text-field get-value) ) 0 ) )
(display "\n")
(display (list-ref (convert (send text-field get-value) ) 1 ) )
(display "\n")
(display (list-ref (convert (send text-field get-value) ) 2 ) )
(display "\n")
)])
and writing this expression in your interface's textfield
((a + 2) * b)
the result should be:
((a + 2) * b)
(a + 2)
*
b
Hope this helps!
</details>
# 答案2
**得分**: 0
抱歉,我将仅为您提供代码的翻译部分:
; 转换列表为字符串列表
(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))))
)))
; 将字符串列表转换为字符串
(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))))
)))
希望这有助于您。
<details>
<summary>英文:</summary>
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.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论