• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 08:53
CET 14:53
KST 22:53
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10[ASL20] Finals Preview: Arrival13
Community News
[TLMC] Fall/Winter 2025 Ladder Map Rotation13Weekly Cups (Nov 3-9): Clem Conquers in Canada4SC: Evo Complete - Ranked Ladder OPEN ALPHA8StarCraft, SC2, HotS, WC3, Returning to Blizzcon!45$5,000+ WardiTV 2025 Championship7
StarCraft 2
General
Zerg is losing its identity in StarCraft 2 [TLMC] Fall/Winter 2025 Ladder Map Rotation Mech is the composition that needs teleportation t RotterdaM "Serral is the GOAT, and it's not close" RSL Season 3 - RO16 Groups C & D Preview
Tourneys
RSL Revival: Season 3 Sparkling Tuna Cup - Weekly Open Tournament Constellation Cup - Main Event - Stellar Fest Tenacious Turtle Tussle Master Swan Open (Global Bronze-Master 2)
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened Mutation # 496 Endless Infection
Brood War
General
FlaSh on: Biggest Problem With SnOw's Playstyle BGH Auto Balance -> http://bghmmr.eu/ What happened to TvZ on Retro? SnOw's ASL S20 Finals Review BW General Discussion
Tourneys
[Megathread] Daily Proleagues Small VOD Thread 2.0 [BSL21] RO32 Group D - Sunday 21:00 CET [BSL21] RO32 Group C - Saturday 21:00 CET
Strategy
PvZ map balance Current Meta Simple Questions, Simple Answers How to stay on top of macro?
Other Games
General Games
Clair Obscur - Expedition 33 Should offensive tower rushing be viable in RTS games? Path of Exile Stormgate/Frost Giant Megathread Nintendo Switch Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
Things Aren’t Peaceful in Palestine US Politics Mega-thread Russo-Ukrainian War Thread Artificial Intelligence Thread Canadian Politics Mega-thread
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Dyadica Gospel – a Pulp No…
Hildegard
Coffee x Performance in Espo…
TrAiDoS
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Reality "theory" prov…
perfectspheres
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2241 users

The Big Programming Thread - Page 76

Forum Index > General Forum
Post a Reply
Prev 1 74 75 76 77 78 1032 Next
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.
Frigo
Profile Joined August 2009
Hungary1023 Posts
Last Edited: 2011-09-05 13:05:57
September 05 2011 13:04 GMT
#1501
It's a bit of misnomer, it returns a T& reference to the stored object (or throws an exception if the pointer is null). Both operations are on references. Naming is kinda taken from std:: reference_wrapper.

I think it would be wiser to implement it the second way, that only the reference wrapper gets modified. Less headaches with immutable types, threads, unintended consequences.

However, operator + might not return T. Damn. This can complicate things in the second case, with the plain operator +. However in that case I am not obligated to return ref<T> in the proxy. Only the += is problematic since I need to return ref<T>&.

I'll get some sleep over this, hopefully I will come up with a satisfying solution.
http://www.fimfiction.net/user/Treasure_Chest
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
September 05 2011 13:33 GMT
#1502
Basically, if operator overloading is used, anything can happen.

The innocent line
a += 1;
could be an overloaded += operator that initiates nuclear warfare.

Also, the lines
a += 1
could increase a by 1 while
a = a + 1
could decrease a by 10.

This is one of the reasons why java removed operator overloading completely (I don't agree with that decision, but it wasn't totally unreasonable).

If you use operator overloading or use classes that do it, always read the documentation (or in case of the usual lack of it, the code). Otherwise there is no way to really know what will happen. Avoid it as much as possible unless the benefits are far greater than the possible pitfalls for this specific scenario.
EscPlan9
Profile Blog Joined December 2006
United States2777 Posts
Last Edited: 2011-09-05 14:06:55
September 05 2011 13:53 GMT
#1503
On September 03 2011 09:16 Neshapotamus wrote:
EscPlan9, I dont understand your post at all. First off, if you have a WSDL, cant you generate a webservice proxy and use strongly typed object. If that approach doesnt work out and sounds like your getting XML, do you have the XSD for the XML. That way, you can serialize and deserialize the XML into a object. If you dont want to do it that way, can't you use LINQ to XML to and create a annonoymous object with the name for the property you would want.

