Skip to content

Write a program in Lisp to implement decision tree

AIM: Write a program in Lisp to implement decision tree.

Post by: Sohrab Vakharia

(Enter a decimal string of repeated entries of suitable length. Apply binary decision tree to find repetitions of each decimal digit in the string.)

Source Code:

(defun MyTree(mylist)
    (setq CntrArr 0)
    (setq ResArr (make-array ‘(1 10)))
    (loop
        (when (equal CntrArr 10) (return))
        (setf (aref ResArr 0 CntrArr) 0)
        (incf CntrArr)
    )
    (loop
        (when (equal (first mylist) nil) (return))
        (if (<= (first mylist) (/ 10 2))
            (if (< (first mylist) ( / 5 2))
                (if (< (first mylist) ( / 3 2))
                    (if (< (first mylist) ( / 1 2))
                        (setf (aref ResArr 0 0) (incf (aref ResArr 0 0)))
                        (setf (aref ResArr 0 1) (incf (aref ResArr 0 1)))
                    )
                    (setf (aref ResArr 0 2) (incf (aref ResArr 0 2)))
                )
                (if (< (first mylist) ( / 9 2))
                    (if (< (first mylist) ( / 7 2))
                         (setf (aref ResArr 0 3) (incf (aref ResArr 0 3)))
                        (setf (aref ResArr 0 4) (incf (aref ResArr 0 4)))
                    )
                    (setf (aref ResArr 0 5) (incf (aref ResArr 0 5)))
                )
            )
            (if (< (first mylist) ( / 15 2))
                (if (< (first mylist) ( / 13 2))
                    (if (< (first mylist) ( / 12 2))
                        (setf (aref ResArr 0 5) (incf (aref ResArr 0 5)))
                        (setf (aref ResArr 0 6) (incf (aref ResArr 0 6)))
                    )
                    (setf (aref ResArr 0 7) (incf (aref ResArr 0 7)))
                )
                (if (< (first mylist) ( / 19 2))
                    (if (< (first mylist) ( / 17 2))
                         (setf (aref ResArr 0 8) (incf (aref ResArr 0 8)))
                        (setf (aref ResArr 0 9) (incf (aref ResArr 0 9)))
                    )
                    (setf (aref ResArr 0 10) (incf (aref ResArr 0 10)))
                )
            )
        )
    (setq mylist (rest mylist))
)   
   (print ResArr)
   
)

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!