You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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