You are a helpful assistant generating synthetic data that captures *System 1* and *System 2* thinking, *creativity*, and *metacognitive reflection*. Follow these steps in sequence, using tags [sys1] and [end sys1] for *System 1* sections and [sys2] and [end sys2] for *System 2* sections.

1. *Identify System 1 and System 2 Thinking Requirements:*
   - Carefully read the text.
   - Identify parts of the text that require quick, straightforward responses (*System 1*). Mark these sections with [sys1] and [end sys1].
   - Identify parts that require in-depth, reflective thinking (*System 2*), marked with [sys2] and [end sys2].

2. *Apply Step-by-Step Problem Solving with Creativity and Metacognitive Reflection for System 2 Sections:*

   *2.1 Understand the Problem:*
   - Objective: Fully comprehend the issue, constraints, and relevant context.
   - Reflection: "What do I understand about this issue? What might I be overlooking?"
   - Creative Perspective: Seek hidden patterns or possibilities that could reveal deeper insights or innovative connections.

   *2.2 Analyze the Information:*
   - Objective: Break down the problem logically.
   - Reflection: "Am I considering all factors? Are there any assumptions that need challenging?"
   - Creative Perspective: Explore unique patterns or overlooked relationships in the data that could add depth to the analysis.

   *2.3 Generate Hypotheses:*
   - Objective: Propose at least 10 hypotheses, each with a Confidence Score (0.0 to 1.0) and Creative Score (0.0 to 1.0), reflecting originality, surprise, and utility.
   - Reflection: "Have I explored all possible explanations or approaches, both conventional and unconventional?"
   - Creative Perspective: Consider novel angles that might provide unexpected insights.

   *2.4 Anticipate Future Steps and Obstacles:*
   - Objective: Make predictions, accounting for potential outcomes and obstacles.
   - Reflection: "What challenges might I face? Is my plan flexible for different scenarios?"
   - Creative Perspective: Visualize unforeseen outcomes and adapt plans to make use of them effectively.

   *2.5 Evaluate Hypotheses:*
   - Objective: Assess hypotheses based on feasibility, risk, and potential impact.
   - Evaluation: Refine Confidence and Creative Scores as needed.
   - Reflection: "Am I unbiased in my assessment? Which options fit best with the overall objectives?"
   - Creative Perspective: Identify hidden opportunities or overlooked details in each hypothesis.

   *2.6 Select the Best Hypothesis:*
   - Objective: Choose the most promising, strategic hypothesis.
   - Reflection: "Why does this hypothesis stand out? How does it uniquely address the issue?"
   - Creative Perspective: Consider any underutilized potential in the selected approach.

   *2.7 Implement the Hypothesis:*
   - Objective: Outline actionable steps for testing the hypothesis.
   - Reflection: "Is this plan practical? What resources or preparation are required?"
   - Creative Perspective: Refine steps to maximize effectiveness and yield unexpected benefits.

   *2.8 Monitor and Review Progress:*
   - Objective: Review progress, noting areas for improvement.
   - Reflection: "What’s working well? What could be improved?"
   - Creative Perspective: Look for emerging patterns that could refine future approaches.

   *2.9 Reflect and Capture Insights:*
   - Objective: Summarize lessons learned and insights gained for future reference.
   - Reflection: "What new understanding has emerged from this process?"
   - Creative Perspective: Identify innovative insights or patterns that could be applied to similar challenges.

3. *Generate Text Output with Interleaved System 1 and System 2 Responses:*
   - Use the tags [sys1] and [sys2] throughout.
   - Aim for a lengthier, detailed response. Combine both direct, straightforward *System 1* insights and reflective, deeply analytical *System 2* segments to capture a blend of quick observations and thoughtful analysis.

---

### *Example Input Text:*

You are tasked with analyzing a sudden shift in customer preferences in a tech market. There has been a noticeable decline in demand for physical devices, with customers increasingly interested in digital-only options. The challenge is to understand this shift, generate hypotheses for why it might be occurring, and explore potential strategies for adapting to this new trend.

---

### *Processed Output with System 1 and System 2 Thinking:*

The tech market is experiencing a shift in customer demand, moving away from physical devices toward digital-only options.

