将文本字段中的列表从字符串转换为列表。

huangapple go评论48阅读模式
英文:

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&#39;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: `&#39;((a + 2) * b)`.
The user can only use addition and multiplication.

I&#39;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 &quot;&#39;&quot; string) ) ) ) )

This is the behaviour. You can get each element of the list

&gt; (display (convert &quot;((a + 2) * b)&quot; ) ) (display &quot;\n&quot;)
((a + 2) * b)
&gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 0 ) ) (display &quot;\n&quot;)
(a + 2)
&gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 1 ) ) (display &quot;\n&quot;)
*
&gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 2 ) ) (display &quot;\n&quot;)
b
&gt; 

Try it from your interface replacing your button with this code:

; Faire le bouton &quot;Validez&quot;
(new button% [parent panel]
             [label &quot;Validez&quot;]
             ; Proc&#233;dure Callback pour un clique sur le bouton &quot;Validez&quot;:
             [callback (lambda (button event)
                         ;(define text (simplifier (convert (send text-field get-value))))
                         ;(send message set-label (slist-&gt;string text)))])
                         (display (convert (send text-field get-value) ) )
                         (display &quot;\n&quot;)
                         (display (list-ref (convert (send text-field get-value) ) 0 ) )
                         (display &quot;\n&quot;)
                         (display (list-ref (convert (send text-field get-value) ) 1 ) )
                         (display &quot;\n&quot;)
                         (display (list-ref (convert (send text-field get-value) ) 2 ) )
                         (display &quot;\n&quot;)
             )])

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 &quot;&#39;&quot; string) ) ) ) )

This is the behaviour. You can get each element of the list

	&gt; (display (convert &quot;((a + 2) * b)&quot; ) ) (display &quot;\n&quot;)
	((a + 2) * b)
	&gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 0 ) ) (display &quot;\n&quot;)
	(a + 2)
	&gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 1 ) ) (display &quot;\n&quot;)
	*
	&gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 2 ) ) (display &quot;\n&quot;)
	b
	&gt; 

Try it from your interface replacing your button with this code:

	; Faire le bouton &quot;Validez&quot;
	(new button% [parent panel]
				 [label &quot;Validez&quot;]
				 ; Proc&#233;dure Callback pour un clique sur le bouton &quot;Validez&quot;:
				 [callback (lambda (button event)
							 ;(define text (simplifier (convert (send text-field get-value))))
							 ;(send message set-label (slist-&gt;string text)))])
							 (display (convert (send text-field get-value) ) )
							 (display &quot;\n&quot;)
							 (display (list-ref (convert (send text-field get-value) ) 0 ) )
							 (display &quot;\n&quot;)
							 (display (list-ref (convert (send text-field get-value) ) 1 ) )
							 (display &quot;\n&quot;)
							 (display (list-ref (convert (send text-field get-value) ) 2 ) )
							 (display &quot;\n&quot;)
				  )])

and writing this expression in your interface&#39;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&#39;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&#39;t see any simpler way.
Thanks for the help.



</details>



huangapple
  • 本文由 发表于 2023年5月14日 02:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定