Title: Showing some Common Lisp features
Author: Solène
Date: 05 December 2017
Tags: lisp
Description:
# Introduction: comparing LISP to Perl and Python
We will refer to Common LISP as CL in the following article.
I wrote it to share what I like about CL. I'm using Perl to compare CL
features. I am using real world cases for the average programmer. If
you are a CL or perl expert, you may say that some example could be
rewritten with very specific syntax to make it smaller or faster, but
the point here is to show usual and readable examples for usual
programmers.
This article is aimed at people with programming interest, some basis
of programming knowledge are needed to understand the following. If
you know how to read C, Php, Python or Perl it should be
enough. Examples have been choosed to be easy.
I thank my friend killruana for his contribution as he wrote the
python code.
## Variables
### Scope: global
**Common Lisp code**
(defparameter *variable* "value")
Defining a variable with defparameter on top-level (= outside of a
function) will make it global. It is common to surround the name of
global variables with **\*** character in CL code. This is only for
readability for the programmer, the use of **\*** has no
incidence.
**Perl code**
my $variable = "value";
**Python code**
variable = "value";
### Scope: local
This is where it begins interesting in CL. Declaring a local variable
with **let** create a new scope with parenthesis where the variable
isn't known outside of it. This prevent doing bad things with
variables not set or already freed. **let** can define multiple
variables at once, or even variables depending on previously declared
variables using **let\***
**Common Lisp code**
(let ((value (http-request)))
(when value
(let* ((page-title (get-title value))
(title-size (length page-title)))
(when page-title
(let ((first-char (subseq page-title 0 1)))
(format t "First char of page title is ~a~%"
first-char))))))
**Perl code**
{
local $value = http_request;
if($value) {
local $page_title = get_title $value;
local $title_size = get_size $page_title;
if($page_title) {
local $first_char = substr $page_title, 0, 1;
printf "First char of page title is %s\n", $first_char;
}
}
}
dataswamp.org:70 /~solene/article-lisp-compare:83: port field too long