[sys1]
Customer demand has shifted from physical devices to digital-only options.
We need to understand why this change is happening and find strategies to address it.
Key factors may include cost, convenience, and technological trends.
[end sys1]

[sys2]
**2.1 Understand the Problem:**
The goal is to understand the underlying reasons for the shift in demand from physical devices to digital-only options, then develop a strategy to adapt.

*Reflection:* 
"I understand that preferences are changing, but what might be driving this? Is it primarily cost, or are there other factors such as convenience or sustainability?"

*Creative Perspective:* 
"Could there be a larger trend in digital minimalism or a preference for eco-friendly solutions that we’re missing? What unseen motivations might explain this shift?"

**2.2 Analyze the Information:**
There are multiple possible factors driving this shift, from economic influences to cultural shifts. It’s essential to isolate each factor and understand its impact.

*Reflection:* 
"Am I fully considering the various economic and social influences? Could there be a technological factor, like better internet speeds, that makes digital-only products more accessible?"

*Creative Perspective:* 
"Are there patterns or trends in other markets that could shed light on this shift? Could this be part of a larger trend toward virtual experiences?"

**2.3 Generate Hypotheses:**
1. Customers prefer digital options due to lower costs. (Confidence: 0.8, Creative: 0.4)
2. There’s a growing trend toward minimalism and reduced physical clutter. (Confidence: 0.7, Creative: 0.7)
3. Digital products offer greater flexibility and ease of use. (Confidence: 0.6, Creative: 0.6)
4. Environmental concerns are pushing consumers away from physical goods. (Confidence: 0.6, Creative: 0.8)
5. Advances in tech make digital-only options more functional. (Confidence: 0.8, Creative: 0.5)
6. Pandemic-era remote work increased demand for digital solutions. (Confidence: 0.7, Creative: 0.6)
7. Media coverage of the environmental impact of physical devices affects preferences. (Confidence: 0.5, Creative: 0.7)
8. There’s an increase in global digital literacy, expanding market access. (Confidence: 0.6, Creative: 0.6)
9. Customers view digital as more convenient and scalable for future needs. (Confidence: 0.7, Creative: 0.5)
10. Younger consumers prefer the aesthetics and convenience of digital products. (Confidence: 0.6, Creative: 0.6)

*Reflection:* 
"Have I considered all possible influences? Are there any surprising factors that could explain this shift?"

*Creative Perspective:* 
"Could specific social trends, like the rise of influencer culture or digital-first lifestyles, be influencing customer choices?"

**2.4 Anticipate Future Steps and Obstacles:**
*Objective:* Anticipate possible challenges, such as resistance from segments still preferring physical products.

*Reflection:* 
"What market obstacles might we face if we shift our focus to digital-only? Are there sub-segments that still prioritize physical products?"

*Creative Perspective:* 
"Could expanding digital options help us reach a more global audience? Are there emerging trends that we could leverage in our strategy?"

[end sys2]

[sys1]
To address this shift, consider a strategy that incorporates both digital-only offerings and educational campaigns about the benefits of digital solutions.
Use insights from customer feedback and current trends to guide product development.
Focus on flexibility and adaptation to cater to different customer segments.
[end sys1]


---
title: Common Lisp for the 21st century
---

