tag:blogger.com,1999:blog-18677687.post6620733956107771242..comments2024-03-27T15:13:24.764-04:00Comments on Computer Science Teacher: Am I Doing It All Wrong?Alfred Thompsonhttp://www.blogger.com/profile/05575057876858763822noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-18677687.post-91469667714449216122017-10-18T12:09:16.986-04:002017-10-18T12:09:16.986-04:00Well, the choice of recursion is not up to you alo...Well, the choice of recursion is not up to you alone, right? It's a bit on how the language is implemented in the compiler and what the Assembly (really machine code) looks like. This is why it can be dangerous to use FP techniques in non FP hybrid languages that don't bias to FP -- sure it might run/compile but that doesn't mean it'll run fast.<br /><br />Same opposite way. OCaml is first and foremost a FP language that also supports OO and imperative. You'll generally get better performance (better optimized machine code) by developing in the predominant paradigm (in OCaml's case FP). You could bend C to your will and do some FP but you might not like the machine code that results.Anonymoushttps://www.blogger.com/profile/12196780270108374854noreply@blogger.comtag:blogger.com,1999:blog-18677687.post-75901114339511200002017-10-18T11:52:10.663-04:002017-10-18T11:52:10.663-04:00Recursion is an important concept to understand an...Recursion is an important concept to understand and a good teaching tool but I use it only as a last resort and I see no good reason to use recursion for either reversing a list or verifying if a string is a palindrome.<br /><br />Even the classic Towers of Hanoi problem becomes simpler with iteration.<br /><br />Repeat until done:<br />1. Move the smallest disk one tower to the right<br />2. Make the only other legal move possible<br /><br />How simple is that?!<br /><br />Regarding methods, as code becomes more complex, one popular new paradigm is test-driven development (which makes automated regression testing possible) and with TDD, I/we break methods based on that are considered to be the testable units.<br /><br />In OOP, the obvious class for this example is Circle, with Area and Circumference being class-level methods (or properties), operating on the class-level radius, not a parameter.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-18677687.post-24951689270956953842017-10-18T09:24:46.874-04:002017-10-18T09:24:46.874-04:00Separating functions into small pieces with single...Separating functions into small pieces with single lines of code is probably OK in small programs, but when you get into larger suites of code/projects, it becomes critical, even for calculations like area of a circle, which will never change. <br /><br />In professional software development you'd want to provide a unit test for the GetArea() and GetCircumference() functions. <br /><br />If you embed your one-line calculation into a 20-line function, you must navigate the 20 lines of your function to test your one-line calculation, which is difficult. You now have a function that does multiple things, and testing those multiple things together, including failure cases, is very complicated. If you have a one-line function, you can write a unit test(s) directly against it with ease. <br /><br />It's an art, and something many professional software developers don't do well. <br /><br />As an exercise, you might want to ask your students to create a program with small functions, a few lines each, and then trade them back and forth. If a function can't be traded with another student that performs a single operation, then that's an example of a function that does too much. For a professional world example, with unit tests, if we find ourselves going to great lengths to test a function that does multiple things, then that's a signal to us that the function should be broken up.<br /><br />Don Brueyhttps://www.blogger.com/profile/00786239905109401570noreply@blogger.comtag:blogger.com,1999:blog-18677687.post-12632319389281633302017-10-18T08:07:54.630-04:002017-10-18T08:07:54.630-04:00Caveat: I have not done a ton of Java.
The genera...Caveat: I have not done a ton of Java.<br /><br />The general way in FP to approach a palindrome is 1) reverse the list, and 2) compare the original with the reverse. <br /><br />Here's an adaption from OCaml's 99 problems (which is the same 99 problems from Lisp, which itself was based on Prolog) assuming the string has already been converted to a list of chars. The essential lesson here, as is often the case in FP, is to use recursion, in this case within a subfunction:<br /><br /><br />let rev list =<br /> let rec aux acc = function<br /> | [] -> acc<br /> | h::t -> aux (h::acc) t in<br /> aux [] list;;<br /><br />let is_palindrome list =<br /> list = rev list;;<br /><br /><br />Again, for simplicity, I've not included a str_to_list function but - surprise - it would also use recursion.<br /><br />So, my question would is there a way to do something similar in Java using methods akin to this approach?<br /><br />See http://ocaml.org/learn/tutorials/99problems.html, problems 5 and 6, for more context. Anonymoushttps://www.blogger.com/profile/12196780270108374854noreply@blogger.com