×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Program to Implement Basic Functions Using LISP Assignment Solution

July 01, 2024
Megan Talbot
Megan Talbot
🇺🇸 United States
Lisp
Meet Megan Talbot, a visionary Lisp wizard with a decade of metaprogramming mastery, sharing insights and guiding developers into the art of code enchantment.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Familiarize yourself with OCaml's pattern matching; it simplifies handling recursive data structures like lists and trees, making your code concise and easier to debug.
News
In 2024, Girls Who Code introduced a Data Science + AI track in their free summer programs for high school students, fostering skills in cybersecurity and creative coding​

Instructions

Objective 
Write a program to implement basic functions using the LISP programming language.

Requirements and Specifications

Function intmax to return the greatest number among two integers.
Function dup which takes an element and returns a tuple with the element duplicated .
Recursive function to add up all numbers from 1 to a given upper limit.
Function backwards to return the reverse order list of a given input list but no sublists .
Function totalbackwards to return total reverse order list .
Function with algorithm to detect palindrome.
Screenshots of output
implementing functions with basic LISP
Source Code
;;1
(defun intmax (x y)
    (cond
      ((> x y) x)
      (T y)))
(print (intmax 2 4))
(print (intmax -1 -2))
(print (intmax 0 0))
(print (intmax -2 2))
;;2
(defun dup (elem)
  (cons elem (list elem)))
(print (dup 5))
(print (dup '(1 2 3)))
(print (dup 'a))
(print (dup nil))
;;3
(defun add-nums (x)
  (cond
    ((<= x 1) 1)
    (t (+ x (add-nums (- x 1))))))
(print (add-nums 1))
(print (add-nums 5))
(print (add-nums 10))
(print (add-nums -1))
;;4
(defun backwards (lst)
  (cond
    ((null lst) lst)
    (t (append (backwards (cdr lst)) (list (car lst))))))
(print (backwards '(a b c h i)))
(print (backwards nil))
(print (backwards '(a (b c) (h i))))
(print (backwards '(a)))
;;5
(defun totalbackwards (lst)
  (cond
    ((null lst)
      lst)
    ((listp (car lst))
      (append (totalbackwards (cdr lst)) (list (totalbackwards (car lst)))))
    (t
      (append (totalbackwards (cdr lst)) (list (car lst))))))
(print (totalbackwards '(a (b c) ((l k (t)) h i))))
(print (totalbackwards nil))
(print (totalbackwards '((a b) ((c d) e))))
(print (totalbackwards '(a b () c d)))
;;6
(defun palindrome (lst)
  (equal lst (backwards lst)))
(print (palindrome '(r o t o r)))
(print (palindrome '(r a c e c a r)))
(print (palindrome '(p a l i n d r o m e)))
(print (palindrome '(r a c e c a r s)))

Related Samples

Explore Lisp Assignment Samples: Delve into our curated selection showcasing Lisp solutions covering recursion, list manipulation, and functional programming paradigms. From basic exercises to complex algorithms, each sample offers insights into Lisp programming. Strengthen your Lisp skills and excel in functional programming with our comprehensive examples.