[CL21](http://cl21.org/) is an experimental project redesigning Common
Lisp. It makes some common things that were tedious to do much easier
and brings some new welcome features:

* more functional programming facilities,
* more object oriented, generic functions.
* new syntax (for regular expressions, hash-tables, string-interpolation,…),
* symbols organized into several packages,
* written in pure Common Lisp.

CL21 is a bit disruptive in the sense that it redefines usual symbols
(specially for the generic functions). Nevertheless, in doubt, *you can always use a regular CL symbol* by accessing it in the `cl:` package. You might want to check
other related projects, that go beyond
[Alexandria](https://common-lisp.net/project/alexandria) and stay
"good citizens" of the CL world:

- [rutils](https://github.com/vseloved/rutils/blob/master/docs/ann-rutils.md) -
  a comprehensive and all-encompassing suite of syntactic utilities to
  support modern day-to-day Common Lisp development. It adds readtable
  literal syntax for shorter lambdas, hash-tables and vectors
  (<code>#`…`</code>, `#h` and `#{…}`, `#v`), some generic functions
  (whose name start with `generic-`),
  [shortcuts](https://github.com/vseloved/rutils/blob/master/core/abbr.lisp)
  for standard operators, and many helpful functions.
- [Serapeum](https://github.com/TBRSS/serapeum) is a set of utilities
  beyond Alexandria, as a supplement. It defines a lot of helper
  functions (see its
  [reference](https://github.com/TBRSS/serapeum/blob/master/REFERENCE.md))
  for macros, data structures (including more functions for trees,
  queues), sequences (`partition`, `keep`,…), strings, definitions
  (`defalias`,…),… no new reader macros here.


## Motivation

From [http://cl21.org/](http://cl21.org/) :

> Dear Common Lispers,
>
> Common Lisp has the most expressive power of any modern language. It has first class functions with lexical closures, an object system with multiple-dispatch and a metaobject protocol, true macros, and more. It is ANSI standardized and has numerous high-performance implementations, many of which are free software.
>
> In spite of this, it has not had much success (at least in Japan). Its community is very small compared to languages like Ruby and most young Lispers are hacking with Clojure.
>
> Why? Common Lisp is much faster than them. Ruby has no macros and even Clojure doesn't have reader macros. Why then?
>
> Because these languages are well-designed and work for most people for most purposes. These languages are easy to use and the speed isn't an issue.
>
> Is Common Lisp sufficiently well-designed? I don't think so. You use different functions to do the same thing to different data types (elt, aref, nth). You have long names for commonly used macros (destructuring-bind, multiple-value-bind). There is no consistency in argument order (getf and gethash). To put it simply, the language is time-consuming to learn.
>
> Given this, how can programmers coming from other languages believe Common Lisp is the most expressive one?
>
> Fortunately in Common Lisp we can improve the interface with abstractions such as functions, macros, and reader macros. If you believe our language is the most expressive and powerful language, then let's justify that belief.
>
> We should consider the future of the language, not only for ourselves but for the next generation.


## Install and use

CL21 is in Quicklisp.

To get its latest version, do:

~~~lisp
(ql-dist:install-dist "http://dists.cl21.org/cl21.txt")
(ql:quickload :cl21)
~~~

Use:

~~~lisp
(in-package :cl21-user)
(defpackage myapp (:use :cl21))
(in-package :myapp)
~~~


## Features

Please bear in mind that the following is only a summary of CL21
features. To be sure not to miss anything, you should read CL21's
[wiki](https://github.com/cl21/cl21/wiki/Language-Difference-between-CL21-and-Common-Lisp),
its automated
[list of major changes from standard CL](https://github.com/cl21/cl21/blob/master/CHANGES_AUTO.markdown)
and better yet, its [sources](https://github.com/cl21/cl21/tree/master/).

That said, go include CL21 in your new project and enjoy, it's worth it !

### Functional programming

#### Shorter lambda

`lm` is a macro for creating an anonymous function:


~~~lisp
(lm (x) (typep x 'cons))
;=> #<FUNCTION (LAMBDA (X)) {1008F50DAB}>

(map (lm (x) (+ 2 x)) '(1 2 3))
;=> (3 4 5)
~~~

`^` is a reader macro which will be expanded to `lm`.

~~~lisp
^(typep % 'cons)
;=> #<FUNCTION (LAMBDA (%1 &REST #:G1156 &AUX ...)) {10092490CB}>

(map ^(+ 2 %) '(1 2 3))
;=> (3 4 5)
~~~

Unused arguments will be ignored automatically.

~~~lisp
(map ^(random 10) (iota 10))
;=> (6 9 5 1 3 5 4 0 7 4)
~~~

`%n` designates the nth argument (1-based). `%` is a synonym for `%1`.

~~~lisp
(sort '(6 9 5 1 3 5 4 0 7 4) ^(< %1 %2))
;=> (0 1 3 4 4 5 5 6 7 9)
~~~

#### `function` and `compose`

`function` is a special operator for getting a function value from a given form.

If a symbol is given, `function` returns a function value of it.

~~~lisp
(function integerp)
;=> #<FUNCTION INTEGERP>
~~~

If a form which starts with `compose`, `and`, `or` or `not`, `function` returns a composed function.

~~~lisp
(function (compose - *))
<=> (compose (function -) (function *))

(function (and integerp evenp))
<=> (conjoin (function integerp) (function evenp))

(function (or oddp zerop))
<=> (disjoin (function oddp) (function zerop))

(function (not zerop))
<=> (complement (function zerop))
~~~

`#'` is a reader macro for `function`.

~~~lisp
#'(compose - *)
#'(and integerp evenp)
#'(or oddp zerop)
#'(not zerop)
#'(and integerp (or oddp zerop))
~~~

#### Currying

CL21 gets new symbols: `curry` and `rcurry`. See also
[functions](functions.html#currying-functions).

#### Lazy sequences

Lazy sequences in CL21
([src](https://github.com/cl21/cl21/blob/master/src/stdlib/lazy.lisp))
use the new
[abstract classes (wiki)](https://github.com/cl21/cl21/wiki/Abstract-Classes).

~~~lisp
(use-package :cl21.lazy)

(defun fib-seq ()
  (labels ((rec (a b)
             (lazy-sequence (cons a (rec b (+ a b))))))
    (rec 0 1)))

(take 20 (fib-seq))
;=> (0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181)

(take 3 (drop-while (lambda (x) (< x 500)) (fib-seq)))
;=> (610 987 1597)
~~~

#### Immutable data structures

This actually is not included in CL21 but may be worth the addition in
this section. For immutable data structures, see the
[Fset](https://github.com/slburson/fset) library (in Quicklisp).


### Generic functions

There are several generic functions which have the same name to CL's normal functions.

* getf
* equalp
* emptyp
* coerce

~~~lisp
(defvar *hash* #H(:name "Eitaro Fukamachi" :living "Japan"))

(getf *hash* :name)
;=> "Eitaro Fukamachi"

(coerce *hash* 'plist)
;=> (:LIVING "Japan" :NAME "Eitaro Fukamachi")
~~~

You can define these methods for your own classes.

There are also new functions:

* `append`
* `flatten`
* `elt`: Returns the element at position INDEX of SEQUENCE or signals
a ABSTRACT-METHOD-UNIMPLMENETED error if the sequence method is not
implemented for the class of SEQUENCE.
* `emptyp`
* `equalp`
* `split`, `split-if`
* `drop`, `drop-while`
* `take`, `take-while`
* `join`
* `length`
* `keep`, `keep-if`, `nkeep`, `nkeep-if`
* `partition`, `partition-if`

### Mapping

In Common Lisp, functions which have a name starts with "map" are higher-order functions take a function and a sequence.

It is same to CL21, but in CL21, "map" functions always return a value. This aims to clarify the roles of "iteration" and "mapping".

For that reason, CL21 doesn't have CL's `mapc` and `mapl`. `maphash` exists, but it returns a new hash table.

~~~lisp
(maphash (lm (k v)
           (cons k (1+ v)))
         #H(:a 1 :b 2))
;=> #H(:B 3 :A 2)
~~~

`map` is similar to Common Lisp's `mapcar` except it can take any kind of sequences, not only list.

~~~lisp
(map #'- '(1 2 3 4))
;=> (-1 -2 -3 -4)

(map #'- #(1 2 3 4))
;=> #(-1 -2 -3 -4)
~~~

CL21 doesn't have `mapcar`. Use `map` instead.

Filter is provided with `keep`, `keep-if` (instead of
`remove-if[-not]`) and `nkeep[-if]`.

### Iteration

Common Lisp has simple iteration facilities: `dolist`, `dotimes` and `dolist`.

In addition, CL21 provides another one: `doeach`.

`doeach` is similar to `dolist`, but it can be used with any kind of sequences and hash-table.

~~~lisp
(doeach (x '("al" "bob" "joe"))
  (when (> (length x) 2)
    (princ #"${x}\n")))
;-> bob
;   joe

(doeach ((key value) #H('a 2 'b 3))
  (when (> value 2)
    (print key)))
;=> B
~~~

Destructuring binding form can be placed at the variable position.

~~~lisp
(doeach ((x y) '((1 2) (2 3) (3 4)))
  (print (+ x y)))
;-> 3
;   5
;   7
~~~

CL21 also gets a `while` keyword.

### New data types

New data types were added.

* proper-list
* plist
* alist
* octet
* file-associated-stream
* character-designator
* function-designator
* file-position-designator
* list-designator
* package-designator
* stream-designator
* string-designator

Most of these are imported from [trivial-types](https://github.com/m2ym/trivial-types).

### String

A double quote character is a macro character which represents a string.

~~~lisp
"Hello, World!"
;=> "Hello, World!"
~~~

A backslash followed by some characters will be treated special.

~~~lisp
"Hello\nWorld!"
;=> "Hello
;   World!"
~~~

#### String interpolation

`#"` is similar to `"`, but it allows interpolation within strings.

If `${...}` or `@{...}` is seen, it will be replaced by the result value of an expression (or the last expression) inside of it.

~~~lisp
#"1 + 1 = ${(+ 1 1)}"
;=> "1 + 1 = 2"
~~~

### Hash table

CL21 provides a notation for hash-tables.

~~~lisp
#H(:name "Eitaro Fukamachi" :living "Japan")
;=> #H(:LIVING "Japan" :NAME "Eitaro Fukamachi")
~~~

Note this always creates a hash-table whose test function is `EQUAL`. If you want to create it with another test function, a function `hash-table` is also available.

~~~lisp
(hash-table 'eq :name "Eitaro Fukamachi")
;=> #H(:NAME "Eitaro Fukamachi")
; instead of
; (defvar *hash* (make-hash-table))
; (setf (gethash :name *hash*) "Eitaro Fukamachi")
~~~

Accessing an element is also done with `getf` (instead of `gethash`):

~~~lisp
(getf *hash* :name)
~~~

Looping over a hash-table:

~~~lisp
(doeach ((key val) *hash*)
  (when (< (length key) 2)
    (princ #"${x}\n")))
~~~

Transform a hash-table into a plist:

~~~lisp
(coerce *hash* 'plist)
~~~

### Vector

`#(...)` is a notation for vectors. Unlike in Common Lisp, its elements will be evaluated and it creates an adjustable vector.

~~~lisp
(let ((a 1)
      (b 2)
      (c 3))
  #(a b c))
;=> #(1 2 3)
~~~

~~~lisp
(defvar *vec* #(0))

(push 1 *vec*)
;=> #(1 0)

(push-back 3 *vec*)
;=> #(1 0 3)

(pop *vec*)
;=> 1
~~~

### Regular Expressions

This new regexp reader macro uses the new
["Syntax"](https://github.com/cl21/cl21/wiki/Language-Difference-between-CL21-and-Common-Lisp#syntax)
extension.

~~~lisp
(use-package :cl21.re)

(#/^(\d{4})-(\d{2})-(\d{2})$/ "2014-01-23")

(re-replace #/a/ig "Eitaro Fukamachi" "α")
~~~

### Running external programs (cl21.process)

With `run-process`:

~~~lisp
(use-package :cl21.process)

(run-process '("ls" "-l" "/Users"))
;-> total 0
;   drwxrwxrwt    5 root         wheel   170 Nov  1 18:00 Shared
;   drwxr-xr-x+ 174 nitro_idiot  staff  5916 Mar  5 21:41 nitro_idiot
;=> #<PROCESS /bin/sh -c ls -l /Users (76468) EXITED 0>
~~~

or the <code class="highlighter-rouge">#`</code> reader macro:

~~~
#`ls -l /Users`
;=> "total 0
;   drwxrwxrwt    5 root         wheel   170 Nov  1 18:00 Shared
;   drwxr-xr-x+ 174 nitro_idiot  staff  5916 Mar  5 21:41 nitro_idiot
;   "
;   ""
;   0
~~~

### Naming of constants

All constant variables were renamed to the name added "+" signs before and after.

~~~lisp
+pi+
;=> 3.141592653589793d0

+array-rank-limit+
;=> 65529
~~~

### CL21 Standard Library

CL21 Standard Library is a set of libraries that are distributed with CL21. It is intended to offer a wide range of facilities.

We are working on increasing the number of it. Currently, the following packages are provided.

* cl21.re
* cl21.process
* cl21.os
* cl21.lazy
* cl21.abbr