'Augdentity Lisp Implementation

Given positive ints r and c indicating number of rows and columns, create a 2D list that represents the "augmented identity matrix" with that dimension: It's the r x c matrix of all zeroes, except the main diagonal is all ones.

NOTE: I am not allowed to use global variables.

Function format:

(defun augdentity (r c) … )

Sample runs:

* (augdentity 3 3)
((1 0 0) (0 1 0) (0 0 1))
* (augdentity 3 5)
((1 0 0 0 0) (0 1 0 0 0) (0 0 1 0 0))
* (augdentity 5 3)
((1 0 0) (0 1 0) (0 0 1) (0 0 0) (0 0 0))
* (augdentity 2 2)
((1 0) (0 1))

This is the Haskell version of the function but I couldn't figure out how to do it in Lisp

augdentity :: Int -> Int -> [[Int]]
augdentity r c = [[if row == col then 1 else 0 | col <- [1 .. c]] | row <- [1 .. r]]


Solution 1:[1]

Well, you can translate that pretty directly using loop:

(loop :for row :below r
      :collect (loop :for col :below c
                     :collect (if (= row col) 1 0)))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Svante