typestar

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

  1. title-line glues four pieces with concatenate, which needs the result type 'string up front.
  2. (string-upcase artist) and (string-capitalize track) recase without touching the originals.
  3. (/ *seconds* 60) returns the exact ratio, while floor and mod split it and float gives a decimal.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Values & bindings, step 3 of 27 in Language basics.

← Previous Next →