Home > Blockchain >  how to get a list out of list of lists in common lisp
how to get a list out of list of lists in common lisp

Time:11-29

I am new to common lisp and trying to get a list out of a splitted string. For example: ["4-No 16dia","6-No 20dia"] Now I want to collect only the third element like ["16","20"] I have got the splitting part correctly using:

(defun my-split (string &key (delimiterp #'delimiterp)
                    )
          (loop :for beg = (position-if-not delimiterp string)
            :then (position-if-not delimiterp string :start (1  end)
                           )
            :for end = (and beg (position-if delimiterp string :start beg))
            :when beg :collect (subseq string beg end)
              :while end))

where :

(defun delimiterp (c) (position c " ,-:"))

but collecting only the third element into a list is the tricky part , I have tried:

(defparameter *list1*
  (loop for i in (cdr list)
     (append (parse-integer
                (nth 0
                     (my-split (nth 3 i)
                               :delimiterp #'delimiterp))))))

P.S: there are two list cz the example string is itself part of a list-of lists Please help me, thanks in advance

CodePudding user response:

I would use a regular expression, and I think I would do this largely irrespective of the language that I was using. Of course, some languages don't have regular expressions, but when they do it saves reinventing the wheel.

In Common Lisp, the regular expressions library is called Common Lisp - Practical Perl Compatible Regular Expressions, cl-ppcre. We load this with (ql:quickload "cl-ppcre").

Then the numbers can be returned using (ppcre:scan-to-strings "^(\\d*)-No (\\d*)dia$" x). The regular expression uses \d to pick out a digit, which in Lisp strings is written \\d. The asterisk says return zero or more digits. The parentheses in the regular expression is the bits that we are going to return, the numbers.

Doing this for a list of string is then just using mapcar.

(defparameter text-match "")
(defparameter text-numbers "")
(defparameter test-text '("4-No 16dia" "6-No 23dia"))


(defun extract-numbers (text)
  (setf (values text-match text-numbers)
    (ppcre:scan-to-strings "^(\\d*)-No (\\d*)dia$" text))
  text-numbers)

(defun extract-numbers-from-list (lst)
  (mapcar #'extract-numbers lst))

(extract-numbers-from-list test-text) ; => (#("4" "16") #("6" "23"))

Edit: lexical bindings

When I was writing the above, I was trying to get the regular expression right AND trying to get the lexical bindings right at the same time. Having only limited time I put the effort into getting the regular expressions right, and used dynamic variables and setf. OK, it got the job done, but we can do better.

The classical lexical binding system is let, syntax (let ( (var1 val1) (var2 val2) ...) body). We can try (let ((x 0))), which is valid Lisp code, but which doesn't do much. As soon as the lexical scope ends, the variable x is unbound. Attempting to access x causes an error.

We can return multiple values from many functions, such as floor or scan-to-string. We now have to bind these values to variables, using (multiple-value-bind (variable-list) values). Most websites don't really do a good job of explaining this. Having bound the variables, I was getting errors about unbound variables. OK, it's worth just saying -

multiple-value-bind binds variables lexically, just like let.

The full syntax is (multiple-value-bind (variable-list) values body) and your code goes into the body section, just like let. Hence the above code becomes:

(defparameter test-text '("4-No 16dia" "6-No 23dia"))

(defun extract-numbers (text)
   (multiple-value-bind (text-match text-numbers)
     (ppcre:scan-to-strings "^(\\d*)-No (\\d*)dia$" text)
     text-numbers))

(defun extract-numbers-from-list (lst)
  (mapcar #'extract-numbers lst))

(extract-numbers-from-list test-text) ; => (#("4" "16") #("6" "23"))

CodePudding user response:

Without dependencies, one could use:

(defun extract-nums (s)
  (mapcar #'(lambda (x) (parse-integer x :junk-allowed t)) 
          (ql-util:split-spaces s)))

And try it with:

(defparameter *s*  (list "4-No 16dia" "6-No 20dia"))
(mapcar #'extract-nums *s*)
;; => ((4 16) (6 20))

parse-integer with the setting junk-allowed-p t helps with extracting integer numbers from the string a lot.

But yes, in real-life I would also just use cl-ppcre, e.g. Mainly the functions cl-ppcre:split and cl-ppcre:scan-to-strings.

(ql:quickload :cl-ppcre)

(defun extract-nums (s)
  (mapcar #'parse-integer (cl-ppcre:scan-to-strings "(\\d )-No (\\d )dia" s))

  • Related