August 19, 2024

P22 - Create a list containing all integers within a given range

If first argument is smaller than second, produce a list in decreasing order.

Example:* (range 4 9)(4 5 6 7 8 9)

lisp

;;; Function that generates a list containing all integers in ;;;
;;; a given interval. If the beginning is less than the end, it generates ;;;
;;; a descending list ;;;

(defun range (ini end)
    (if (> ini fim)
        ;; if the list is from a larger number to a smaller ;;
        (if (eql ini fim)
            (cons fim nil)
            (cons ini (range (- ini 1) end))
        
        ;; if the list is from a smaller number to a larger ;;
        (if (eql ini fim)
            (cons fim nil)
            (cons ini (range (+ ini 1) end)))
Be first to comment
Leave a reply