|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On September 09 2011 03:37 icystorage wrote:Show nested quote +On September 08 2011 23:39 Kambing wrote:On September 08 2011 21:57 icystorage wrote:i would like to thank the people who helped my on my last problem, yes it was working properly, my bad D: i have another problem :< im stuck and i hope you guys can help it compiles correctly but gives an error when i use it > myprod n m = if m == 0 then 0 else (myprod n (m - 1)) + n
> myprod2 n m = if n == 1 then m else if n mod 2 == 1 then (myprod (n div 2) (m + m)) + m else (myprod (n div 2) (m + m))
this is a multiplication function. myprod is my old fcn, its very slow so i tried improving it by using binary multiplication. myprod2 is what i have come up but it returns an error even though it compiles properly, the error is no instance for (num ((a0->a0->a0->a10->a20)) arising from the literal '5' possible fix: add an instance declaration for (num ((a0->a0->a0->a10->a20)) and my problem is i dont know how to fix it =/ That's a compiler error actually. The problem is that when you use standard function names as infix operators, you need to encase them in backquotes, e.g., n `div` 2 is equivalent to div 2 n. Writing 1 div 2 is in effect saying "apply the div function (of type Integral a => a -> a -> a) to the constant 1 (of type Num a => a) and then take the result and apply the constant 2". Clearly 1 is not a function so the whole thing fails to type check. EDIT: because of typeclasses, Haskell can give 1 div 2 a type 1 div 2 :: (Num a1, Num ((a -> a -> a) -> a1 -> t), Integral a) => t
Because it tries to unify the type of 1 with a function type that can take the div function as an argument. However, it fails when the typechecker tries to find an instantiation of the type variable a that satisfies the constraint. Hence the nasty compiler error that you see. *holds hand* thank you my friend... thank you... how could i be so stupid ._.
No problem friend. Your example surprised a pair of Haskell hackers for a few minutes. It also completely stumped them why you ever want to treat a number as a function. It took a former Haskell design committee member a few minutes to point out that with sufficient typeclass hackery, you can make the following Haskell fragment
10 PRINT "Hello world!" 20 PRINT "This is an embedded BASIC program written in Haskell"
thanks to the Embedded BASIC Haskell package.
|
Okay, my pointer/reference wrapper is nearing completion. Passed most of my basic little tests, but I will make a proper unit test and documentation for it later. http://frigocoder.dyndns.org/svn/Frigo/Lang/ref
It still has some quirks though. Because it is implicitly convertible to pointer and reference as well, it will fail for ambiguous overloads. I solved this problem for friend binary operators, but not for functions, alien assignment operators or alien/friend compound assignment operators (due to an unimplemented feature in GCC). In those cases users need to explicitly specify whether they want pointer or reference with get() or getPointer(). Chained implicit conversions do not work either, due to the same reason that prevented me from setting the precedence of pointer conversion lower: C++ allows only 1 implicit conversion (via operator or constructor) in a conversion sequence.
Dereferencing with unary * returns a const reference to the object, but the unary address-of & operator is proxied to the underlying class, for example this allows getting a T* from ref<shared_ptr<T>>, but why would you do that anyway? I'm thinking about returning simply a pointer to the object. It's a minor issue really, users should use get() and getPointer() anyway.
Binary operators now return whatever they would return in T, I'm thinking about changing them to return ref<X>, I'm rather undecided on this one. Changing would require different treatment of return types and it would be slower, but it would solve chaining in classes that return pointers in binary operators. Pointers or rvalues are straightforward, just ref<X>(ptr or rvalue), references are a bit icky, they would need ref<X>(&reference), but that's unsafe, however somewhat acceptable. ref<X>(reference) would copy the result, which is not acceptable in a lot of cases, for example, T& Array<T>::operator [] (int i).
And of course, due to an unimplemented feature in GCC, compound assignment operators are not proxied to the underlying class, it is broken down as an assignment to the result of a binary operator. For example, x += y is broken down as x = (x + y). I believe this is better anyway.
|
On September 09 2011 00:17 Nisani201 wrote:w3schools should not be included in the OP; read this for more info. Those are definitely good links. w3schools is definitely sub-intermediate and not even correct at times. Nonetheless it offers hands-on intro material on a wide variety of web technologies, and it delivers it without the daunting complexity of w3c's specifications (e.g. http://www.w3.org/TR/CSS/#css3 is not a fun read). Maybe noobies should step up their game, and it is nice to see people pointing out w3schools's faults (there are many) -- but everyone needs that nooby first tutorial that holds their hand the entire way before they go into the deep end.
MDN's js guide is very good. Some of MDN's other docs are very poor.
Maybe the OP can update the post with these new links.
|
Is it ok to bother you coding bosses with extremely simple newb stuff (first programming class lulz) or is this more for in depth stuff?
|
On September 12 2011 05:58 f0rzaa wrote: Is it ok to bother you coding bosses with extremely simple newb stuff (first programming class lulz) or is this more for in depth stuff? Of course it's okay. Hit us.
|
Well the return reserved word, early in the text it says without the use of return sometimes my programs may act in a way I don't want them to. My instructor told us not to worry about it for now because we will cover it later on but I want to know, how does exactly return effect my program if it's included and if it's not? My first program didn't have it but it seemed to work fine, so when would these "unwanted results" occur?
|
Which programming language? In what function, main or something else? What does this function should return?
In C and C++ you pretty much need a return statement in non-void functions, otherwise it may return garbage from the stack. The compiler even warns you about it. I tested it with int, and a custom class as return type. While it does seem to return a 0 value by default for int, if the return type is a custom class, it does NOT execute the default constructor. It fills the retunr object with garbage, so yes, it will behave in unpredictable ways.
The main function does not seem to have any trouble, it seems to return 0 by default (if it returns int). And obviously return is not needed for void functions.
|
|
I think this was the first example of the class(C++) intro:
#include <iostream>
using namespace std;
int main() { cout << "Hello world!" << endl; return 0; }
I realize this might be extremely simple but I was playing around with Codeblocks and I seem to get the same thing with or without the return 0;. Hello world! is printed either way.
So basically my question is why's it work with and without it if it's needed?
|
On July 07 2010 22:37 Adeny wrote: Am I the only one who would like a go-to thread to ask questions rather than having to make a new thread? nope i agree
|
On September 12 2011 11:07 f0rzaa wrote: I think this was the first example of the class(C++) intro:
#include <iostream>
using namespace std;
int main() { cout << "Hello world!" << endl; return 0; }
I realize this might be extremely simple but I was playing around with Codeblocks and I seem to get the same thing with or without the return 0;. Hello world! is printed either way.
So basically my question is why's it work with and without it if it's needed?
There is no caller function that would do anything with the return value of main, so even if it "returns" garbage, there is no problem with it. (Both Linux and Windows can check for the return value, the latter with IF ERRORLEVEL, but it was a while ago since I used it). You may still get a compiler warning though.
|
On September 12 2011 09:40 Klesky wrote: Most compilers willshould? issue a syntax error* if you don't include a return statement on a non-void function - I wouldn't worry about it. ... edit: this is assuming it's C++. Continuing to assume C++, it's easy to wind up with a function like
int foo(int a) { if(a) { return 0; } } which is legal. (In other words, a compiler that rejects that violates the standard.) GCC doesn't even warn about that with default settings. (That's level 1 in MSVC, so unless you turn off all warnings or disable that one specifically, you'll see it.)
I'm also going to bet your first program was something like 'void main() {}' would would explain why it didn't have a return statement. If it was, get your teacher to throw out your book. ;-)
G++ hasn't even compiled that for ages, though MSVC compiles it without warning. (Near as I can tell, a void return from main is accepted by g++ 2.95 and rejected by 3.0. The C complier accepts it, but with a warning on default settings.y)
|
I wanna post a piece of code because I'm goddamn proud of it.
One guy at stackoverflow.com basically asked whether it is possible to detect cycles in an alleged binary tree in O(n) time and O(1) space, in an analogue to the Floyd cycle detection in lists.
After a few days of thinking I managed to come up with a pretty nice solution. I flatten the binary tree to a list-like structure, meanwhile watching for cycles. Cycle detection, tree flattening and in-order traversal in one piece!
It starts at the root node as its current. In every iteration it either 1) gets it left child ("top") and the predecessor element ("bottom"), then move THE ENTIRE LEFT SUBTREE above the current, preserving the in-order sequence of nodes, then of course moves its current pointer to traverse the newly discovered nodes (okay, this is basically two cases but handled similarly). 2) if it has no left subtree then it moves to the next node in the right subtree that indeed has one, visiting nodes in-order.
The effect of these two steps is flattening the tree. It also incorporates Floyd's cycle detection algorithm at two places where it is possible to get in a loop. One is where it searches for the predecessor node, the other is where it searches for a node with a left subtree. Every single cycle will get caught in one of these parts sooner or later.
Every time it executes a moving of a left subtree, it trades a "left-child relation" to a "right-child relation", and it arguably moves all left subtrees. I also think it visits all edges at most twice but I'm not sure, it would need heavier proof.
+ Show Spoiler [Example code] + #include <cstdio> #include <iostream> #include <queue>
#define null NULL #define int32 int
using namespace std;
/** * Binary tree node class **/
template <class T> class Node { public:
/* Public Constructors */
Node () : left (null), right (null), value () {}
explicit Node (T x) : left (null), right (null), value (x) {}
/* Public Attributes */
Node* left; Node* right; T value;
};
/** * This exception is thrown when the flattener & cycle detector algorithm encounters a cycle **/
class CycleException {
public:
/* Public Constructors */
CycleException () {} virtual ~CycleException () {}
};
/** * This functions flattens an alleged binary tree, throwing a new CycleException when encountering a cycle. Returns the root of the flattened binary tree. **/
template <class T> Node<T>* flatten (Node<T>* current) { // Count steps (well, kinda) to determine runtime int32 steps = 0; // Keep track of the root node so the tree is not lost Node<T>* root = current; // Keep track of the parent of the current node since it is needed for insertions Node<T>* parent = null;
// Loop while there are subtrees to process while( current->left != null or current->right != null ){
steps++;
// There is a left subtree, so current has a predecessor, which we will call "bottom" of the subtree if( current->left != null ){ Node<T>* top = current->left; Node<T>* bottom; // The top and the bottom is one and the same if( top->right == null ){ bottom = top; } // The bottom is buried in the right subtree of top if( top->right != null ){ // Find it using Floyd's cycle detection algorithm applied to right childs Node<T>* turtle = top; Node<T>* hare = top->right; while( hare->right != null ){ if( turtle == hare ){ throw new CycleException(); } if( hare->right != null ){ steps++; hare = hare->right; } if( hare->right != null ){ steps++; hare = hare->right; } turtle = turtle->right; } bottom = hare; } // Remove subtree from current current->left = null; // Insert subtree between the current node and its parent, if there is any if( parent != null ){ parent->right = top; } bottom->right = current; // If the current node is the root then the top is the new root if( root == current ){ root = top; } // Step up to process the top, parent remains the same current = top; continue; }
// There is only a right subtree, Floyd's cycle detection algorithm must be applied to find a node that has a left subtree if( current->left == null and current->right != null ){ cout << "Visited " << dec << current->value << " @ 0x" << hex << reinterpret_cast<int32>(current) << endl; Node<T>* hare = current->right; Node<T>* hareParent = current; Node<T>* turtle = current; while( hare->left == null and hare->right != null ){ if( turtle == hare ){ throw new CycleException(); } if( hare->left == null and hare->right != null ){ cout << "Visited " << dec << hare->value << " @ 0x" << hex << reinterpret_cast<int32>(hare) << endl; steps++; hare = hare->right; hareParent = hareParent->right; } if( hare->left == null and hare->right != null ){ cout << "Visited " << dec << hare->value << " @ 0x" << hex << reinterpret_cast<int32>(hare) << endl; steps++; hare = hare->right; hareParent = hareParent->right; } turtle = turtle->right; } current = hare; parent = hareParent; continue; }
}
cout << "Visited " << dec << current->value << " @ 0x" << hex << reinterpret_cast<int32>(current) << endl; cout << "Steps taken: " << dec << steps << endl;
// there are no more subtrees to process, we are finished, the tree does not contain cycles return root;
}
template <class T> void traverseFlat (Node<T>* current) { while( current != null ){ cout << dec << current->value << " @ 0x" << hex << reinterpret_cast<int32>(current) << endl; current = current->right; } }
template <class T> Node<T>* makeCompleteBinaryTree (int32 maxNodes) { Node<T>* root = new Node<T>(); queue<Node<T>*> q; q.push(root); int32 nodes = 1; while( nodes < maxNodes ){ Node<T>* node = q.front(); q.pop(); node->left = new Node<T>(); q.push(node->left); nodes++; if( nodes < maxNodes ){ node->right = new Node<T>(); q.push(node->right); nodes++; } } return root; }
template <class T> void inorderLabel (Node<T>* root) { int32 label = 0; inorderLabel(root, label); }
template <class T> void inorderLabel (Node<T>* root, int32& label) { if( root == null ){ return; } inorderLabel(root->left, label); root->value = label++; inorderLabel(root->right, label); }
int32 main (int32 argc, char* argv[]) { if(argc||argv){}
typedef Node<int32> Node;
// Make binary tree and label it in-order Node* root = makeCompleteBinaryTree<int32>(1 << 16); inorderLabel(root);
// Try to flatten it try{ root = flatten(root); }catch(CycleException*){ cout << "Oh noes, cycle detected!" << endl; return 0; }
// Traverse its flattened form traverseFlat(root);
}
|
I haven't examined the solution that closely, but isn't mutating the original tree kind of cheating the O(1) space requirement?
|
I doubt it. It does not create new edges, only rearranges them, unlike Morris traversal, and it can be argued even Morris is O(1). I can not think of any sensible storage method that would incur any overhead due to this. Definitely not the common Node structure with left and right pointers.
By the way, the "inorderLabel" function is there only to demonstrate that it indeed does print nodes out in-order. It is not part of the algorithm.
|
I mean, Morris traversal restores the original state of the tree afterwards, so while it isn't thread-safe, it keeps the tree intact. Your algorithm seems to leave the tree in a flattened state.
|
I agree with Typhon, I don't think it's fair if you don't count the time spent to make the ("alleged") binary tree a binary tree again!
But it's a neat idea, and I definitely can't think of a better way to accomplish it in constant space.
|
ok, im in high school and looking for some good extracurriculars for colleges. I have limited knowledge in programming, as I've only taken 2 classes, one intro to basic at school, and then an intro to python. I did very well in those classes and enjoyed them , but still, im quite nooby. Basically what im wondering is, do you think it would be possible for me to try and learn how to make a very basic app for the app store? And if that would be too challenging, do you have any suggestions to try and incorporate programming into a fun, good extracurricular?
|
On September 13 2011 10:59 Clank wrote: ok, im in high school and looking for some good extracurriculars for colleges. I have limited knowledge in programming, as I've only taken 2 classes, one intro to basic at school, and then an intro to python. I did very well in those classes and enjoyed them , but still, im quite nooby. Basically what im wondering is, do you think it would be possible for me to try and learn how to make a very basic app for the app store? And if that would be too challenging, do you have any suggestions to try and incorporate programming into a fun, good extracurricular?
No, you could do it in a couple weeks worth of evenings. Go do it!
|
On September 13 2011 02:33 Typhon wrote: I mean, Morris traversal restores the original state of the tree afterwards, so while it isn't thread-safe, it keeps the tree intact. Your algorithm seems to leave the tree in a flattened state.
On September 13 2011 02:36 catamorphist wrote: I agree with Typhon, I don't think it's fair if you don't count the time spent to make the ("alleged") binary tree a binary tree again!
But it's a neat idea, and I definitely can't think of a better way to accomplish it in constant space.
Well if the problem specification requires the immutability of the tree then yeah, it requires O(n) space to store the original tree (Edit: how to copy an alleged tree with cycles anyway?). It is not possible to restore it solely from the in-order traversal / flattened form.
However it might be possible to modify the algorithm such that instead of flattening, it reduces the tree to some kind of zig-zag form, storing enough information to reconstruct the tree. It sure as hell would be difficult to construct such an algorithm 
|
|
|
|