Quiz 18 in CSCE 330 Fall 2007 - Recursive Function Error Identification, Quizzes of Computer Science

The solution to quiz 18 in the csce 330 fall 2007 course, where students were asked to identify the error in a recursive function intended to reverse a list. The function in question breaks one of the two rules of recursion: the inductive step. Instead of making a recursive call with smaller arguments, it calls the function with the same size argument, leading to an infinite recursion.

Typology: Quizzes

Pre 2010

Uploaded on 09/02/2009

koofers-user-fp9
koofers-user-fp9 🇺🇸

8 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCE 330 Fall 2007
Quiz 18
Assigned Tuesday 07-12-04
A recursive function has two parts, the basis and the inductive step.
1. The basis computes the result for sufficiently small arguments, without
making any recursive call.
2. The inductive step calls the function recursively, with smaller arguments.
The following recursive function (which is intended to reverse a list and which
is almost identical to the correct version of naive reverse that we discussed in
class on Thursday) breaks one of these two rules. Which one? In which way?
fun reverse(L) =
if L = nil then nil
else reverse(L) @ [hd(L)];
Answer: The second (because the recursive call does not have a smaller
argument)
1

Partial preview of the text

Download Quiz 18 in CSCE 330 Fall 2007 - Recursive Function Error Identification and more Quizzes Computer Science in PDF only on Docsity!

CSCE 330 Fall 2007

Quiz 18

Assigned Tuesday 07-12-

A recursive function has two parts, the basis and the inductive step.

  1. The basis computes the result for sufficiently small arguments, without making any recursive call.
  2. The inductive step calls the function recursively, with smaller arguments.

The following recursive function (which is intended to reverse a list and which is almost identical to the correct version of naive reverse that we discussed in class on Thursday) breaks one of these two rules. Which one? In which way?

fun reverse(L) = if L = nil then nil else reverse(L) @ [hd(L)];

Answer: The second (because the recursive call does not have a smaller argument)