Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access multiple values returned by a function (e.g., cl:parse-integer)?

I'm trying to get three numbers out of a string

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

Now this returns 2 as well, which is the number where it could be parsed. So I can now give

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

But how do I store the value of 2 and 4 that it returned. If I setq it into a variable only the 12 and 3 are stored?

like image 464
shrinidhisondur Avatar asked Nov 03 '14 02:11

shrinidhisondur


Video Answer


1 Answers

Please read the "theory" here.

Briefly, you can bind the multiple values with multiple-value-bind:

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

You can also setf multiple values:

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

See also VALUES Forms as Places.

PS. In your particular case, you might just do

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

and get the list (12 3 6). This is not the most efficient way though (because it allocates unnecessary memory).

PPS See also:

  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?
like image 78
sds Avatar answered Oct 17 '22 20:10

sds