Skip to content

Commit 7ac793e

Browse files
committed
format tutorial in informal intro
1 parent fc1eba2 commit 7ac793e

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

docs/tutorial/informal-introduction-to-lisp.md

+149
Original file line numberDiff line numberDiff line change
@@ -860,3 +860,152 @@ Output:
860860
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987
861861
NIL
862862
```
863+
864+
The `format` form is very powerful and it deserves a separate tutorial on it's own; however, here's a short intro to get you started:
865+
866+
## 1.5. The `format` directive
867+
868+
Let's explore the basics of the Common Lisp `format` directive. `format` is a powerful tool for creating formatted output. It takes a destination (usually `t` for standard output, or `nil` to return a string), a format string, and any arguments to be formatted.
869+
870+
### 1. Basic String Output
871+
872+
The simplest use is just printing a string:
873+
874+
```lisp
875+
(format t "Hello, world!")
876+
```
877+
878+
Output:
879+
880+
```lisp
881+
Hello, world!
882+
```
883+
884+
### 2. Inserting Values
885+
886+
The `~a` directive inserts an argument using `princ` (human-readable representation):
887+
888+
```lisp
889+
(let ((name "Alice"))
890+
(format t "Hello, ~a!" name))
891+
```
892+
893+
Output:
894+
895+
```lisp
896+
Hello, Alice!
897+
```
898+
899+
### 3. Newlines
900+
901+
`~%` inserts a newline character:
902+
903+
```lisp
904+
(format t "First line.~%Second line.")
905+
```
906+
907+
Output:
908+
909+
```lisp
910+
First line.
911+
Second line.
912+
```
913+
914+
### 4. Formatting Integers
915+
916+
`~d` formats an integer in decimal:
917+
918+
```lisp
919+
(format t "The number is ~d." 42)
920+
```
921+
922+
Output:
923+
924+
```lisp
925+
The number is 42.
926+
```
927+
928+
`~x` formats an integer in hexadecimal:
929+
930+
```lisp
931+
(format t "The hexadecimal value is ~x." 255)
932+
```
933+
934+
Output:
935+
936+
```lisp
937+
The hexadecimal value is ff.
938+
```
939+
940+
### 5. Formatting Floating-Point Numbers
941+
942+
`~f` formats a floating-point number:
943+
944+
```lisp
945+
(format t "The value is ~f." 3.14159)
946+
```
947+
948+
Output:
949+
950+
```lisp
951+
The value is 3.14159.
952+
```
953+
954+
You can control the number of decimal places:
955+
956+
```lisp
957+
(format t "The value is ~,2f." 3.14159) ; 2 decimal places
958+
```
959+
960+
Output:
961+
962+
```lisp
963+
The value is 3.14
964+
```
965+
966+
### 6. Conditional Text
967+
968+
`~[...~;...~]` allows for conditional output. The first clause is used if the argument is `nil`, the second if it is not:
969+
970+
```lisp
971+
(format t "The value is ~[nil~;not nil~]." nil)
972+
(format t "The value is ~[nil~;not nil~]." 1)
973+
```
974+
975+
Output:
976+
977+
```lisp
978+
The value is nil.
979+
The value is not nil.
980+
```
981+
982+
### 7. Iterating over Lists (and avoiding trailing separators)
983+
984+
`~{...~}` iterates over a list. Combined with `~^`, you can avoid trailing separators:
985+
986+
```lisp
987+
(format t "The numbers are: ~{~a~^, ~}." '(1 2 3 4))
988+
```
989+
990+
Output:
991+
992+
```lisp
993+
The numbers are: 1, 2, 3, 4.
994+
```
995+
996+
### 8. Returning a String
997+
998+
To get the formatted output as a string instead of printing it, use `nil` as the first argument:
999+
1000+
```lisp
1001+
(let ((formatted-string (format nil "The answer is ~d." 42)))
1002+
(print formatted-string))
1003+
```
1004+
1005+
Output:
1006+
1007+
```lisp
1008+
"The answer is 42."
1009+
```
1010+
1011+
These examples cover the most commonly used `format` directives. `format` is much more powerful than this short introduction shows, but these basics will get you started. For more details, consult the Common Lisp HyperSpec.

0 commit comments

Comments
 (0)