# How do I convert a list written on a text field from string to list?

**URL:** <https://racket.discourse.group/t/how-do-i-convert-a-list-written-on-a-text-field-from-string-to-list/1945>\
**Category:** Questions & Answers\
**Tags:** gui\
**Created:** [May 13, 2023, 6:46pm UTC](https://racket.discourse.group/t/how-do-i-convert-a-list-written-on-a-text-field-from-string-to-list/1945 "2023-05-13T18:46:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![louberlu](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/louberlu/32/1142_2.png) [@louberlu](https://racket.discourse.group/u/louberlu)\
**Post date:** [May 13, 2023, 6:46pm UTC](https://racket.discourse.group/t/how-do-i-convert-a-list-written-on-a-text-field-from-string-to-list/1945/1 "2023-05-13T18:46:44Z")

</div>

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.

```scheme
(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:

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

```

Here is a modification of that I have done.

```scheme
(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.

```scheme
> (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.

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [May 13, 2023, 7:17pm UTC](https://racket.discourse.group/t/how-do-i-convert-a-list-written-on-a-text-field-from-string-to-list/1945/2 "2023-05-13T19:17:40Z")

</div>

I _was_ going to steer you to the design recipe... and at some point soon I probably will... but in this case, you're trying to duplicate functionality that is 100% already in existence:

```scheme
#lang racket

(require rackunit)

(define (convert str)
  (read (open-input-string "(a + b)")))

(check-equal? (convert "(a + b)") '(a + b))

```

It's perhaps worth noting that this will only read the _first_ s-expression from the string, so for instance `(convert "(a + b)(a + b)") ` will only return `'(a + b)`; if that's a problem, you'll have to build a loop to read repeatedly.

---

<div class="post-metadata">

**Author:** ![louberlu](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/louberlu/32/1142_2.png) [@louberlu](https://racket.discourse.group/u/louberlu)\
**Post date:** [May 13, 2023, 7:26pm UTC](https://racket.discourse.group/t/how-do-i-convert-a-list-written-on-a-text-field-from-string-to-list/1945/3 "2023-05-13T19:26:57Z")

</div>

It works when I test in the interpreter.

```scheme
> (read (open-input-string "(a + (b + c))"))
'(a + (b + c))

```

But on the window, I write the same thing, and it returns this on the interpreter :  
`symbol→string: contract violation expected: symbol? Given: '(b + c)`

Can I have a suggestion? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![louberlu](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/louberlu/32/1142_2.png) [@louberlu](https://racket.discourse.group/u/louberlu)\
**Post date:** [May 13, 2023, 7:56pm UTC](https://racket.discourse.group/t/how-do-i-convert-a-list-written-on-a-text-field-from-string-to-list/1945/4 "2023-05-13T19:56:42Z")

</div>

I know why it doesn't work. Because this function only work with simple list.

```scheme
(define (slist->string slst)
  (string-join (map symbol->string slst) " "))

```

I will try to modify it and see if it works.  
I will update tomorrow.

---

<div class="post-metadata">

**Author:** ![louberlu](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/louberlu/32/1142_2.png) [@louberlu](https://racket.discourse.group/u/louberlu)\
**Post date:** [June 1, 2023, 9:28am UTC](https://racket.discourse.group/t/how-do-i-convert-a-list-written-on-a-text-field-from-string-to-list/1945/5 "2023-06-01T09:28:07Z")

</div>

Hi, sorry I didn't keep my promise to update.  
Here how I made it work.

```scheme
; 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.
