Question

Common LISP function Give a trace of the function when the input is 5 (defun guess...

Common LISP function

Give a trace of the function when the input is 5

(defun guess (N)

(if (or (zerop N) (= N 1)) 1

(+ (guess (- N 1)) (guess (- N 2)))))

I have ran this code and with the following commands and i get this:

> (trace)

>(guess 5)

1. Trace: (GUESS '5)

1. Trace: (GUESS ==> 8

8

Is this correct or am i doing something wrong plz help.

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

Solution:

Let's do the trace here,

so N= 5

will call

guess(5)

initial condition if (or (zerop N) (= N 1)) will fail

and

(+ (guess (4)) (guess (3))))) => (Thi is equivlanet to guess(4) + guess(3))

again

(+ (guess (3)) (guess (2))))) => (Thi is equivlanet to guess(3) + guess(2))

again

(+ (guess (2)) (guess (1))))) => (Thi is equivlanet to guess(2) + guess(1))

here guess(1) will return 1

and guess(2) will be called

(+ (guess (1)) (guess (0))))) => (Thi is equivlanet to guess(1) + guess(0))

both the calls will return 1 and guess(2) will return 2

Now, let's get back

==============================================================

now we are returning the values and going backwards

(+ (guess (2)) (guess (1))))) => (Thi is equivlanet to guess(2) + guess(1)) will retun 3 (2+1)

(+ (guess (3)) (guess (2))))) => (Thi is equivlanet to guess(3) + guess(2)) will return 3 + 2 = 5

(+ (guess (4)) (guess (3))))) => (Thi is equivlanet to guess(4) + guess(3)) will return 5+3 = 8

You are absolutely right. :)

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Add a comment
Know the answer?
Add Answer to:
Common LISP function Give a trace of the function when the input is 5 (defun guess...
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
  • Programming C....... function code is clear but compile is wrong .. I input 8 and compiled...

    Programming C....... function code is clear but compile is wrong .. I input 8 and compiled 2 0 1 1 2 3 5 8 13.. look first number 2 is wrong. The correct answer 8 is 0 1 1 2 3 5 8 13 21.. Need fix code to compile correctly.. Here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[1000]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for (...

  • Python Problem Hello i am trying to finish this code it does not save guesses or...

    Python Problem Hello i am trying to finish this code it does not save guesses or count wrong guesses. When printing hman it needs to only show _ and letters together. For example if the word is amazing and guess=a it must print a_a_ _ _ _ not a_ _ _ _ and _a_ _ _ separately or with spaces. The guess has a maximum of 8 wrong guesses so for every wrong guess the game must count it if...

  • Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with...

    Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with output plesase. LISP Programming Assignment It is a good idea to start this assignment early; Lisp programming, while not inherently difficult, often seem somewhat foreign at first, particularly when it comes to recursion and list manipulation. This assignment is loosely based on material by Dr. Henri Casanova.   Problem #1 Define a function that takes two arguments and returns the greater of the two. Problem...

  • I am trying to create tkinter GUI for guess a number game.I am not able to...

    I am trying to create tkinter GUI for guess a number game.I am not able to complete it .Could someone guide me to complete.I am so confused about using tkinter in while loop . Below code is not working correctly .Could someone please help with this ? def get_binary_digits(dividend): binary_digits = [] while dividend != 0: quot = dividend // 2 remainder = dividend % 2 binary_digits.append(remainder) dividend = quot # The quotient becomes the new dividend. binary_digits.reverse() return binary_digits...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Please, I am evaluating an error term y = 5^(n+1)*(n+1) and wish to find the number...

    Please, I am evaluating an error term y = 5^(n+1)*(n+1) and wish to find the number of terms n required for ln (1.2) to yield an error < 10^-5. The manual computation gave me n > 5. But I need to come out with a MATLAB code with explanations and an output to confirm my manual answer. function error a = 1.2 - 1; n = 1; while (1/(n+1) * a^(n+l)) > 10e-6 n = n+1; end fprintf ('n =...

  • Need some guidance with the following problems as I am new to Java programming. Any assistance...

    Need some guidance with the following problems as I am new to Java programming. Any assistance is greatly appreciated. Thanks Using printf and specifiers for all print instructions: 1.            Code the following: a.            10% is stored in a variable called discount when the customer is a student; otherwise, it stores 0%. Code this if … else control structure using the conditional operator (ternary operator). The variables discount and student have already been declared. Assume student is a boolean variable. b.           ...

  • 7. #4. 18 Following is a recursive function which takes as input (argument) a vector $x$...

    7. #4. 18 Following is a recursive function which takes as input (argument) a vector $x$ and returns a scalar. Either by looking carefully at the function, or by running the function with a few simple input vectors and observing the returned values in each case, determine what the function is doing. 29 00- "**{r} 01 - myfun=function(x){ 02 - if(length(x)=-1){return(x) C03 - } else 84 return(x[1]+my funcx[-1])) 205 - ) 206 myfun(c(1:1000)) #here's what happens when it is applied...

  • 3. Write the function find sorted elt whose input is a vector sorted in increasing order...

    3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

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