Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 574 575 [576] 577 578 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907121 times)

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8625 on: December 08, 2015, 11:59:45 am »

Here's one dedicated to that purpose:

Stack Exchange's Code Review Section

If you're not familiar with the Stack Exchange sites in general, then I'd highly recommend them.  Stack Overflow in particular is a great wealth of information on specific programming problems, but I do advise you to be careful about searching for questions on that particular site before asking any new ones, since most things have been asked a dozen times and closed as duplicates with extreme prejudice.

The code review site is much newer and obviously subjective, so it should be more accessible.  Having said that, I've never asked for a code review there before, so I don't know exactly what requirements they have for posting code snippets.  Be sure to read the rules.
Logged
Through pain, I find wisdom.

jaked122

  • Bay Watcher
  • [PREFSTRING:Lurker tendancies]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8626 on: December 08, 2015, 12:25:16 pm »

Recursion is a powerful and compact notation, but it's very costly in terms of memory and speed. There's basically no situation where you're not better off just using a loop in terms of computer resources. But it could have readability advantages some times for code that you know won't get called very often, so saving in programmer time.


Tail call optimization.


 That's all I'm going to say. Lots of languages have this capability, I think that gcc can do it for some circumstances in C and C++, Haskell is useless without it, Ocaml is also useless without it, Scheme is generally supposed to use it when available(not all implementations get around to doing this), common lisp seems to do it sometimes... if you use the right lisp(Clozure common lisp, SBCL, maybe CLISP).


Of course,while you can implement DFS using a loop in any language, not using recursion is pointless because you need to use a stack anyway.


Anyway saying that you shouldn't use recursion isn't quite good advice. Recursion should only be done if it makes sense to, I.E. you use the stack in order to expand upon computations compactly.


It shouldn't be used where loops make more sense, but if it is harder to implement as a loop than as a recursive function, then perhaps it might be a case where it makes sense to use.
« Last Edit: December 08, 2015, 12:52:40 pm by jaked122 »
Logged

breadman

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8627 on: December 08, 2015, 04:57:13 pm »

Recursion is a powerful and compact notation, but it's very costly in terms of memory and speed. There's basically no situation where you're not better off just using a loop in terms of computer resources. But it could have readability advantages some times for code that you know won't get called very often, so saving in programmer time.
I ran into a situation once where it was called for.  It only went six or seven levels deep, but had a messy state at each level, calling the next level many times before returning.  Yes, I could have used an array for each variable, but recursion was much easier to understand.

I've since lost all copies of the program, but its result is prominently displayed on a U.S. Patent.  My father even decided to frame it for me.
Logged
Quote from: Kevin Wayne, in r.g.r.n
Is a "diety" the being pictured by one of those extremely skinny aboriginal statues?

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8628 on: December 08, 2015, 05:20:43 pm »

i would like to buy your patent

i will put it into production, and call it the recursatron

name your price, but know that the only currency i have available is honor and good wishes
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #8629 on: December 08, 2015, 05:26:48 pm »

I can think of a function where recursion is required. The Ackermann function.

Code: [Select]
int Ackermann(int m, int n)
{
    if (m == 0)
    {
        return n + 1;
    }
    else if (m > 0 && n == 0)
    {
        return Ackermann(m - 1, 1);
    }
    else
    {
        return Ackermann(m - 1, Ackermann(m, n - 1));
    }
}
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8630 on: December 08, 2015, 05:34:02 pm »

Patent?  Seriously?
Good job?
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

DrunkGamer

  • Bay Watcher
  • Beer loving Gentledwarf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8631 on: December 08, 2015, 05:43:28 pm »

