Imposter.
Imposter.

Reputation: 41

Scheme convert entry into a list

how do i convert an input to a list?

(define (conjuger v t p)
    (cond ((equal? t '(present)) (present (radical v) p))

I want to input the V as a word, but i am forced to type it as a list for the program to work. Is there a way to have scheme do this:

'(m a n g e r) => '(manger)

'(manger) => '(m a n g e r)

Upvotes: 4

Views: 2081

Answers (2)

Óscar López
Óscar López

Reputation: 235984

Try this for the first part:

(define (implode lst)
  (string->symbol
   (apply string-append
          (map symbol->string lst))))

(list (implode '(m a n g e r)))
; => '(manger)

And this for the second part:

(define (explode itm)
  (map (lambda (c) (string->symbol (string c)))
       (string->list
        (symbol->string itm))))

(explode (car '(manger)))
; => '(m a n g e r)

Notice that in your code you're using symbols, not strings. I wrote my answer accordingly.

Upvotes: 4

user1710139
user1710139

Reputation:

This is the version of Nils M. Holm, Sketchy Scheme. 4.5th edition (Raleigh 2011) p. 72–74:

(define (compose f g)
  (lambda x
    (f (apply g x))))

(define symbol->list
  (compose string->list symbol->string))

(define (explode item)
  (map (lambda (item)
         (string->symbol
          (string item)))
       (symbol->list item)))

(define (implode item)
  (letrec
      ((sym->char
        (lambda (item)
          (let ((string (symbol->string item)))
            (if (not (= (string-length string) 1))
                (error "bad symbol -- IMPLODE")
                (string-ref string 0))))))
    (string->symbol
     (list->string
      (map sym->char item)))))

scheme@(guile-user)> (explode 'foo)
$1 = (f o o)
scheme@(guile-user)> (implode '(b a r))
$2 = bar

Older versions of the book are avaible online for free in pdf or html format under the title »sketchy lisp« (dealing explicitly with Scheme, though). The problem of splitting symbols into lists and vice versa is treated in chapter 2.5.

Upvotes: 0

Related Questions