Maybe, an example of what your trying to do and I can be more clear on taking the best approach.


Fair enough. I have no prior experience with WSDLs - I've just been working with them as if they are XML files. So to be frank, I do not understand the majority of your post either.

The previous developer (no longer with the company) used SvcUtil on the WSDL to generate the client proxy. I originally was tackling the problem through straight XML parsing methods, most recently XPATH. But I figure I really shouldn't be reinventing the wheel when he already figured out a reliable way of parsing the WSDL for the important information he needs. I just know going through his code gives me major headaches trying to understand what he's doing in the sections (no documentation, no comments, code works but is ugly as hell), but I digress...

I'll look into finding the schema (XSD) for the WSDLs. Though I do not yet understand what I should be getting from serializing/deserializing the XMLs (zero experience with that), or how to make use of that information.

As for the last point, I haven't touched LINQ yet. I know SQL and it seems fairly similar in syntax.

The current plan going forward:
1) Take more time to understand how SvcUtil was used on the WSDLs previously.
2) Take more time for planning and design once I understand the organization of the information returned from the SvcUtil generated client proxy.
3) Create new methods to send that information to the (WIP) GUI frontend and generate the tree structure.

A simplified dummy WSDL I created (from the top of my head while at home) that's similar to the WSDLs being analyzed:

<wsdlMe>
<complexType name="Create">
<element name="Items" type="tns:Items" maxOccurs="unbounded"></element>
<element name="Username" type="string"></element>
</complexType>

<complexType name="Items">
<element name="Description" type="string"></element>
<element name="Quantity" type="int"></element>
<element name="Price" type="decimal"></element>
</complexType>

<complexType name="CreateResponse">
<element name="Errors" type="tns:Errors" maxOccurs="unbounded"></element>
</complexType>

<complexType name="Errors">
<element name="Description" type="string"></element>
</complexType>

<complexType name="Finish">
<element name="Time" type="datetime"></element>
</complexType>

<complexType name="FinishResponse">
<element name="Errors" type="tns:Errors" maxOccurs="unbounded"></element>
</complexType>

<messages>
<message name="CreateMsgIn" type="tns:Create"></message>
<message name="CreateMsgOut" type="tns:CreateResponse"></message>
<message name="FinishMsgIn" type="tns:Finish"></message>
<message name="FinishMsgOut" type="tns:FinishResponse"></message>
</messages>

<portType>
<operations>
<operation name="Create" type="tns:CreateMsg"></operation>
<operation name="Finish" type="tns:FinishMsg"></operation>
</operations>
</portType>
</wsdlMe>


The QA tester selects the operation they want to create a test case for. Then the WSDL is parsed (portType/operations/operation) to create the tree structure for the user. So they would see something like

(operation selected is Create)
Create
--CreateMsg
----CreateMsgIn
------Create
--------Items
-----------Description
-----------Quantity
-----------Price
--------Username
----CreateMsgOut

... etc

I've been experimenting with using a Dictionary <string, TreeNode> to hold the information on the tree structure.
Key = child-node-name(string)
Value = parent-node (treenode)

So then when adding a treenode, it's something like

// create new node
TreeNode newNode = new TreeNode(childNodeName);
// get parent node
TreeNode prevNode = dictionary[parentNodeName];
// add child to parent node within tree
prevNode.Nodes.Add(newNode);


I'm pretty stressed that I've wasted a lot of time due to my inexperience here. Thankfully it isn't a high priority at the company? Either way, I've certainly come to appreciate taking more time for designing and planning ahead of time before coding.

