Wikipedia:Reference desk/Archives/Computing/2015 January 16

= January 16 =

please give me a problem to solve (as below)
could you please give me some problems to solve, using a normal old language like C, etc. Then, after I've solved them, can you include how a monad would help if it existed in the language?

For example if I did this example with "regular expressions" you would have an easy time of it. If I did this request for "classes" it wouldn't be so hard either, you could give me case where it would really help. I know these two concepts. But I don't know what a monad is. So, could you lead me to its usefulness by making me do some things in C? I would then like to solve the problem in the normal way in C, and then read the next part of your response where you explain how it would happen with monads. (Obviously the target is to then use the understanding in a language that does have them.) Thank you. If you need to understand my request better I will give you examples of string-matching problems in C that I could have been given for the first example. (Before being introduced to the idea of regular expressions.) 212.96.61.236 (talk) 06:45, 16 January 2015 (UTC)


 * I assume you're talking about monad (functional programming), but I'm kind of confused by your question. Why would I have an easy time with the regular expression example? What about you? :-)
 * Most (if not all) uses of monads boil down to mutable state and/or backtracking, or something that can be described in terms of backtracking. If you know C then you understand mutable state, and if you understand regular expression matching as found in Perl (as opposed to the traditional NFA→DFA approach) then you understand backtracking. So the regular expression example makes the most sense to me. But it's hard to come up with a good problem suggestion.
 * You could write a recursive descent parser for some regular expression (or any context-free grammar) that is not predictive: i.e., when you have several alternatives to choose from, you don't peek at the next symbol to figure out which alternative to choose, you just stupidly try them one by one until one doesn't fail. That would map pretty closely to a simple monadic solution, with each function you write corresponding to one line in Haskell's do notation. Probably. I'd have to see your solution. This seems like a lot of work, though.
 * Maybe better would be to write a parser as a collection of Python list comprehensions, returning a list of all possible parses of the input. If you can figure out how to make that work, you're 95% of the way there, because monads aren't much different from list comprehensions. -- BenRG (talk) 08:34, 16 January 2015 (UTC)
 * I don't understand any of the words you just said, and, sorry, I could have found similar language all over our articles and the web. I didn't come here for a summary.  I came here for an example that does not use any terminology (not even the word monad).
 * Here is what you could give me for regular expressions. "Read a file line by line, and print out any lines which contain exactly 5 groups of characters separated by 1 or more consecutive spaces. So: "this is a line, okay" matches but "this isn't a line-sorry" doesn't as there is no space between line and sorry. Also "34 dsfj 342 12d df" matches.)."  I could do that example in a few minutes in C, without reference to any terminology.  I'd like an example, not abstract terminology.  Give me a problem in terms that an eighth-grader can understand. 212.96.61.236 (talk) 08:59, 16 January 2015 (UTC)
 * I'm sorry, but I don't know what words you know. If you want to write the program you just suggested in C, I can write code with the same functionality in Haskell, but I'm not sure it will be all that enlightening.
 * If someone said "I don't understand garbage collection, so give me a programming problem, I'll solve it in C, then explain how adding garbage collection to C would have helped in solving that problem", I wouldn't really know how to respond. I think the benefits of garbage collection, structured programming, OOP classes, and such only show up in larger programs, and the same is true of monad-style programming, so a toy example probably won't benefit from introducing any of those things. -- BenRG (talk) 22:39, 16 January 2015 (UTC)


 * I don't think any Haskell apologists frequent the computer reference desk, so it's unlikely we'll synthesize a great and novel example for you... but perhaps you'll find the Haskell Tutorial for C Programmers enlightening, particularly its section on monads. Personally, much as I try to keep my mind open to new ways of thinking about programming, I cannot see any scenario where a monad makes for a better solution to a problem.  Rather, it seems like monads are used to make problems out of solutions - and they do so without any side effects.  (Forgive my cynicism: the monad as a concept in Haskell is explicitly used to break the abstraction of a purely functional programming language, and the details are swept under the rug as a "feature of the implementation" that is implemented in "low level code" - ergo, implemented in C.  That the author of this tutorial for C programmers would try to pull this wool over our eyes speaks legions about the utility and the target audience of the Haskell language). Nimur (talk) 08:43, 16 January 2015 (UTC)


 * I don't see why you posted this response. (And I didn't mention Haskell.) Monads are a feature, they can be used for something, if they don't exist in a language you can't use them.  Make me complete an example (not in the abstract, give me a concrete example an eighth-grader can understand) in C, and then we'll go from there.  if you can't think of an example or don't want to, thanks for your input and we'll let someone else give me one.  thanks. 212.96.61.236 (talk) 08:59, 16 January 2015 (UTC)
 * No other common programming language uses the term.
 * Here's your example task: "Print the character 'a'.". In C, you can use putc, but in a purely functional programming language, this I/O operation changes the state of the machine... so it is illegal. Use a monad to avoid this conundrum.  Here is a C implementation of a Haskell-runtime that implements this monad.  You can review the source code for reference.
 * As you can plainly see from this, the trivial example, the monad concept exists to work around a self-imposed requirement of the programming-style. The only language you will find this construction in actual use is in Haskell.  You could apply the concept to other languages, but ... nobody does so.  Nimur (talk) 09:03, 16 January 2015 (UTC)
 * There are libraries for monadic programming in other languages including Python, but I have no experience with them.
 * The point of restrictive disciplines like strong static typing and structured programming is that you get something in return for your effort—generally code that's easier to read and maintain. This is also true of the lack of side effects in Haskell, in my experience; when you know what parameters a function takes and what it returns, you know everything there is to know about that function, and that's useful.
 * Monads do not break Haskell's rule about side effects, and understanding that is certainly important to understanding monads.
 * The sweeping-under-the-rug thing also applies to garbage collection. I hope you aren't opposed to garbage-collected languages too. Really, it applies to any higher-level language feature. -- BenRG (talk) 22:39, 16 January 2015 (UTC)

