Skip to article frontmatterSkip to article content

Lisp Exercises and Solutions (Part 1)

Hello 1

Solution to Hello 1
(defun hello (msg)
  (format t "Hello, ~A!" msg))

Remove 1

Solution 1

Solution to Remove 1

Remove doesn’t change the original list. It just returns a new list with that element removed.

We can use setf and bind the returned list back to the original variable/name:

(setf lst (remove 'c lst))

This is somewhat similar to Go’s append() (and a few other languages as well), which always returns the new collection, to which we more often than not reassign to the same reference:

package main

import "fmt"

func main() {
  nums := []int8{1, 2, 3}
  nums = append(nums, 4)
  fmt.Printf("%#v", nums)
  //=> []int8{1, 2, 3, 4}
}

However, if we are striving to work in a functional functional style and treat data structures as immutable, then we should avoid reassigning.