(This post was more made to clarify what I'm working on, than to specifically ask questions. Feel free to comment on whatever anyways)
Undefeated TL Tecmo Super Bowl League Champion
pettter
Profile Joined December 2009
Sweden1032 Posts
September 05 2011 14:01 GMT
#1504
Actually, operator overloading, used correctly, is more obvious and less prone to errors than .plus() shenanigans, due to having a well-defined order of precedence without resorting to masses of parentheses. Still, as many (most?) advanced features of C++, it can be horribly, horribly misused as well.

A couple of really hairy combination is a) the combination of multiple inheritance and operator overloading and b) the combination of dynamic typing and operator overloading. Having well-defined types of your variables is absolutely crucial to proper usage of operator overloading.

As for the current problem, the second variant would seem more reasonable, but keep in mind that users may have selected x += y as opposed to x = x + y precisely because they want to change x the object, not x the reference. See [section 13.9 here for example.

It mostly comes down to: comment your damn code, and use const liberally to demonstrate which values change and which do not.
Frigo
Profile Joined August 2009
Hungary1023 Posts
September 05 2011 21:00 GMT
#1505
On September 05 2011 22:33 Morfildur wrote:
This is one of the reasons why java removed operator overloading completely (I don't agree with that decision, but it wasn't totally unreasonable).

Yeah, there are a few heavy abuses of operator overloading but that's no reason to forbid them completely. They are vital to numerical calculations (just as custom value types but that's another story...). They have well defined precedence and can greatly simplify expressions and make them much more transparent. Even if they don't allow full overloading, operators could be aliases for member functions, for example x + y could simply call x.add(y). I believe there is a Java precompiler that does such.

A method can be just as bad as an operator. Undocumented, in violation of contract, malicious. The common reasoning against operators can be applied to methods as well: an add() method might not have anything to do with addition, it could call launchNuclearMissile() just as easily as an operator. They should be treated like methods, with the same scrutiny applied to them. Complete with firing the programmer who abuses operators

If you use operator overloading or use classes that do it, always read the documentation (or in case of the usual lack of it, the code). Otherwise there is no way to really know what will happen. Avoid it as much as possible unless the benefits are far greater than the possible pitfalls for this specific scenario.

Yup, that's the problem here. I do not have a definite class to just look at how it implements operators. Both binary and compound assignment operators might return T, T*, T&, const T& or even a completely unrelated type. The best approach in my case would be to limit the return types to a few correct cases, and use partial template specialization to override this best-guess behavior when it can't model a case properly.
http://www.fimfiction.net/user/Treasure_Chest
0x64
Profile Blog Joined September 2002
Finland4583 Posts
September 05 2011 21:36 GMT
#1506
today I run into a very strange bug with a java midlet app.
I have a base class A and two derived class of A, say B and C.

the problem was with constructor.

A(int n) {
foo = new Plop[n]
}

B() {
super(10);
initFoos();
}

C() {
super(12);
initFoos();
}

----
Now the thing is when I would call initFoos for C, it would crash with out of bound after 10 elements on one device. And work properly on another device...
Just wondering if someone really experienced have deeper knowledge of possible ambiguities.
I have 10 years of work in the field, so I know better not to blame anything else than my code (in this case one of my workers.. ).
We did a work around but I'm not happy about that.
Dump of assembler code from 0xffffffec to 0x64: End of assembler dump.
Frigo
Profile Joined August 2009
Hungary1023 Posts
Last Edited: 2011-09-05 21:56:50
September 05 2011 21:42 GMT
#1507
From the sound of it, C.initFoos() does not give a flying fuck about foo.length (or even modifies foo).

Give us more code, it is difficult to tell the problem from a few lines.
http://www.fimfiction.net/user/Treasure_Chest
Neshapotamus
Profile Blog Joined May 2006
United States163 Posts
Last Edited: 2011-09-06 07:16:37
September 06 2011 07:11 GMT
#1508
Hey EscPlan9,

Seems like you need parse an XML file (in your case a WSDL) and get relevant information from it.

I dont really understand what WIP is but I am assuming its your front end (GUI) which has some type of tree control which can parse an object.

Here is my plan on how I would tackle your situation:
http://schemas.xmlsoap.org/wsdl/ contains the XSD for any WSDL.
Save your xsd

