Question

*Write a LISP program* Write a function that squares a number if it is odd and...

*Write a LISP program*

Write a function that squares a number if it is odd and positive, doubles it if it is odd and negative, and otherwise divides the number by 2.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

(defun func(num)

(cond

((and (oddp num)(plusp num)) (format t "~% Square of num: ~d " (* num num)))

((and (oddp num)(minusp num)) (format t "~% Value after multiplying by 2: ~d " (* 2 num)))

(t (format t "~% Value after dividing by 2: ~d " (/ num 2)))

)

)

(func 100)  

Code Screenshot:

Output:

screenshot with explanation and comments:

Add a comment
Know the answer?
Add Answer to:
*Write a LISP program* Write a function that squares a number if it is odd and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the function below in scheme/lisp programming language. Drracket Exercise: It is well known that n^2...

    Write the function below in scheme/lisp programming language. Drracket Exercise: It is well known that n^2 is equal to the sum of the first n odd numbers. For example, 16 = 4^2 = 7 + 5 + 3 + 1. Write a function that takes as input a natural number, n, and that returns the square of its input by adding the first n odd numbers. Your code may not contain delayed operations and must use accumulative recursion.

  • Python 3 Write a program that calculates the factorial of the greatest odd number in a...

    Python 3 Write a program that calculates the factorial of the greatest odd number in a sequence of positive numbers (separated by spaces). The program must receive sequences of integers, one per line. The end of the input cases will be indicated by the number -1. For each line the program must print the factorial of the greatest odd number in each sequence. Note that for this question, there is always a greatest positive odd number in each line. Sample...

  • 1. Write a program that takes a number as input and check whether the number is...

    1. Write a program that takes a number as input and check whether the number is positive, negative or zero. 2. Write a C++ program that prompts the user to enter a number and checks whether the entered number is even or odd. 3.  Using switch-case statement write a C++ program that prompts the user to enter 1, 2 or 3 and display "Red" if selection is 1, "Yellow" if selection is 2, or "Green" if selection is 3. 4. Write...

  • Java Write a program that will look for two perfect squares, both odd, that will add...

    Java Write a program that will look for two perfect squares, both odd, that will add together to make another perfect square. Ex--->3^2+4^2=5^2 , 5^2+12^2=13^2 , 6^2+8^2=10^2 , 7^2+242^=25^2

  • Lisp program count-of (symbol list): Write a function named count-of that takes two parameters, a symbol...

    Lisp program count-of (symbol list): Write a function named count-of that takes two parameters, a symbol and a list. Count the number of instances of x in the list. Do not count the number of x in any sub-lists within the list. Test: count-of 'a '(a 'a(a c) d c a). Result: 2 trim-to (symbol list): Write a function named trim-to that takes a symbol and list as parameters. Return a new list starting from the first occurrence of the...

  • In HTML write a program that calculates and displays all factors of a number with a...

    In HTML write a program that calculates and displays all factors of a number with a function. A factor is any number that divides into a number evenly. For examples: Factors of 20 are 1,2,4,5,10,20 For the program: Prompt the user for a number or use an input Call a function to calculate all factors using loops and conditionals and modulus Display all factors to the page Bonus +5 If the factor is Even : Print it in Green If...

  • 2. Write a LISP function COUNTLETS which takes a list and returns the number of top-level...

    2. Write a LISP function COUNTLETS which takes a list and returns the number of top-level alphabetic atoms in the list. For example: COUNTLETS ( (A 2) B 3 C4 (D 5))) Returns the value 2, the letters A and D are not at the top level

  • Lisp Scheme Write a procedure in Lisp Scheme called charflip that takes a string as a...

    Lisp Scheme Write a procedure in Lisp Scheme called charflip that takes a string as a parameter and returns a copy of this string, flipping the case of every character in an odd position (index 1, 3, 5, etc..). I suggest that you do this recursively. This means cons, car and cdr will come in handy, as will these functions: • char-upper-case? • char-downcase • char-upcase • list->string • string->list Here’s this function in action. scheme@(guile-user)> (charflip "hatburg") $16 =...

  • Write a function to find the smallest odd number in a list. The function takes a...

    Write a function to find the smallest odd number in a list. The function takes a list of numbers (each number is greater than or equal to 0) as an input. The function outputs a tuple, where the first number in the tuple is the smallest odd number and the second number in the tuple is the number of occurrences of that number. For example, if the input is [1, 4, 7, 3, 5, 2, 1, 3, 6] then the...

  • Question 2 (10 pts) Write a Lisp program that calculates the length of a three-dimensional vector...

    Question 2 (10 pts) Write a Lisp program that calculates the length of a three-dimensional vector V: (x y z) based on the following formula. vector - length (V)- Vx2 + y2+z2 Implement your program in two different ways: 1- Using anonymous function (lambda) 2- Without applying the anonymous function Which method is more efficient in terms of memory allocation?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT