Strings & numbers in Lisp
Joining strings and dividing numbers, where Lisp keeps exact answers by default.
;; concatenate wants its result type spelled out before the pieces
(defparameter *artist* "brian eno")
(defparameter *track* "an ending")
(defun title-line (artist track)
(concatenate 'string
(string-upcase artist)
" - "
(string-capitalize track)))
;; / keeps an exact ratio, floor truncates, float widens to a decimal
(defparameter *seconds* 262)
(format t "~a~%" (title-line *artist* *track*))
(format t "exact minutes: ~a~%" (/ *seconds* 60))
(format t "whole minutes: ~a with ~a left over~%"
(floor *seconds* 60) (mod *seconds* 60))
(format t "rounded off: ~,2f~%" (float (/ *seconds* 60)))
How it works
title-lineglues four pieces withconcatenate, which needs the result type'stringup front.(string-upcase artist)and(string-capitalize track)recase without touching the originals.(/ *seconds* 60)returns the exact ratio, whilefloorandmodsplit it andfloatgives a decimal.
Keywords and builtins used here
concatenatedefparameterdefunfloatfloorformatmod
The run, in numbers
- Lines
- 18
- Characters to type
- 597
- Tokens
- 86
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 69 seconds.
Step 3 of 3 in Values & bindings, step 3 of 27 in Language basics.