blendedMultiply (x, y)
if (|x| and |y| are both less than THRESHOLD)
return gradeSchoolMultiply(x, y)
else
split x into halves xL and xR
split y into halves yL and yR
P1 = blendedMultiply(xL, yL)
P2 = blendedMultiply(xR, yR)
P3 = blendedMultiply(xL+xR, yL+yR)
return P1*2n + (P3 - P1 - P2)*2n/2 + P2
The first thing that jumps out at me is that there is a lot of English here that would be difficult to parse; specifically the contents of the if statement, and the two split lines. Other than that, this is pretty similar to "real" code. blendedMultiply is clearly a function with two arguments (untyped, which won't be a problem translating to Python, but would be going to a typed language like C++ or Java). The assignments of P1, P2, and P3 look pretty straightforward. And all of the return statements look pretty straightforward.
I think if I were parsing this, I would look for camel-case words followed by parenthesis for function definition and function calls (could also be a word made up of letter, numbers, and underscores, like blended_multiply). I suppose I could determine whether the function was being defined or called by first looking to see if the name already existed (probably a function call then), or by looking to see if the arguments already existed (if not, then it's probably a function definition). Then I could add the "def" and ":" as needed.
For the if statement, I could probably write | | into the language as representing the magnitude of a variable, which could be changed to len(x) in Python. I think it would be somewhat easy to check for "less than" and replace with "<" (but maybe it's not as trivial as I think). "Are both" could be dropped, since there's already an "and". Although, I would need to rearrange the structure a little: "if(len(x) < THRESHOLD and len(y) < THRESHOLD):". I think capitalized words are generally considered to be constants, so I could just look for a defined constant somewhere.
I'm really not sure how to handle the split lines at all... the last return statement is easy, though. After all, math is math.
My professor indented his code in a Pythonic way, which is also how I write my pseudocode (it's way more readable that way). I like the idea of using whitespace to parse my language, but I have friends who complain about Python's required indentation. I'm thinking that newlines will be required, but indentation will be optional (but we'll see).
CONCLUSION:
- Functions should be defined as camel-case or underscore-separated words followed by a parenthesis with 0 or more arguments.
- Functions are called the same way that they are defined.
- if statements should contain their conditionals in parenthesis.
- Assignment should be done with one equals operator
- Math is math (although exponents might be tricky... Python uses ** instead of ^)
- Arithmetic symbols can also be represented by their English label (e.g. "less than" vs. "<" or "mod"/"modulus"/"modulo" vs "%").
- Constants should be named with all caps.
No comments:
Post a Comment