Programming

Discussion in 'General Chatter' started by Morven, Feb 26, 2015.

1. MorvenIn darkness be the sound and light

I've been using two free books, which are the first two listed on the page on the Haskell site: "Learn You a Haskell for Great Good", which is a friendly and somewhat humorous read but might annoy some; and the O'Reilly book whose name I forget. Both are free on the Web but cost if you want paper.

I do have an unfair advantage in that my freshman year of Computer Science used Miranda as it's teaching language, and Miranda can be considered "Haskell Lite". It doesn't have Haskell's imperative constructs for IO, nor monads, nor most of the advanced typing features, but the basic style is very similar.

2. ExohedronDoesn't like words

Thanks! I've gotten pretty deep into type theory and category theory from the mathematical logic side, so for me the later stuff will probably actually be easier than the beginning stuff.

3. MorvenIn darkness be the sound and light

Then I suspect it'll suit you quite well.

In general I'm enjoying its conciseness. Type inference means there's much less need to tell the system what you're doing because it can work it out. And the general idiom of the language appears to be one of elegantly expressed, minimalist solutions.

Also, the community seems super helpful and very keen on showing people how to do things.

4. winterykiteNon-newtonian genderfluid

Python global/local variables stuff

I've been writing a small text based adventure (300-ish lines) in python since a few hours, and I've finally gotten all the missing : and = sorted out, but it keeps erroring out because it apparently can't find a variable - or, rather, it assumes "chased" is a local variable (which it can't find), whereas it's a global variable.
I've tried having the list of global variables both above and below the functions, and neither works ::/ probably some dumb newbie mistake, but....
(https://www.dropbox.com/s/sz7ka54gd5sn2hr/ex37.py?dl=0)
(Warning: I haven't documented/commented this file at all.)

5. WithAnHSpace nerd

Put
global chased
inside the function, right after the def line.

6. winterykiteNon-newtonian genderfluid

@WithAnH thank you, worked like a charm!
(and i just noticed, I need to loop the start() room, otherwhise it just terminates. whoops.)

7. WithAnHSpace nerd

Let me know if you have any other problems - I'm happy to help!

8. winterykiteNon-newtonian genderfluid

I like it. It's certainly better than anything I've made. I will admit that I cheated, but only out of habit.

11. MorvenIn darkness be the sound and light

Heh!

For the record: This is NOT best practices. Try to either pass in the needed value (And return any values that change) or, for games, I like passing in a dictionary/object that holds necessary game states.

• Like x 1
13. Lissa Lysik'anDragon-loving Faerie

Globals in any language are a "bad thing". One reason is because it is VERY easy to lose track of where they could be modified and bugs from "it got changed in function Z in file A but I forgot that file A even exists" are REALLY hard to track down. Pass objects that contain the data structures you need - then you know where it came from and where it might get changed. In the languages I use, pass by const is preferred if you just want to look at the data and not change it, so you can't accidentally change it. And since it's const, if you even TRY to change it the compiler says "uh uh, you promised you weren't going to mess with that, you can't go back on your word now" and barfs and you see right away that you goofed.

• Like x 1
14. winterykiteNon-newtonian genderfluid

Im on ex37 of Learn Python The Hard Way, I'm not that far into python yet ::D
(and I needed some of the variables to not disappear / get rebooted to the starting value when the room is left/entered a second time)

15. MorvenIn darkness be the sound and light

Doesn't matter so much at that stage, but best to keep it in mind that it's really not the best way when you get into more complicated things. In general, global state means that things are harder to reason about, harder to debug, harder to test, and kinda messier.

16. winterykiteNon-newtonian genderfluid

::3 I'll keep that in mind
In Java I would've fed everything to a set of objects and called it a day

But yeah, I suspect it's only working at this point because I have less than 400 lines of actual code and it's pretty simple and straightforward.

You posted the code right?

In general, anything that might be "global" I pass into containing functions. Big example with Pygame (A games library in Python), is the Surface that represents the display. Since you draw to it constantly, you need to have it in every function that draws. So things like game loops. So my method is to instantiate it as part of the if __name__ == "__main__" and then pass it into the game loop functions.

Example:
Code:
```def main(display):
while true:
display.fill(background)
draw_stuff(display)
pygame.display.update()

if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode(resolution)
main(screen)
```

18. albedometasperg

• Like x 2

So I'm teaching myself Ruby with CodeAcademy, and I have Learning Ruby The Hard Way bookmarked for reference and further reading. Through trial and error and referencing other people's code on the Q&A forum I managed to make a histogram program thing for the course.

puts "Hello, I need input from you."​
text = gets.chomp
words = text.split()
frequencies = Hash.new(0)
words.each {|value| frequencies[value] += 1}

frequencies = frequencies.sort_by do |word, count|
count
end
frequencies.reverse!
frequencies.each do |word, count|
puts word + " " + count.to_s
end

frequencies.each do |word, count|
puts word + " " + count.to_s
end
I'm proud of myself c:

• Like x 3
20. albedometasperg

So, folks that have internetted/programmed for much longer than me... I've heard a lot about how back before the "Eternal September" (when non-specialists started getting internet access en masse), everything was civilized and everyone was inducted into a common culture. Chivalry and common social norms ruled the land! Everyone used proper grammar! It wasn't like these trolly places of today!

Some of the things those oldschool folks day about the Usenet days reminds me of the fedora crowd today. And I understand that Usenet users, being mostly college programming students/professors/professionals, were mostly middle-class white dudes.

Was it really a cool culture, for the most part? Or was there a lot of fedora nonsense that the fedoras of today see through rose-tinted lenses?