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

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

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:

  1. 我不是英语母语者所以如果有任何错误请纠正我
  2. 我编写了一个程序它接受一个算术表达式并对其进行简化它接受一个列表并返回一个类似于这个的列表`'((a + 2) * b)`
  3. 用户只能使用加法和乘法
  4. 我正在尝试创建一个GUI它将接受列表对其进行简化并返回结果但是文本字段只返回一个字符串值我想将字符串值转换为类似于前面示例的列表
  5. 如果用户写入`(a + (b * 1))`我应该如何将其转换为列表`(a + (b * 1))`
  6. 以下是我已经完成的部分
  7. (define (convert string)
  8. (map string->symbol ; 将每个子字符串转换为符号
  9. (string-split string))) ; 通过空格拆分字符串
  10. ; 创建一个名为“Simplifier”的窗口
  11. (define frame (new frame% [label "Simplifier"]))
  12. ; 创建一个名为“panel”的容器
  13. (define panel (new vertical-panel% [parent frame]))
  14. ; 在“panel”容器中创建一条消息
  15. (define msg (new message%
  16. [parent panel]
  17. [label "Give an expression to simplify. Remember the parentheses!"]))
  18. ; 创建一个文本字段
  19. (define text-field (new text-field%
  20. (label "Expression : ")
  21. (parent panel)))
  22. ; 创建一个将显示结果的消息
  23. (define message (new message%
  24. [parent panel]
  25. [auto-resize #t]
  26. [label " "]))
  27. ; 创建一个名为“Validez”的按钮
  28. (new button% [parent panel]
  29. [label "Validez"]
  30. ; 单击“Validez”按钮时的回调过程:
  31. [callback (lambda (button event)
  32. (define text (simplifier (convert (send text-field get-value))))
  33. (send message set-label (slist->string text)))])
  34. ; 显示窗口
  35. (send frame show #t)

以下是convert函数的结果:

  1. > (convert "(a + b)")
  2. '(|(a| + |b)|)

是否可以提供建议?
提前感谢您。

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m not an English native, so if there are any mistakes, please correct me.
  4. 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)`.
  5. The user can only use addition and multiplication.
  6. 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.
  7. If the user writes `(a + (b * 1))`, how do I convert it into a list `(a + (b * 1))`?
  8. 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)

  1. Here the result of convert:

> (convert "(a + b)")
'(|(a| + |b)|)

  1. Can I have a suggestion?
  2. Thanks in advance.
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. I hope this code helps you
  7. (define (convert string) (eval (read ( open-input-string (string-append &quot;&#39;&quot; string) ) ) ) )
  8. This is the behaviour. You can get each element of the list
  9. &gt; (display (convert &quot;((a + 2) * b)&quot; ) ) (display &quot;\n&quot;)
  10. ((a + 2) * b)
  11. &gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 0 ) ) (display &quot;\n&quot;)
  12. (a + 2)
  13. &gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 1 ) ) (display &quot;\n&quot;)
  14. *
  15. &gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 2 ) ) (display &quot;\n&quot;)
  16. b
  17. &gt;
  18. Try it from your interface replacing your button with this code:
  19. ; Faire le bouton &quot;Validez&quot;
  20. (new button% [parent panel]
  21. [label &quot;Validez&quot;]
  22. ; Proc&#233;dure Callback pour un clique sur le bouton &quot;Validez&quot;:
  23. [callback (lambda (button event)
  24. ;(define text (simplifier (convert (send text-field get-value))))
  25. ;(send message set-label (slist-&gt;string text)))])
  26. (display (convert (send text-field get-value) ) )
  27. (display &quot;\n&quot;)
  28. (display (list-ref (convert (send text-field get-value) ) 0 ) )
  29. (display &quot;\n&quot;)
  30. (display (list-ref (convert (send text-field get-value) ) 1 ) )
  31. (display &quot;\n&quot;)
  32. (display (list-ref (convert (send text-field get-value) ) 2 ) )
  33. (display &quot;\n&quot;)
  34. )])
  35. and writing this expression in your interface's textfield
  36. ((a + 2) * b)
  37. the result should be:
  38. ((a + 2) * b)
  39. (a + 2)
  40. *
  41. b
  42. Hope this helps!
  43. <details>
  44. <summary>英文:</summary>
  45. I hope this code helps you
  46. (define (convert string) (eval (read ( open-input-string (string-append &quot;&#39;&quot; string) ) ) ) )
  47. This is the behaviour. You can get each element of the list
  48. &gt; (display (convert &quot;((a + 2) * b)&quot; ) ) (display &quot;\n&quot;)
  49. ((a + 2) * b)
  50. &gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 0 ) ) (display &quot;\n&quot;)
  51. (a + 2)
  52. &gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 1 ) ) (display &quot;\n&quot;)
  53. *
  54. &gt; (display (list-ref (convert &quot;((a + 2) * b)&quot; ) 2 ) ) (display &quot;\n&quot;)
  55. b
  56. &gt;
  57. Try it from your interface replacing your button with this code:
  58. ; Faire le bouton &quot;Validez&quot;
  59. (new button% [parent panel]
  60. [label &quot;Validez&quot;]
  61. ; Proc&#233;dure Callback pour un clique sur le bouton &quot;Validez&quot;:
  62. [callback (lambda (button event)
  63. ;(define text (simplifier (convert (send text-field get-value))))
  64. ;(send message set-label (slist-&gt;string text)))])
  65. (display (convert (send text-field get-value) ) )
  66. (display &quot;\n&quot;)
  67. (display (list-ref (convert (send text-field get-value) ) 0 ) )
  68. (display &quot;\n&quot;)
  69. (display (list-ref (convert (send text-field get-value) ) 1 ) )
  70. (display &quot;\n&quot;)
  71. (display (list-ref (convert (send text-field get-value) ) 2 ) )
  72. (display &quot;\n&quot;)
  73. )])
  74. and writing this expression in your interface&#39;s textfield
  75. ((a + 2) * b)
  76. the result should be:
  77. ((a + 2) * b)
  78. (a + 2)
  79. *
  80. b
  81. Hope this helps!
  82. </details>
  83. # 答案2
  84. **得分**: 0
  85. 抱歉,我将仅为您提供代码的翻译部分:

; 转换列表为字符串列表
(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))))
)))

  1. 希望这有助于您。
  2. <details>
  3. <summary>英文:</summary>
  4. sorry I didn&#39;t keep my promise to update.
  5. 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))))
)))

  1. It works for me. I can&#39;t see any simpler way.
  2. Thanks for the help.
  3. </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:

确定