+1 (315) 557-6473 

Flatten a List And Find The Largest Atom Using LISP Assignment Solution.


Instructions

Objective
Write a program to flatten the list in LISP, write code to find the largest atom in the list.

Requirements and Specifications 

To write a LIPS assignment follow the steps. All of them are lisp code
  1. Function 1] Flat-Max Write the code for a flat-max function which takes a single list of numbers and returns the largest number. > (flat-max '(11 40 25)) 40
  2. Function 2 Full-Max Write the code for a full-max function which takes a nested list of numbers and returns the largest number. > (full-max '((11 40) 25)) 40
  3. Function 3 Flat-Rev Write the code for a flat-rev function which takes a single list and returns the reversed list. > (flat-rev '(11 40 25)) (25 40 11)
  4. Function 4 Full-Rev Write the code for a full-rev function which takes a nested list and returns the reversed list. > (full-rev '((11 40) 25)) (25 (40 11))
  5. Function 5 Flatten Write the code for a flatten function that takes a nested list and returns a flattened version of the list. > (flatten '((11 40) 25)) (11 40 25)
  6. Create two lisp functions - (apply-sub expr sub) (compose sub1 sub2)
Screenshots of output
Code to flatten list find largest atom using LISP

Source Code

; Function that takes a single list of numbers and returns the largest number.

(defun flat-max (lst)

    (if (null (cdr lst)) ; if there's only one element

        ; return the first element

        (car lst)

        ; else, return the max of the first element and the list tail

        (max (car lst) (flat-max (cdr lst)))

    )

)

; Function that takes a nested list of numbers and returns the largest number.

(defun full-max (lst)

    (let

        ; get first element in list

        ((first-elem

            (if (listp (car lst)) ; if the element is a list

                (full-max (car lst)) ; first element is the max of the list

                (car lst) ; else it's the first element

            )

        ))

        (if (null (cdr lst)) ; if there's only one element

            ; return the first element

            first-elem

            ; else, return the max of the first element and the list tail

            (max first-elem (full-max (cdr lst)))

        )

    )

)

; Function that takes a single list and returns the reversed list.

(defun flat-rev (lst)

    (if (null lst) ; if empty list

        lst ; return the list

        ; else, return the inverted list tail and append the first element at the end

        (append (flat-rev (cdr lst)) (list (car lst)))

    )

)

; Function that takes a nested list and returns the reversed list.

(defun full-rev (lst)

    (if (null lst) ; if empty list

        lst ; return the list

        ; else, return the inverted list tail and append the first element at the end

        (append (full-rev (cdr lst))

            (if (listp (car lst)) ; if element is a list

                (list (full-rev (car lst))) ; reverse list before appending

                (list (car lst)) ; else, append element as list

            )

        )

    )

)

; Function that takes a nested list and returns a flattened version of the list.

(defun flatten (lst)

    (if (null lst) ; if empty list

        lst ; return the list

        (if (listp (car lst)) ; else, if first element is a list

            ; if it's a list, append flattened tail list at end of first list

            (append (flatten (car lst)) (flatten (cdr lst)))

            ; if not a list, add first element to start of flattened tail list

            (cons (car lst) (flatten (cdr lst)))

        )

    )

)

;???? Apply function sub to expression

(defun apply-sub (expr sub)

    (funcall sub expr) ; evaluate function sub using the given expr value

)

;??? Apply function sub1 after function sub2

(defun compose (sub1 sub2)

    ; create a new function that evaluates sub2 and uses the result to evaluate sub1

    (lambda (&rest x) (funcall sub1 (apply sub2 x)))

)