Use visual studio tools to create a class out of the XSD as follows:
xsd.exe [fileName].xsd /c <--cmd line tool (this creates a C# class)

The xsd tool complained about "...", I needed to remove the "..." from the xsd to create the C# class

Add this class to your project.


Write this method as follows:
public object XmlToObject(Type type, string fileName)
{
XmlSerializer ser = new XmlSerializer(type);
FileStream myFileStream = new FileStream(fileName, FileMode.Open);
object obj = ser.Deserialize(myFileStream);
return obj;
}

Now you have your XML(WSDL) as a strong typed object instead of XML. You wont need to parse XML and make references to any xml methods.

You need to convert this object into a Model that you want to send out. I would create a model class and just grab the information you need from the deserialized object.

Have another methods that would create your model(deserialized object) with your tree structure which you would send to your front end.

PM me if this is confusing in any way or I didn't understand you correctly.

Frigo
Profile Joined August 2009
Hungary1023 Posts
September 06 2011 16:00 GMT
#1509
I've got a dilemma about a design choice in my pointer / reference wrapper, the insight of an outsider would be welcome.

Should I proxy operator == and != to the underlying class, just as I do in the case of other operators, or should I use them for pointer comparison instead? Or perhaps both, the latter for pointers, references, rvalue references, reference wrappers, and the former for everything else? Though it sounds kinda dangerous to have two different semantics for a single operator.

I already have an "is" member function for pointer comparison, but it is kinda ugly. For example
ref<int> x = 1;
ref<int> xcopy = x;
ref<int> y = 2;
cout << x.is(xcopy) << endl;
cout << x.is(y) << endl;
cout << x.is(null) << endl;


Perhaps I could define an "is" macro with some trick so the following is possible
cout << x is xcopy << endl;
cout << x is y << endl;
cout << x is null << endl;


For reference, operator = is NOT proxied to the underlying class, it just copies the address where it can. If the user wants assignment he uses
ref<SomeClass> x = new SomeClass(...);
ref<SomeClass> y = new SomeClass(...);
x.get() = y.get();

http://www.fimfiction.net/user/Treasure_Chest
mmp
Profile Blog Joined April 2009
United States2130 Posts
Last Edited: 2011-09-07 15:22:02
September 07 2011 13:53 GMT
#1510
Challenge: Write a function A in your language of choice that takes an object argument O and adds a new function B to O defined as follows: given a function argument C, B sets every other function D in O (exclude B) in the object equal to the given function C applied to the original function D (the return value of C(D) is a function, of course).

Solution (javascript):
+ Show Spoiler +

A = function(O) {
var modified = new Array();
for (var D in O) {
if (!Object.isFunction(O[D])) continue;
modified.push(D);
};
O.B = function(C) {
modified.each(function(D){
O[D] = C(O[D]);
});
return O;
};
return O;
}


I wrote something like this today as part of a functional modifier stack. So you declare A(O) to make an O with extensible methods. You can repeatedly stack functions by repeatedly calling O.B(C) to convert every D into C(D), C(C(D)), C(C(C(D))).... Here is a video of what a modifier stack looks like in Blender to get an idea of how useful this can be.
I (λ (foo) (and (<3 foo) ( T_T foo) (RAGE foo) )) Starcraft
Frigo
Profile Joined August 2009
Hungary1023 Posts
Last Edited: 2011-09-07 16:56:14
September 07 2011 14:08 GMT
#1511
So you are essentially doing an O.D ∘ C composition for every D method of O, where C is a function.

That is not possible in C++ due to lack of type inspection. Even with a wrapper class like mine, you can not proxy methods with arbitrary name, so composition of all methods is not possible. Especially not dynamically.

But I believe you can make a class template A that in its constructor accepts o and c objects (of which are instances of template parameters O and C, an object and a functor respectively), and proxies all operators and a given set of methods of O that are known beforehand. Still no dynamically though.

Edit:

Yup, it is possible statically for a limited set of methods and operators, see this example for operator ():

#include <iostream>
#include <functional>
#include <utility>

using namespace std;

template <class O, class C>
class A
{

public:

A (O& o2, C& c2) :
o (o2),
c (c2)
{}

template <class... Args>
decltype(std::declval<O&&>()(std::declval<C&&>()(std::declval<Args&&>()...))) operator () (Args&&... args)
{
return o(c(std::forward<Args&&>(args)...));
}

protected:

O& o;
C& c;

};



class O
{

public:

int operator () (int x)
{
return 2 * x;
}

};

class C
{

public:

int operator () (int x)
{
return x + 1;
}

};


int main (int argc, char* argv[])
{
O o;
C c;
A<O, C> a(o, c);

cout << a(1) << endl;
cout << a(10) << endl;
}

http://www.fimfiction.net/user/Treasure_Chest
mmp
Profile Blog Joined April 2009
United States2130 Posts
September 07 2011 15:31 GMT
#1512
Impressive, Frigo. I was hoping someone would post a C/C++ solution.
I (λ (foo) (and (<3 foo) ( T_T foo) (RAGE foo) )) Starcraft
icystorage
Profile Blog Joined November 2008
Jollibee19350 Posts
September 08 2011 12:57 GMT
#1513
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 =/
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
Kambing
Profile Joined May 2010
United States1176 Posts
Last Edited: 2011-09-08 15:20:11
September 08 2011 14:39 GMT
#1514
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.
FranzP
Profile Joined November 2010
France270 Posts
Last Edited: 2011-09-08 15:13:08
September 08 2011 14:53 GMT
#1515
@Frigo I'm working with java so there is this kind of design i.e. == compares ref and .equals(x) method compares content.

What I don't like with this design is that I usually use object comparison more often than ref comparison. And I feel like the equals method clutters my code.

It's just about taste but if I were you I would use == to compare the referenced object and another method to compare reference. That's just my feelings using == and .equals()
"Cyberhacking is kind of like masturbation I guess, all countries do it but nobody actually talks about it. China just was accidentally doing it with the door wide open." Newbistic
Nisani201
Profile Joined September 2010
United States1400 Posts
September 08 2011 15:17 GMT
#1516
w3schools should not be included in the OP; read this for more info.
Enjoy your day.
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
September 08 2011 15:23 GMT
#1517
On September 08 2011 23:53 FranzP wrote:
@Frigo I'm working with java so there is this kind of design i.e. == compares ref and .equals(x) method compares content.

What I don't like with this design is that I usually use object comparison more often than ref comparison. And I feel like the equals method clutters my code.

It's just about taste but if I were you I would use == to compare the referenced object and another method to compare reference. That's just my feelings using == and .equals()


.equals() also adds some problems to the language like x.equals(y); that throws an exception if x is null, == doesn't have that problem. That leads to wierd stuff like "somestring".equals(x); which is... not quite logical or intuitive.
Kambing
Profile Joined May 2010
United States1176 Posts
September 08 2011 16:36 GMT
#1518
On September 09 2011 00:23 Morfildur wrote:
Show nested quote +
On September 08 2011 23:53 FranzP wrote:
@Frigo I'm working with java so there is this kind of design i.e. == compares ref and .equals(x) method compares content.

What I don't like with this design is that I usually use object comparison more often than ref comparison. And I feel like the equals method clutters my code.

It's just about taste but if I were you I would use == to compare the referenced object and another method to compare reference. That's just my feelings using == and .equals()


.equals() also adds some problems to the language like x.equals(y); that throws an exception if x is null, == doesn't have that problem. That leads to wierd stuff like "somestring".equals(x); which is... not quite logical or intuitive.


.equals itself is a nasty corner of the language due to the difference between primitive (value) and reference types. However, being able to call methods on any object, i.e., "hello".equals(x), should be expected in an object-oriented language where "everything is an object". It ought to be considered weird that you aren't able to call methods on primitive data (in Java at least, C# automatically boxes primitives so that you can do this --- at the cost of the actual boxing operation).
icystorage
Profile Blog Joined November 2008
Jollibee19350 Posts
September 08 2011 18:37 GMT
#1519
On September 08 2011 23:39 Kambing wrote:
Show nested quote +
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 ._.
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
Frigo
Profile Joined August 2009
Hungary1023 Posts
September 08 2011 19:20 GMT
#1520
On September 08 2011 23:53 FranzP wrote:
@Frigo I'm working with java so there is this kind of design i.e. == compares ref and .equals(x) method compares content.

What I don't like with this design is that I usually use object comparison more often than ref comparison. And I feel like the equals method clutters my code.

It's just about taste but if I were you I would use == to compare the referenced object and another method to compare reference. That's just my feelings using == and .equals()


Thank you for your feedback. I asked my friend as well and concluded that indeed this is the correct approach.

Object comparison is done with == and !=, pointer comparison is done with an "is" macro that is defined as:
#define is .getPointer() ==


It seems to be working pretty well, it handles pointer comparison between reference wrappers as well, due to implicit conversion to pointer.
http://www.fimfiction.net/user/Treasure_Chest
Prev 1 74 75 76 77 78 1032 Next
Please log in or register to reply.
Live Events Refresh
WardiTV Korean Royale
12:00
Group Stage 1 - Group A
WardiTV559
LiquipediaDiscussion
Kung Fu Cup
12:00
2025 Monthly #3: Day 5
Cure vs herOLIVE!
Reynor vs TBD
RotterdaM693
TKL 366
IndyStarCraft 269
SteadfastSC122
IntoTheiNu 43
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 617
TKL 352
Tasteless 324
Reynor 276
IndyStarCraft 269
Rex 147
SteadfastSC 113
Railgan 25
MindelVK 21
StarCraft: Brood War
Britney 36790
Rain 8932
Sea 4139
firebathero 4096
BeSt 1748
Horang2 1687
GuemChi 939
Soma 605
EffOrt 552
Stork 500
[ Show more ]
Mini 456
actioN 283
Rush 225
Killer 174
Last 146
Hyun 144
scan(afreeca) 136
hero 133
Bonyth 99
Mind 90
Barracks 61
Sharp 51
zelot 50
yabsab 49
Sea.KH 45
sorry 42
Snow 39
sas.Sziky 24
Shinee 18
Hm[arnc] 15
NaDa 8
Bale 6
Dota 2
singsing3070
qojqva1678
Dendi1083
Gorgc737
XcaliburYe301
Counter-Strike
byalli539
x6flipin531
Heroes of the Storm
Khaldor238
Liquid`Hasu193
Other Games
FrodaN4939
B2W.Neo1844
Pyrionflax359
Fuzer 256
KnowMe216
DeMusliM40
Organizations
Dota 2
PGL Dota 2 - Main Stream14873
PGL Dota 2 - Secondary Stream1808
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• Adnapsc2 8
• Dystopia_ 6
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV606
• Ler66
Upcoming Events
BSL 21
6h 7m
JDConan vs Semih
Dragon vs Dienmax
Tech vs NewOcean
TerrOr vs Artosis
IPSL
6h 7m
Dewalt vs WolFix
eOnzErG vs Bonyth
Replay Cast
9h 7m
Wardi Open
22h 7m
Monday Night Weeklies
1d 3h
Replay Cast
1d 9h
WardiTV Korean Royale
1d 22h
BSL: GosuLeague
2 days
The PondCast
2 days
Replay Cast
3 days
[ Show More ]
RSL Revival
3 days
BSL: GosuLeague
4 days
RSL Revival
4 days
WardiTV Korean Royale
4 days
RSL Revival
5 days
WardiTV Korean Royale
5 days
IPSL
6 days
Julia vs Artosis
JDConan vs DragOn
RSL Revival
6 days
Liquipedia Results

Completed

Proleague 2025-11-14
Stellar Fest: Constellation Cup
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
BSL Season 21
CSCL: Masked Kings S3
SLON Tour Season 2
RSL Revival: Season 3
META Madness #9
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.