/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?

Old thread:

Other urls found in this thread:

github.com/sushihangover/CursesSharp
github.com/rnowley/csharp-ncurses
twitter.com/SFWRedditVideos

thank you for using an anime image

Thank you for using an anime image
java is trash btw

Lisp > Haskell

Beginner here.

Is there a way i can make this shorter?

>lispfag can't even use his own language correctly
kek

Stop using such a shitty, verbose language, perhaps?

see

>using stdin for human input

TRIGGERED

(> lisp haskell)

better?

>lisp
>haskell

switch (guess.CompareTo(random))

what else are you supposed to use? and why?

>human
mate...

>mate
user...

Command line arguments, but those don't really work for that example.
However, almost no "real" programs are interacted with in that way.

Stop.
You're not funny.

Thanks user, gonna try it

rude

Bah, you tried.

(define-monad
(>>= (lambda (a f) (f a)))
(return (lambda (x) x)))

It's fine for now, can have a look at ncurses if you really want to dig a hole before you try to fly.

stdin works... just need to check if the input is sane

Tried writing some GL graphics code in Rust to see if it could be practical and if I'd turn into an SJW land whale in the process. Results are inconclusive at this time, for both questions.

However, I'm now quite convinced the GL specs are absolutely retarded about data types. I know old school C doesn't give a shit but this is supposed to be a well-thought out standard of some sort and it keeps mixing unsigned, signed and even pointers(!) with each other as if they didn't matter; all of those require a cast in Rust, making the API look uglier than ever.

It's the Unix way :^)

>can have a look at ncurses
>C#
How?

