6. Functions

The asterisk, or *, indicates the Common Lisp REPL prompt. In this case, we are calling a custom function named my-last that takes a list as an argument and returns the last element of that list.

As you can see, Common Lisp syntax is structured with parentheses, or S-expressions. The first position inside the parentheses indicates the function to call, and the following positions are the arguments passed to that function. Our future my-last function will take a list as an argument and return the last element of that list.

Functions are defined with the defun keyword, followed by the function name, a parameter list in parentheses, and the function body.

For example:

(defun total (num1 num2)
  (+ num1 num2))

To call it:

* (total 3 5)
8

In other languages we have a reserved word like return to return a value from a function. In Common Lisp, the value of the last expression is the value that is returned. In the previous example, the expression (+ num1 num2) is the last expression, and the only one, so its result is the value returned by the function.

Another way to call it is through an anonymous function:

* (funcall #'total 10 15)
25

Or using apply to pass a list of arguments:

* (apply #'total '(20 30))
50

Using '(20 30) is a way to create a literal list in Common Lisp. The apostrophe (') before the parenthesis indicates that what follows is a literal list and should not be evaluated. You could also use the list function to create the same list: (list 20 30).

To capture the values individually, you can use nth-value, first, or last (these are functions that operate on multiple returned values or lists):

* (nth 2 '(a b c d))
(C)

* (first '(a b c d))
(A)

* (last '(a b c d))
(D)

* (last '(a b c d) 2)
(C D)

* (subseq '(a b c d) 1 3)
(B C)

You also have functions such as car and cdr at your disposal.

* (car '(a b c d))
A

* (cdr '(a b c d))
(B C D)

The difference between first and car is that first is a generic function that can operate on different types of sequences, while car is specifically designed for linked lists. In practice, for lists, both work the same way.

Common Lisp inherited from its predecessors a series of combinations of car and cdr that allow you to access nested elements in lists. For example, cadr is equivalent to (car (cdr list)), that is, the second element.

* (cadr '(a b c d))
B

* (caddr '(a b c d))
C

* (cadddr '(a b c d))
D

* (cddr '(a b c d))
(C D)

* (cdddr '(a b c d))
(D)

These functions can be combined up to four levels deep (c[ad]{2,4}r), although in practice they are rarely used beyond three levels.

However, the modern Common Lisp standard provides more readable alternatives for accessing elements of a list:

* (first '(a b c d))
A

* (rest '(a b c d))
(B C D)

* (second '(a b c d))
B

* (third '(a b c d))
C

* (fourth '(a b c d))
D

There are equivalent functions up to tenth (tenth element). The rest function is equivalent to cdr and returns all elements except the first.

* (fifth '(a b c d e f))
E

* (tenth '(1 2 3 4 5 6 7 8 9 10))
10

As a general rule, it is recommended to use the modern versions (first, second, rest, etc.) in new code, since they are more descriptive and easier to read. The car and cdr functions are kept for historical and compatibility reasons.

And there are other useful functions for manipulating lists such as reverse, append, cons, list, among others.

* (reverse '(a b c d))
(D C B A)

* (append '(a b) '(c d))
(A B C D)

* (cons 'a '(b c d))
(A B C D)

* (list 'a 'b 'c 'd)
(A B C D)

We will develop these further later on, but for now you already have the tools you need to solve some problems.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Desafíos de programación atemporales y multiparadigmáticos

Desafíos de programación atemporales y multiparadigmáticos

Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.

Buy the book

Will you buy me a coffee?

This is how I keep writing without ads or paywalls.

Comments

There are no comments yet.