Discussion:
HW 9 - #2
(too old to reply)
Hamilton Richards
2004-11-15 03:37:17 UTC
Permalink
Dr. Richards,
Can we use global variables and/or helper functions in our
tail recursive multiplication function?
Yes, that's expected.
It's not immediately obvious to me how to write a tail recursive
multiplication function w/ only 2 parameters...
Nor is it to me.

--HR
--
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138 Austin, Texas 78712-0233
***@cs.utexas.edu ***@swbell.net
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
Nathaniel Robinson
2004-11-16 08:49:05 UTC
Permalink
i just wrote a helper function that had a different set of parameters...

-nathaniel
Post by Hamilton Richards
Dr. Richards,
Can we use global variables and/or helper functions in our
tail recursive multiplication function?
Yes, that's expected.
It's not immediately obvious to me how to write a tail recursive
multiplication function w/ only 2 parameters...
Nor is it to me.
--HR
--
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138 Austin, Texas 78712-0233
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
Havoc
2004-11-16 08:55:46 UTC
Permalink
Correct me if I'm wrong, but isn't the following a tail recursive function?

int rprod ( int a, int b ) {

if (b < 0)

return - (rprod ( a, -b ));

else if (b == 1)

return a;

else

return a + rprod ( a, b - 1);

}
Post by Hamilton Richards
Dr. Richards,
Can we use global variables and/or helper functions in our tail recursive
multiplication function?
Yes, that's expected.
It's not immediately obvious to me how to write a tail recursive
multiplication function w/ only 2 parameters...
Nor is it to me.
--HR
--
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138 Austin, Texas 78712-0233
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
Havoc
2004-11-16 09:49:12 UTC
Permalink
Sorry, my bad.
Post by Havoc
Correct me if I'm wrong, but isn't the following a tail recursive function?
int rprod ( int a, int b ) {
if (b < 0)
return - (rprod ( a, -b ));
else if (b == 1)
return a;
else
return a + rprod ( a, b - 1);
}
Post by Hamilton Richards
Dr. Richards,
Can we use global variables and/or helper functions in our tail
recursive multiplication function?
Yes, that's expected.
It's not immediately obvious to me how to write a tail recursive
multiplication function w/ only 2 parameters...
Nor is it to me.
--HR
--
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138 Austin, Texas 78712-0233
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
Nathaniel Robinson
2004-11-16 09:54:54 UTC
Permalink
i do not believe it is. according to slide 60, you cannot have it return
something and add it to something else. in essence, you are leaving a trail
of "a +" that have to be calculated after the last call returns. his notes
state that this is incorrect. Wikipedia states that it is a type of
recursion that can be transformed into iteration, which i don't believe
yours can. a webpage on the haskell.org site says that another definition is
" A call is tail-recursive if nothing has to be done after the call
returns", which i think is clearer.

-nathaniel

(sorry for the long-winded answer, but i wanted to make sure for myself, and
needed lots of proof)
Post by Havoc
Correct me if I'm wrong, but isn't the following a tail recursive function?
int rprod ( int a, int b ) {
if (b < 0)
return - (rprod ( a, -b ));
else if (b == 1)
return a;
else
return a + rprod ( a, b - 1);
}
Post by Hamilton Richards
Dr. Richards,
Can we use global variables and/or helper functions in our tail recursive
multiplication function?
Yes, that's expected.
It's not immediately obvious to me how to write a tail recursive
multiplication function w/ only 2 parameters...
Nor is it to me.
--HR
--
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138 Austin, Texas 78712-0233
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
Ted Alan Wrenn Jr.
2004-11-16 16:53:03 UTC
Permalink
There is a pretty neat C/C++ convention that allows for default parameters
to be passed into a function.
e.g.
int rprod(int a, int b, int acc=0){
...
}

then you can call rprod like this:
int c = rprod(100,200);

That way you don't need a helper function.

TJ
Post by Hamilton Richards
Dr. Richards,
Can we use global variables and/or helper functions in our
tail recursive multiplication function?
Yes, that's expected.
It's not immediately obvious to me how to write a tail recursive
multiplication function w/ only 2 parameters...
Nor is it to me.
--HR
--
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138 Austin, Texas 78712-0233
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
Nathaniel Robinson
2004-11-16 17:06:09 UTC
Permalink
crap, i forgot about default initialization. either way works though. did he
mention this method in the notes?

-nathaniel

ps. i suggest reading this webpage by Prof. Downing, and the provided
examples, before doing more than just this assignment using this strategy
http://www.cs.utexas.edu/users/downing/cs378/Elements/DefaultArguments.html
Post by Ted Alan Wrenn Jr.
There is a pretty neat C/C++ convention that allows for default parameters
to be passed into a function.
e.g.
int rprod(int a, int b, int acc=0){
...
}
int c = rprod(100,200);
That way you don't need a helper function.
TJ
Post by Hamilton Richards
Dr. Richards,
Can we use global variables and/or helper functions in our
tail recursive multiplication function?
Yes, that's expected.
It's not immediately obvious to me how to write a tail recursive
multiplication function w/ only 2 parameters...
Nor is it to me.
--HR
--
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138 Austin, Texas 78712-0233
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
Ted Alan Wrenn Jr.
2004-11-16 17:07:46 UTC
Permalink
did he mention this method in the notes?
I don't think so, i just remember it from highschool.
tj
crap, i forgot about default initialization. either way works though. did he
mention this method in the notes?
-nathaniel
ps. i suggest reading this webpage by Prof. Downing, and the provided
examples, before doing more than just this assignment using this strategy
http://www.cs.utexas.edu/users/downing/cs378/Elements/DefaultArguments.html
Post by Ted Alan Wrenn Jr.
There is a pretty neat C/C++ convention that allows for default parameters
to be passed into a function.
e.g.
int rprod(int a, int b, int acc=0){
...
}
int c = rprod(100,200);
That way you don't need a helper function.
TJ
Post by Hamilton Richards
Dr. Richards,
Can we use global variables and/or helper functions in our
tail recursive multiplication function?
Yes, that's expected.
It's not immediately obvious to me how to write a tail recursive
multiplication function w/ only 2 parameters...
Nor is it to me.
--HR
--
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138 Austin, Texas 78712-0233
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
Loading...