ha, at least it works... OepnGL in chicken scheme is a pain unless you use glls... oh and then there are no docs covering the "schemefied" functions apart from one saying "it's like the C binding" (which it's not).

github.com/sushihangover/CursesSharp
github.com/rnowley/csharp-ncurses
ect...

you know you have to put braces around multiple statements after an if?

in your case you'd execute only the first statement after each if and execute the second intended one everytime. You'd print "you have... guesses left" every loop

*execute the first statement after the "if" if it was true and the second one everytime

if he switches to python he can save it

His code doesn't even work because he's missing curly braces. But that doesn't matter. What actually matters is the language amirite? This is proof that Sup Forums cannot program.

Yeah, thanks.

I had it in braces before and it worked, then i removed them because it made the code shorter, the compiler didn't list any errors so i thought it was ok.

Have to remember to test it all the time

You're the proof Sup Forums can't program, since your autism can't even comprehend that maybe the screenshot doesn't include irrelevant parts of the program, such as the using statements.

Kill yourself.

(unless (any (lambda (lang) (eq? +user-lang+)) (list lisp haskell c)) (post (gen-generic-Sup Forums-post))

never leave out braces

shorter doesn't mean better

Has anybody tried Xamarin?

>maybe the screenshot doesn't include irrelevant parts of the program, such as the using statements
So curly braces are using statements. hmm ok.

I just finished working through K&R and I want to do some shit in C. What are some good little open-source programs to hack?

I'm currently using dwm as window manager and it looks like the perfect project to modify (completely written in C, very small and well documented), but I'm not that experienced yet and I don't think I can into X programming yet. If anyone knows any projects in a similar vein I'd love to know about them. I'm on Linux btw.

what the hell are "using statements"?
like (using (glfw3) (with-window 620 480 "" (program)))?

nice pic btw

rewrite in order:
cat, wc, ls, hexdump, brainfuck, little man computer, match brackets, small scheme, something with sdl, something with opengl 2.0 (3.3 is a fucking pain)

thanks for posting an anime image.
dwm seems okay, i haven't touched it myself but i have heard multiple people of about your level recommend it for the exact same purpose
this

lsw, sselp, dmenu, dwm, st, surf
quite a nice intro to X11 and other libs

>opengl 2.0 (3.3 is a fucking pain)
If you know exactly what you're doing, GL 3.3 (and maybe a 4.x extension or two) are basically GL: The Good Parts. Supported on just about any PC and some mobile hardware, and potentially very fast.

Figuring out how to use it, however, _is_ a fucking pain because of all the outdated tutorials and even old API specs still maintained on the web.

c# equivalent of #include

Xlib isn't hard at all actually, just skim through the docs a bit and see yourself. you could add notification functionality using glib / gtk+ or a clock, or implement the ability to make windows vertical, those are the things i'd like to see in dwm
you could also make a decent terminal emulator to replace st, its god awful

Most of the suckless stuff is very simple and seems somewhat easy to fuck around with.
If you're seriously inexperienced with C though, it would probably be a better idea to write a few programs of your own first to get somewhat decent at it.
Trying to dive right into "real world" programs is probably just going to be overwhelming and confusing.

even worse when using scheme as there's even less tutorials for that... and half of the things make it almost impossible to run 3.3 as only glls works properly if I don't implement half of it in C.
see it's the suckless beginner path with hello world (lsw) ect...

learning some C++

say I want to write a generic binary tree. how do I ensure the type used in the template can be compared?

something like class BinTree in Java

still doesn't fix his code

of course not
i just told that user what a using statement is

I'm stuck with a problem within MFC (which I have to use because reasons): I want to have a window that keeps its aspect ratio no matter what. Drag? Simple enough. Maximize? Yeah ok, capture the WM_ and do stuff. But tiling operations such as WINKEY + RIGHT? Get fucked. Anyone know of an implementation I could look at?

don't bother, C++ is awful, just do a regular template and use

>how do I ensure the type used in the template can be compared?
You can't. You just HOPE that the type overloads the comparison operators and actually returns at least a partial ordering from it.
You really shouldn't use C++. It's complete garbage.

yes, Javascript is a shitty language
but so is, for example, C (if you're saying type soundness / coercion is the principle behind quality).
struct s { void * d; };
struct s o;
*((int*)&o) = 42;

lmao is this real? rust doesn't have this problem

>*((int*)&o)
That's undefined behaviour, you fucking idiot.

that's because rust is new, C++ hasn't ripped off haskell's type classes yet, concepts were delayed

guess it lags behind too much to be useful
does the adress in d cast to int point to 42? thats efficient as fuck

well fuck

how do libraries solve this problem? are you supposed to get that from the error messages or is there some way of annotating this?

there is no decent way of avoiding this, unless you want to fucking do a base class just for comparison

btw i got it
thank you based opencv template matching

>even worse when using scheme
Ah ok, I can understand that. At least from my brief experiment with Rust it's only casting, casting everywhere.

And no working offsetof --- better explain those magic numbers in comments or something.

There was a proposed C++ feature called Concepts that would have implemented this, but the language is already so fucking complicated they couldn't polish all the details in time for the last few editions of the standard. It almost got into C++17 but was rejected by the committee.

Right now if you pass an incompatible type as a template argument it'll just get going and generate invalid garbage, then shit out several screens of failed resolution attempts. It can get pretty unreadable, especially with the standard library.

yep. my point. UB is still part of the language. (or the lack of.. just like implementation-defined, unspecified and well-defined behaviors are). the guy I'm replying to is saying javascript isn't a real language because it has some memetastic qualities. if thats true then a lot of languages aren't "real".

Scheme is blessed with obscure examples (about 4 of them in total on 4 different gl eggs) and hardly any docs. I'm slowly translating a C++ tutorial to scheme

Wait, that's not C

newbie here

So I made this little program to find the fraction of whatever number you type in (num) within the range of 1-rng

so for example if i want to find the number 1.47 within the range of 25 it will return 22/15, actually a tuple of (22, 15) which is an approixmation for 1.47. The higher the rng the higher the accuracy.

How should i make it more effective and quick? When rng is high it takes the loop forever to get it done. i need a more effective algorithm guise

R?

is this good? first program ever.

#include
#include

void print_plus_one(void) {

static int a = 0;
printf("%d\n", a);
static bool reachedTen = false;

if(a == 10){
reachedTen = true;

};

if (reachedTen == false){
a++;
};
if(reachedTen == true){
a--;
};

}

int main(void) {

for(int i=0; i

It doesn't seem like anyone answered your question, user.
I just had a go at reducing the LoC by making it harder to read:
static void Main(string[] args) {
Console.WriteLine("I'm thinking of a number between 1 and 10, you have 4 changes to guess which one");
var output = new[] { "Higher", "Lower", "You won", };
var answer = new Random().Next(1, 10);
var rules = new List { a => answer > a, a => answer < a, a => a == answer};
for (var i = 3; i >= 0; i--) {
var guess = Convert.ToInt32(Console.ReadLine());
var attempt = rules.Select(rule => rule(guess)).Select((val, index) => new {val, index}).First(result => result.val).index;
Console.WriteLine(output[attempt]);
if (attempt == output.Length - 1) break;
if (i == 0) { Console.WriteLine("You lost"); break; }
Console.WriteLine("You have {0} {1} left", i, i > 1 ? "guesses" : "guess");
}
Console.WriteLine("Answer was: {0}", answer);
}

>== true
>== false
it's terrible

but at least you got your braces mostly right

why is it terrible?

from fractions import Fraction

def fract(num, rng):
return Fraction.from_float(num).limit_denominator(rng)

I'm dumb, reading thru the code I just realized that linq bullshit is too complex for what could happen:
// this is overly complex
var attempt = rules.Select(rule => rule(guess)).Select((val, index) => new {val, index}).First(result => result.val).index;
// shorter and faster, not that it matters on a 3 element list
var attempt = rules.Select(rule => rule(guess)).ToList().IndexOf(true);

You can have
if (bla)

and
if (!bla)

to check whether is bla true or false.

What kind of output do you expect from the program?

How are you enjoying your job so far?

Oh wait.

gotcha. is that a convention?

just to satisfy my curiosity, do these two implementations compile to the same machine code? the only difference is the C code, right?

from random import randint

rnd = randint(1, 10)
print("I'm thinking of a number between 1 and 10, you have 4 chances to guess which one")

for i in range(3, -1, -1):
guess = int(input("Your guess: "))
if guess == rnd:
print("You won!")
break
else:
print("Higher!") if guess < rnd else print("Lower!")
print("You have %d guesses left" % i)
if i == 0:
print ("You lost!")

If this is your first time and got a program to compile, congratulations I guess.

static int a = 0;

Don't use static unless you have a very good reason to. It can get confusing as hell if the function is called from multiple places; the different invocations of the function will refer to the same "a" variable and possibly affect each other in surprising ways. You can get the same output from a program that doesn't have this problem.

if (reachedTen == false){
a++;
};

The semicolon after the closing brace does nothing (compiler probably parses it as an empty statement following the if's block, but does not necessarily emit any code).

If you're using gcc or something compatible, always compile your code with "-Wall" to get as much feedback about silly code as possible.

...

It will, given a decent enough compiler, generate the same instruction. A comparison to == true is more explicit and clear so may help you, as a novice, understand. However, it's a convention used by seasoned programmers.

0
1
2
3
4
5
6
7
8
9
10
9
8
7
6
5
4
3
2
1
0

it works

also why include stdbool.h if you don't have to?
remove it, and the true/falses and now its portable ANSI C and doesn't require a C99 compiler..

** It's NOT a convention used by seasoned programmers.

Reminder: you can solve many problems in your code by yourself if you won't just put your code into the compiler but thoroughly execute your code manually, writing down the state of the program with every line of code.

i was using gcc, but my editor is using a linter which is compatible with c99. didn't realize i didn't need it

what language will get me a job quickly

or you can step through it with a debugger

ANSI C didn't have explicit boolean type, so it performs comparison with 0.
!bla will convert bla into 0 if there was non-zero value, or into 1 if there was zero.

spanish -- for when the wall gets built

Probably Java, C#, PHP or JavaScript, but you're basically lower pajeet tier if you know 1 language and have no feel or theoretical knowledge for how/why it even works.

well, i removed it, but i couldn't figure out how to use _Bool quickly, so I rolled my own. i mean, defining a boolean isn't hard. this language is serious podracing

#include
typedef int bool;
#define true 1;
#define false 0;

void print_plus_one(void) {

static int a = 0;
printf("%d\n", a);
static bool reachedTen = false;

if(a == 10){
reachedTen = true;

};

if (!reachedTen){
a++;
};
if(reachedTen){
a--;
};

}

int main(void) {

for(int i=0; i

#define true 1;

static bool reachedTen = false;


Careful with macros: #define is not a C statement and doesn't have to end with a semicolon. This preprocesses into static bool reachedTen = false;;.

and

That looks ugly.

#include

void print_plus_one(void) {
static int a = 0;
printf("%d\n", a);
static int reachedTen = 0;

if (a == 10) {
reachedTen = 1;
}

if (!reachedTen) {
a++;
} else {
a--;
}
}


Keep it simple, stupid.

Should probably have copypasted the line with #define false on it.

Also, you could just use 1/0 as true/false like real C programmers. Unreadable crap means you're either a super wizard or at least have breddy gud job security.

>A language should ideally support many concepts in a well-factored way, so that the programmer can choose the right concepts whenever they are needed without being encumbered by the others.
>This style of programming is sometimes called multiparadigm programming, implying that it is something exotic and out of the ordinary.
>On the contrary, in our experience it is clear that it should be the normal way of programming. Mainstream languages are far from supporting this.
>Nevertheless, understanding the right concepts can help improve programming style even in languages that do not directly support them, just as object-oriented programming is possible in C with the right programmer attitude.

>this is an actual paper we have to read

...

And? Grokking multiple paradigms is generally a good thing.

I took a course on Haskell at uni, never ended up using it for anything relevant but I think my C++ (and JS) improved slightly from focusing on functional programming for a while.

Sure, but I thought that saying "object-oriented programming is possible in C with the right programmer attitude." is about as retarded as saying you could, in theory, build a functioning computer out of bottle caps and turds. You can, but why the fuck would you?

Is Clojure any good?