2014-11-03 10 views
6

sto cercando di ottenere tre numeri su una stringaCome accedere a più valori restituiti da una funzione (ad esempio, cl: parse-integer)?

(parse-integer "12 3 6" :start 0 :junk-allowed t) 
12 ; 
2 

Ora, questo ritorna 2 pure, che è il numero dove potrebbe essere analizzato. Così ora posso dare

(parse-integer "12 3 6" :start 2 :junk-allowed t) 
3 ; 
4 

Ma come faccio a memorizzare il valore di 2 e 4 che è tornato. Se I setq in una variabile sono memorizzati solo gli 12 e 3?

risposta

11

Leggere la "teoria" here.

In breve, è possibile associare la multiple values con multiple-value-bind:

(multiple-value-bind (val pos) (parse-integer "12 3 6" :start 0 :junk-allowed t) 
    (list val pos)) 
==> (12 2) 

È inoltre possibile setf multipla values:

(setf (values val pos) (parse-integer "12 3 6" :start 0 :junk-allowed t)) 
val ==> 12 
pos ==> 2 

Vedi anche VALUES Forms as Places.

PS. Nel vostro caso particolare, si potrebbe anche fare

(read-from-string (concatenate 'string 
           "(" 
           "12 3 6" 
           ")")) 

e ottenere l'elenco (12 3 6). Questo non è il modo più efficiente (perché assegna la memoria non necessaria).

PPS Vedi anche:

  1. How to convert a string to list using clisp?
  2. In lisp, how do I use the second value that the floor function returns?