Motivation
I'd like to say that despite some difficulties along the way, this was a really good question. There are lots of cool, sophisticated, and/or tricky concepts in programming that are traditionally described absolutely horribly. Far too often, the documentation is written from the point of view of someone who already knows exactly what the concept is for, but merely needs some help with the particular syntax, or something. But new learners who have no idea what the concept is for are left at sea, trying to reverse engineer the utility of the concept from the toy examples that are used to "explain" it.

(I would therefore tend to dispute the OP's assertion that if he'd done the question with regular expressions we'd necessarily have had an "easy time of it", but anyway.)

My favorite example of this problem is pointers, which are traditionally explained using a bunch of artificial examples which make perfect sense to someone who already understands pointers and none to someone who doesn't. (I once tried to motivate and explain them "properly" in some class notes I wrote, although the current discussion makes me realize that it might have been better to motivate the idea with an example that actually involved programming, rather than the rather abstract clubroom mailbox example.)

Thanks to Nimur for his very good second answer. (I learned something new today!) —Steve Summit (talk) 13:31, 16 January 2015 (UTC)


 * If someone asks what an atom looks like, and someone responds with the usual BS about electron clouds, and someone else says it's like a mini solar system, you might thank the second person for finally making it clear. You learned something today! But you didn't, because the thing you thought you learned isn't true.
 * C pointers are a very simple concept, in hindsight, but I remember struggling to understand them when I learned C. For that matter I remember struggling to understand long subtraction in elementary school. I don't think there's a royal road to any of these things. You have to work with them yourself to understand the rules. Quantum mechanics is very simple too, in hindsight. -- BenRG (talk) 22:39, 16 January 2015 (UTC)


 * In the case of physics, it's not as simple as "correct" or "incorrect". The planetary model of the atom is a useful model in many cases, particularly when looking at chemical bonds in molecules.  The next level up, where you have various shaped s, p, d, and f orbitals, is more complex, but a bit more accurate.  Then you get to electrons existing everywhere simultaneously, and only exhibiting a probability distribution waveform which collapses when it is observed, and it gets even messier, although perhaps a bit closer to the real case.  So, you choose the model that's best for your case, just as you do in other areas.  You wouldn't just reject a flat map as always being incorrect, and insist on a globe in all cases, would you ? StuRat (talk) 04:10, 19 January 2015 (UTC)

Excel
I have a set of data for 2014 in a worksheet in excel. I have used this data to create a line graph in a separate worksheet. The formula goes like this: ='2014'!$B$9:$M$9. This has done exactly what I wanted, however I have now created another worksheet with the same layout for new data for 2015. I want my graph to now show one continuous line now covering both 2014 and 2015 data, but can't figure out how to do it. I have tried various combinations like ='2014'!$B$9:$M$9+'2015'!$B$9:$M$9 for example but this doesn't work... Please can someone help! — Preceding unsigned comment added by 86.21.27.165 (talk) 19:39, 16 January 2015 (UTC)


 * I did a Google search for Excel discontiguous reference and found Excel Tips - Multi Area Argument. It looks like you use a comma to specify a multi-area range. However, be aware some functions in Excel require a contiguous range, so it might not work. To try it, use something like  or  . --Bavi H (talk) 02:21, 17 January 2015 (UTC)

need more wired connections
I have a wireless router with four wired connections. Now I need two more wired connections. I have a router with four Ethernet connections. Is there a way to connect the two routers to have wireless plus at least six wired connections? Bubba73 You talkin' to me? 20:22, 16 January 2015 (UTC)


 * You need a simple Ethernet hub or switch. I don't know if the hub/switch built into your second router would work for that.  I suspect it would cause you problems, and that you'd be better off with a simple, dedicated hub or switch.  I believe you can get them quite cheaply. —Steve Summit (talk) 20:32, 16 January 2015 (UTC)


 * (ec) Thanks, I'm looking at a hub that has five ports but they look the same. So do I just run an Ethernet cable from the router to one of the ports on the hub, and that adds the additional Ethernet ports?  Bubba73 You talkin' to me? 20:58, 16 January 2015 (UTC)


 * Right. Once upon a time you needed a crossover cable, and/or you needed to make sure to connect to the distinct "uplink" port on the secondary ("downstream") switch/hub, but that was a significant nuisance, so these days, just about all ethernet ports include autosensing circuitry so you don't have to worry about that. —Steve Summit (talk) 22:19, 16 January 2015 (UTC)


 * My router has a port for the internet but then the other four ports just say "Ethernet". The hub I'm planning to buy doesn't seem to have any difference in the labeling of the ports.  And I remember the crossover cables - I had to buy one (I still have it, unused).  And they were super expensive - about 3 or 4 times the cost of a regular cable - just because they have two wires switched on one connector! Bubba73 You talkin' to me? 00:38, 17 January 2015 (UTC)


 * You can connect the two and turn off DHCP on the second router so it is just a switch. But a new wireless router with more wired connections will probably have better wireless protocols. --  Gadget850talk 20:54, 16 January 2015 (UTC)


 * My current router has 802.11n (which is good enough) and gigabet wired Ethernet. It isn't too old, so I don't want to purchase a new one.  Bubba73 You talkin' to me? 21:08, 16 January 2015 (UTC)

The hub will make each computer show up on the home network, right? (not just share the internet connection). Bubba73 You talkin' to me? 01:23, 17 January 2015 (UTC)
 * I wouldn't recommend a hub, the difference can't be more than a few dollars between a hub and a switch nowadays if you ever actually find one. That said, since hubs have been basically dead for the vast majority of home user products and as our article said never took off in the GbE world anyway, it's fairly unlikely you're looking at a hub. Anyway whether you by a new switch, or reuse the second router the devices should see each other like normal provided they are on the same subnet (and have the same Vlan tagging, if any). As has been suggested, if you reuse the second router, it'll probably be best to turn off any DHCP, wireless, and perhaps firewall although it's fairly rare the LAN traffic will be firewalled. But really just turn off as much as you can. Technically you don't have to turn off the DHCP, you could for example provide a separate range of IPs but in the same subnet and with the appropriate gateway address etc, and allow each router to act as a DHCP server, but that will add additional complications so should only be done if you really can't turn of the DHCP server on the second router. The only thing to bear in mind is the connection speed between devices on separate switches will be more limited. If you have device A, B, C & D on a network switch, devices A & B would for example be able to have a full duplex 1 GbE available between them and so C & D (or whatever two devices). But if you have a single link between the two GbE switches, and it has devices W, X, Y, Z on it, A & W; and X & B could edit:not both communicate with each other with a full duplex 1 GbE connection. Nil Einne (talk) 11:53, 17 January 2015 (UTC)


 * As others have pointed out, connect a switch to your router. The router doesn't care that there are several computers connected to one of its ports, it will still handle DHCP for all of them. A normal ethernet cable, for connecting your computer to the router, will be appropriate to connect the router to the switch, as autonegotiation of link-speed and cross-over has been standard for years now. LongHairedFop (talk) 14:52, 17 January 2015 (UTC)


 * Yes, I read up on the difference between a hub and a switch and decided that a switch was better. I've ordered a Netgear GS105 ProSafe five-port switch, unmanaged.  Bubba73 You talkin' to me? 15:56, 17 January 2015 (UTC)