4 ms·
For what it's worth, on a fairly recent SBCL, here's what the compiler/interpreter prints when you enter the author's function: ~> sbcl This is SBCL 1.4.7,
by waterhouse 8y ago
For what it's worth, on a fairly recent SBCL, here's what the compiler/interpreter prints when you enter the author's function:
~> sbcl
This is SBCL 1.4.7, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
* (defun add-text-padding (str &key padding newline)
"Add padding to text STR. Every line except for the first one, will be
prefixed with PADDING spaces. If NEWLINE is non-NIL, newline character will
be prepended to the text making it start on the next line with padding
applied to every single line."
(let ((str (if newline
(concatenate 'string (string #\Newline) str)
str)))
(with-output-to-string (s)
(map 'string
(lambda (x)
(princ x s)
(when (char= x #\Newline)
(dotimes (i padding)
(princ #\Space s))))
str))))
; in: DEFUN ADD-TEXT-PADDING
; (MAP 'STRING
; (LAMBDA (X)
; (PRINC X S)
; (WHEN (CHAR= X #\Newline) (DOTIMES (I PADDING) (PRINC #\ S))))
; STR)
; --> TRULY-THE SB-KERNEL:%MAP
; ==>
; (MAP-INTO (MAKE-SEQUENCE SB-C::RESULT-TYPE (MIN (LENGTH #:G51))) SB-C::FUN
; #:G51)
;
; caught WARNING:
; The function (LAMBDA (X) :IN ADD-TEXT-PADDING) called by MAP-INTO returns NULL but CHARACTER is expected
; See also:
; The SBCL Manual, Node "Handling of Types"
;
; compilation unit finished
; caught 1 WARNING condition
ADD-TEXT-PADDING
*
Thus, it identifies the exact problem the author describes at the end.
- deleted 8y ago[deleted]
- fiddlerwoaroof 8y agoYeah, just because the language doesn’t force you to specify types, doesn’t mean that the compiler can’t infer them for you. What was most eye-opening for me about CL was just how helpful the compiler of a dynamically typed programming language can be.
- fjsolwmv 8y agoChecking dynamic types us not the work of a compiler. Checking types at compile time is static type checking. If a type is wholly dynamic (determined at runtime via something like casting to a type specified in user input), the compiler can't check it. (Also, type inference is orthogonal to static vs dynamic typing.)
- phoe-krk 8y ago> Checking dynamic types us not the work of a compiler. Checking types at compile time is static type checking. By your logic, Common Lisp is both dynamically and statically typed, because compile-time type checks are done by the compiler and warnings/errors are signaled whenever proper. More, if you declare (OPTIMIZE SPEED), the compiler is going to print a list of all places where it was unable to infer types on its own and where it expects help from the programmer in form of type declarations for individual variables.
- kazinator 8y agoAlso: why would padding be an optional keyword parameter (defaulting to nil!) when it's required to be a non-negative integer for the function to work correctly? Since it is required, it wants to be a required parameter.