"for comprehension" in Common Lisp

Suppose you have a list and want to create new list by applying some algorithms.

Common Lisp provides collect funcion for this. For example, let's say we have a list of string in lower-case letters and we want to capitalize all the strings in list. We can do that as follows:

(defvar names '("james" "maria" "rico" "peter"))

CL-USER> (loop for name in names
             collect (string-capitalize name))
("James" "Maria" "Rico" "Peter")

Or let's say you want new list which contains length of each string in list, then you can do following:

CL-USER> (loop for name in names
             collect (length name))
(5 5 4 5)

Or let's say you want a new list with all entries in upper-case, then you can do the following:

CL-USER> (loop for name in names
             collect (string-upcase name))
("JAMES" "MARIA" "RICO" "PETER")

But, lisp is pretty cool functional language. We can also use map function achieve same results. Pay attention to map function signature below:

(map result-type function &rest sequences+)

Here, you have to specify expected result-type. In our case it will be 'list.

For example: to capitalize all entries in list of string

CL-USER> (map 'list #'string-capitalize names)
("James" "Maria" "Rico" "Peter")

or, to get new list with length of each entry:

CL-USER> (map 'list #'length names)
(5 5 4 5)

or, to convert all elements of list to uppercase:

CL-USER> (map 'list #'string-upcase names)
("JAMES" "MARIA" "RICO" "PETER")

Bubble Sort in Common Lisp

"Bubble Sort" is usually first sorting algorithm beginners stumble upon when learning about in algoritms part in "Data Structure and Algorithm".

In real-life to understand this one quick example can be, suppose you are given a group of humans and your job is to arrange them in order of height i.e. smallest person first and tallest one should be last. What you are going to do is, ask them stand in a line. Now take the first person and compare height of first person with height of second person. If First person is taller, then you swap their position. If not, you proceed and compare first person's height with next person. If first person is taller, than you swap their position otherwise you move on to next. If no person smaller then first person is found then you have found it's right position.

Now you repeat above process, but this time you start from second peson and use same steps. This way you put the second person in right position.

Repeat this process and each time, start with one position higher than position used in previous step. In the end you ends up arranging all the person in order of their heights.

So, how you do it in Common Lisp. You create a function which takes list as argumnent. Measure the length to know how many times you have to iterate the list. We have run two loops, one to make sure we process all elements and second one to really compare and swap the elements of the list. See code below:

(defun bubble-sort (x)
  (let ((l (length x)))
    (dotimes (n l)
      (let ((swapped nil))
       (dotimes (m (- l 1))
         (let ((num1 (nth m x))          ; get number at current index
               (num2 (nth (+ m 1) x)))   ; get number at current index + 1
           (if (> num1 num2)             ; if current number is larger than next, then swap the values.
               (let ((temp num1))
                 (setf (nth m x) num2)
                 (setf (nth (+ m 1) x) temp)
                 (setf swapped T)))
           ))
       (when (not swapped)               ; exit if, list is already sorted.
         (return x))))))

Welcome

Hi,

How are you?

Welcome to lambda world. This website is all about how to do things in functional programmng langauges. Mainly Lisp, Haskell, Clojure and OCaml.

Thank You, JR