Hey guys, I want to hear your opinion on something. (I'm annoying everyone with a knowledge of programming better than mine with it  :P)

What do you believe is better? To read a bit about any language and then teach yourself the rest by googling, reading snippets and all that stuff (Slower, but "easier", so to speak) or reading a book entirely and doing what it says?
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8632 on: December 08, 2015, 05:53:38 pm »

the former

it's not really slower, you get results faster that way too

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8633 on: December 08, 2015, 05:55:52 pm »

Yeah, I prefer the former. Books can be really useful too though, especially with concepts you're having trouble with on your own. Just following them 100% is a bit much.

(I'm annoying everyone with a knowledge of programming better than mine with it  :P)
Don't worry about it. The thread exists to discuss programming, at all levels. If people are annoyed by someone making an effort to learn then they can sod off.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8634 on: December 08, 2015, 06:41:42 pm »

Most of the time I learn via Internet, but to learn unusual concepts I find that most of the time books are better for that purpose.
To get behind Shen's type system I read "The Book of Shen" (of course) and to achieve a deeper understanding of macros in Common Lisp "Let Over Lambda" was quite useful.

So, stick with small-scale stuff for now but if you encounter something that interests you and you want to get a little bit deeper into that specific topic, try to find a book on it.
Logged
Taste my Paci-Fist

Moghjubar

  • Bay Watcher
  • Science gets you to space.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8635 on: December 08, 2015, 07:04:29 pm »

The best way to learn is to make something.  And not something insane, something like a mini-text rpg with dice rolling and input/output, etc.  Yes, this is simple, it doesn't have to be Cataclysm.

Theres definitely merit in following tutorials or reading a book all the way thru, but IMO: get your basic setup done, figure out what you want to do, then start doing it and learning all the actual references you need to look up.  You will run into roadblocks, sure, but you can get past them.   Your drive to complete the project will help, instead of just doing checkmarks and saying 'I read 5 pages today' or 'I did the exercise section' or whatever.

Past that, you can just keep making stuff all the way up, and maybe getting some use of what you make too, instead of just dumb tutorial projects.
Logged
Steam ID
Making things in Unity
Current Project: Demon Legend
Made This too (publisher abandoned ) Farworld Pioneers
Mastodon

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8636 on: December 08, 2015, 07:27:52 pm »

Hey guys, I want to hear your opinion on something. (I'm annoying everyone with a knowledge of programming better than mine with it  :P)

What do you believe is better? To read a bit about any language and then teach yourself the rest by googling, reading snippets and all that stuff (Slower, but "easier", so to speak) or reading a book entirely and doing what it says?
I find I'm motivated by constant progress.  So googling stuff is great because once I get it to work I can immediately see a functioning result to my code.
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8637 on: December 08, 2015, 09:15:41 pm »

I can think of a function where recursion is required. The Ackermann function.
100% Required or just preference? In my opinion it's extremely rare to find an algorithm which absolutely cannot be converted between iteration and recursion if you put some effort in.

A quick google shows that it's possible to write a loop version, and I'm pretty sure that the loop version could be optimized faster.
http://stackoverflow.com/questions/10742322/how-to-rewrite-ackermann-function-in-non-recursive-style
« Last Edit: December 08, 2015, 09:17:43 pm by Reelya »
Logged

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8638 on: December 08, 2015, 11:38:55 pm »

Fussing around with SMTP for a class. I can work my way through sending an email manually over Telnet no problem, but for this assignment we need to automate it. I can get through most of it, but for the life of me I cannot figure out how to end sending the message body. I know you send a "." on a line by its own, mind. I just never get the expected 250 reply from the server.

Code: [Select]
if (send(sockFD, ".\r\n", 3,0) != 3)
{
        perror("Error sending message.");
        (void) close(sockFD);
        exit(1);
}

I have tried every variation on the above that I can think of - writing the text to be sent to a fixed-length buffer and calling send() on that instead of a string, every combination of the characters ., \r, and \n that I can think of from . alone to being flanked on both sides by \r\n, and appending it to the last part of the email body instead of sending it in its own packet.

halp

y it no work
« Last Edit: December 08, 2015, 11:51:12 pm by Bauglir »
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #8639 on: December 09, 2015, 06:53:26 am »

I can think of a function where recursion is required. The Ackermann function.
100% Required or just preference? In my opinion it's extremely rare to find an algorithm which absolutely cannot be converted between iteration and recursion if you put some effort in.

A quick google shows that it's possible to write a loop version, and I'm pretty sure that the loop version could be optimized faster.
http://stackoverflow.com/questions/10742322/how-to-rewrite-ackermann-function-in-non-recursive-style

Ah, Ok. I just couldn't think of one myself.
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.
Pages: 1 ... 574 575 [576] 577 578 ... 796