Hello World
(format t "Hello, world!~%")
Run with a Common Lisp implementation such as SBCL:
sbcl --script main.lisp
This prints a line in Common Lisp using format, which is a flexible function for building and writing text.
Variables
(defparameter *name* "Dan")
(defparameter *count* 1)
(defparameter *active* t)
These global parameters hold simple values you can reuse across expressions.
Functions
(defun greet (name)
(format nil "Hello, ~A!" name))
(greet "world")
This defines a function and then calls it. Lisp keeps the function shape compact, even for small reusable helpers.
Lisp Add
(defun add (param1 param2)
(+ param1 param2))
This just adds the two input numbers with the language’s normal arithmetic and returns the sum.
Lisp Add Border
(defun add-border (picture)
(let* ((width (length (first picture)))
(border (make-string width :initial-element #\*))
(wrapped (mapcar (lambda (row) (format nil "*~A*" row))
(cons border picture))))
(append wrapped (list (make-string (+ width 2) :initial-element #\*)))))
This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.
Lisp Adjacent Elements Product
(defun adjacent-elements-product (input-array)
(let ((vec (coerce input-array 'vector))
(max-product most-negative-fixnum))
(dotimes (i (1- (length vec)))
(setf max-product (max max-product (* (aref vec i) (aref vec (1+ i))))))
max-product))
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Lisp Almost Magic Square
(defun almost-magic-square (a)
(let ((m (make-array '(3 3)))
(row-sum (make-array 3 :initial-element 0))
(col-sum (make-array 3 :initial-element 0))
(max-sum 0))
(loop for idx from 0 below 9
for val in a
do (setf (aref m (floor idx 3) (mod idx 3)) val))
(dotimes (i 3)
(dotimes (j 3)
(incf (aref row-sum i) (aref m i j))
(incf (aref col-sum i) (aref m j i))))
(dotimes (k 3)
(setf max-sum (max max-sum (aref row-sum k) (aref col-sum k))))
(let ((i 0) (j 0))
(loop while (and (< i 3) (< j 3))
do (let ((diff (min (- max-sum (aref row-sum i))
(- max-sum (aref col-sum j)))))
(incf (aref m i j) diff)
(incf (aref row-sum i) diff)
(incf (aref col-sum j) diff)
(when (= (aref row-sum i) max-sum) (incf i))
(when (and (< j 3) (= (aref col-sum j) max-sum)) (incf j)))))
(loop for idx from 0 below 9
collect (aref m (floor idx 3) (mod idx 3)))))
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Lisp Are Equally Strong
(defun are-equally-strong (your-left your-right friends-left friends-right)
(and (= (max your-right your-left) (max friends-left friends-right))
(= (min your-left your-right) (min friends-right friends-left))))
This compares each person’s strongest and weakest arm. If both pairs match, the result is true.
Lisp Array Change
(defun array-change (a)
(let ((vec (coerce a 'vector))
(min 0))
(dotimes (k (1- (length vec)))
(when (>= (aref vec k) (aref vec (1+ k)))
(let ((dif (+ (- (aref vec k) (aref vec (1+ k))) 1)))
(incf (aref vec (1+ k)) dif)
(incf min dif))))
min))
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
Lisp Array Maximal Adjacement Difference
(defun array-maximal-adjacent-difference (a)
(let* ((vec (coerce a 'vector))
(c (length vec))
(dif 0))
(loop for i from 1 below (1- c)
do (setf dif (max dif
(abs (- (aref vec i) (aref vec (1- i))))
(abs (- (aref vec i) (aref vec (1+ i)))))))
dif))
This checks the gap between each pair of neighbors and returns the largest difference.
Lisp Binary Gap
(defun binary-gap (n)
(let* ((bits (write-to-string n :base 2))
(trimmed (string-trim "0" bits))
(groups (split-on-char trimmed #\1))
(gap 0))
(dolist (zero groups)
(setf gap (max gap (length zero))))
gap))
(defun split-on-char (s ch)
(loop with start = 0
for pos = (position ch s :start start)
collect (subseq s start pos)
while pos
do (setf start (1+ pos))))
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.
Lisp Bracket
(defun bracket (s)
(let ((stack '()))
(loop for v across s
do (cond
((char= v #\))
(if (or (null stack) (char/= (pop stack) #\())
(return-from bracket 0)))
((char= v #\])
(if (or (null stack) (char/= (pop stack) #\[))
(return-from bracket 0)))
((char= v #\})
(if (or (null stack) (char/= (pop stack) #\{))
(return-from bracket 0)))
(t (push v stack))))
(if (null stack) 1 0)))
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.