/dpt/ - Daily Programming Thread

Last thread: What are you working on, Sup Forums?

Other urls found in this thread:

gnu.org/software/libc/manual/html_node/Finding-Tokens-in-a-String.html#Finding-Tokens-in-a-String
twitter.com/NSFWRedditVideo

C is the only language

... that sucks.

C is the worst language.

*asm

Define functional programming in under 20 words.

>Define functional programming in under 20 words.
Programming with pure functions as the base mechanism of abstraction.

...

programming with functions
derivatives of lambda & pi calculus

Lambda Calculus

defmodule Lambda do
def eval({:lambda, x, body}, env) do
fn(arg) ->
eval(body, fn(y) -> if (x == y), do: arg, else: env.(y) end)
end
end

def eval({rator, rand}, env) do
(eval(rator, env)).(eval(rand, env))
end

def eval(x, env) when is_atom x do
env.(x)
end
end

Horrendous definitions, lads.

here.

Yeah, but that's a shame, my printing function does not mutate the strings pointed by the array pointers. Why wouldn't "char **" be incompatible with "const char *const *"? Why is the conversion deemed unsafe in this particular way?

Do lisplets honestly believe homoiconicity is the key to metaprogramming?

I eventually want to mutate the strings from the array pointers. The const qualifier would prevent me to do that and making a copy of the array of "char *" to an array of "const char *" every time I call the printing function sounds like a waste of time and space.

when you program with the functional subset of the procedural set.

You can't mutate string literals. If you do it's UB.

I carefully made sure that every string in the program is not a literal.

char str0[] = "probably";
char str1[] = "maybe";
char str2[] = "surely";
The literals here only serve for initialization. It's safe to mutate str0, str1 and str2 after then.

>Tell me what's wrong with this:
What do you get told is wrong?

Passing 'char **' to parameter of incompatible type 'const char *const *'.

I know the language only allow non-const to const implicit conversion for first-order types, but it looks fishy to disallow it for n-th order types. How is it unsafe?

>i = 1; i < nmeb
Why don't you just use i = 0 instead of that if statement bullshit?

I don't want a trailing "," at the end of the loop.

>language comprehensibility relies heavily on whitespace

That's not true, most languages suck.

>Not having whitespace sensitive operator precedence

Do you guys ever work on something for a whole day, feel really proud of the result and then the next day you don't want to even go near that code?

I don't know what it is, just not feeling it like I was yesterday.

I think I'm just gonna jerk off to anime for half a day and see how I feel afterwards.

Liquor helps with that.

It's 8:30AM here.

>Do you guys ever work on something for a whole day, feel really proud of the result and then the next day you don't want to even go near that code?
Story of every day of my life. Can't finish personal projects due to heavy autism.

It's saturday though.

Yes, when learning a new language I usually look at the old code in disgust. Recently it was my re implementation of GenServer before checking what GenServer was. Didn't know what handle_info was for so rolled out a custom loop which later I found was pointless as I could just have used handle_info + Process.send_after(self(), msg, delay).

Using GenServer after was pretty rewarding though since it proved to be much simpler than I had first thought it would be to get a process to run every T seconds. (I'm writing a monolith on the erlang vm to replace most of the scripts I have around to learn properly and make it easier to track them)

I'll pass on the morning drinking. Maybe chugging a pot of coffee will help though.

I'm trying to debug some undefined behaviour in my C program involving string manipulations with multiple threads and mutex locks. I've got a ton of printf statements starting from the first line of my main program. However, when I get a segmentation fault, which happens only one out of every 10 runs, I don't get a single print statement - straight to a segmentation fault. What could be going on behind the scenes such that I try to access memory I shouldn't before the first line of code is even executed? Valgrind and strict gcc compiler options report nothing

Do you have system set to make core dumps on segfaults? Just run your program until it crashes and open the core dump with gdb.

>multiple threads and mutex locks
sound like hell, tried message passing between them? Shared memory is a pain,

If your system uses systemd, you probably have the dumps from your previous segfaults stored.

$ coredumpctl list

I have no idea why systemd has that but it does.

>his language spec doesn't guarantee tail recursion

Is it a warning or an error? As far as I know that conversion should be a-ok with C++.

>his language spec has to guarantee recursion because it was designed by people that can't comprehend looping constructs
When will funclets learn?

I enabled core dumps and this is what gdb reports initially. This is my first time reading a core dump - although I didn't even use strtol, I used atoi.
[New LWP 30519]
[New LWP 30514]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./asn3.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 __GI_____strtol_l_internal (nptr=0x0, endptr=0x0, base=10,
group=, loc=0x7f2ea3824400 )
at ../stdlib/strtol_l.c:293
293 ../stdlib/strtol_l.c: No such file or directory.
[Current thread is 1 (Thread 0x7f2ea145d700 (LWP 30519))]
(gdb)


>message passing
Sorry, what do you mean?

enter the command 'bt'

(gdb) p $_siginfo._sifields._sigfault.si_addr
$1 = (void *) 0x0
(gdb) bt
#0 __GI_____strtol_l_internal (nptr=0x0, endptr=0x0, base=10, group=,
loc=0x7f2ea3824400 ) at ../stdlib/strtol_l.c:293
#1 0x000055b72524bcf9 in atoi (__nptr=) at /usr/include/stdlib.h:241
#2 client_func (arg=) at asn3.c:68
#3 0x00007f2ea38306da in start_thread (arg=0x7f2ea145d700) at pthread_create.c:456
#4 0x00007f2ea356ad7f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:105


The first command looks like I accessed a null pointer, which is still surprising that I didn't even print the first printf statement

You should probably -O0 when you debug so you don't get all shit.

Maybe this is some bug but I've tried to disable optimization with -O0 but I still get that same optimized out. Should I remove the '-g' flag?

I compile with gcc -std=c99 asn3.c -g -pthread -o asn3.out

No, without -g you lose at the debugging information. It should work if you add -O0 to that, don't see why it wouldn't.

non-python whitespace languages are pretty nice.
I think they get a bad rep, but a good one can kill most bike-shedding/format wars, and make even the pajeetest pajeet write pretty code.

You're right I used it manually instead of through make and I got some more output:
(gdb) bt
#0 __GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0,
base=base@entry=10, group=group@entry=0,
loc=0x7f123fa33400 ) at ../stdlib/strtol_l.c:293
#1 0x00007f123f6ac642 in __strtol (nptr=,
endptr=endptr@entry=0x0, base=base@entry=10) at ../stdlib/strtol.c:106
#2 0x00007f123f6a81e0 in atoi (nptr=) at atoi.c:27
#3 0x0000559168092dca in client_func (arg=0x5591686db8c0) at asn3.c:50
#4 0x00007f123fa3f6da in start_thread (arg=0x7f123e66e700)
at pthread_create.c:456
#5 0x00007f123f779d7f in clone ()
at ../sysdeps/unix/sysv/linux/x86_64/clone.S:105

Looks to me like you are using pthreads incorrectly and the implementation is segfaulting, which would explain why you're seeing optimized-out things even though you're not optimizing.

>non-python whitespace languages are pretty nice
Cite one senpai.

Disregard that, I suck cocks.

Could be. I have an array of thread which I call in a loop.
pthread_t thread[num_client - 1]; // each client is a thread
.
.
.
if (pthread_create(&thread[client], NULL, &client_func, (void*)str[client]) != 0) {
printf("\nError creating thread %d.\n", client);
return 1;
}

Idris and a lot of functional languages.

That's your brain telling you it needs a rest, nothing more. Trust me: do today something completely different, ideally go outside for a walk to rest your eyes, tomorrow it'll be different.

That gif made me cum

>C is the worst language

Then what the fuck does that make C++?

...

Just as inconsistent and shitty, but now there's more of it.

Or just use -Og.

Pi calculus isn't fucking functional programming.

keep telling yourself that

shit

>Idris
>tfw you get the next james bond when you search for a programming language

>a calculus with no functions is functional programming

It's white-space requirements are minimal, but an example of one that benefits.

also stop shitposting on Sup Forums so much then.

you could model it with arrows

You can model assembly with monads but that doesn't make it functional.

>

lol

Hey guys I have 2 questions I hope you can help me with.

1. After you finally learn one language and understand the basic premise, does it make it easier to learn others?

2. What's the easiest way to pick up programming? I've tried using books but it was difficult. I use codeacademy and that helped quiet a bit but still not as fas as I'd like to learn. Suggestions?

I did a simple game in Java swing.
Used JPanel to display. Like every other time I launch the program panel doesn't display anything and other times it works just fine.
Any idea what could cause this?

wtf is wrong with her hind

1. yes
2. learn by doing. Pick up any book and do all exercises it gives you, don't skip

Just looks like you're calling atoi(NULL), what's asn3.c:50?

It's a disease by which name I forgot that makes some part's of the afflicted's body more prone to synthesize fat cells that others. Simply stated, she's obese.

I'm doing C and Java for school, which other language should I learn and play with whenever I'm bored?

Nim, and learn a functional that's not Lisp.

Basically I'm reading from an input file with space separated numbers. This is line 50:
num = atoi(token + 3);

From some tests it looks like sometimes strtok() is returning a NULL token when theres a perfectly good token waiting to be parsed. I can't explain it.

Definitely not Lisp.

Something functional. Haskell or Scheme.

Are you using strtok() in several threads? strtok() is not thread-safe, use strtok_r().

Don't bother learning a pure functional language, but do learn something with proper functionals. Like F# or Scala or even C#. Java's "lambdas" are barely-masked anonymous classes and it's a tool you really should learn how to use.

>Are you using strtok() in several threads? strtok() is not thread-safe, use strtok_r().
Fucking hell, yes I am. I will try that - I really hope that was it ;_;

>which is still surprising that I didn't even print the first printf statement
Threads are worlds of fun. For educational purposes: use gdb to step through your program from the start to see where your entry point is and in what order your program actually executes in.

>or Scala or even C#.
Stop giving bad advice.
Scala is awful and LINQ isn't real functional programming.

Is it like an unwritten law that any term like "_____ programming" loses all meaning eventually?

Can't you just write pure functions and have no global state in an imperative language? Why do we need functional languages?

Don't listen to these tinfoil faggots trying to drag you into their irrelevance, Scala may have problems but the only reason they hate it is because it's JVM. Or you could try Kotlin; it's rather young and I haven't tried it, but it's supposed to fix a lot of Scala's issues.

And "LINQ is not real functional" doesn't even make sense. Unless you're using the SQL-esque syntax LINQ is just a library that uses C#'s functional functionality. You can make your own. It's lazy-loaded, it's got tail syntax, and it can be as immutable as you want it to be. It's as functional as you want it to be.

I could fucking kiss you user. It works. Just to think if I didn't happen to mention strtok() in the previous post I would never have thought about it. Thank you so much.

They certainly are. I know now to always be considering whether anything I'm doing is thread safe

>can't you just do functional programming in an imperative language?

Statements are kinda useless if they don't have side effects. It's just getting reduced to functional programming at that point with more boilerplate.

C doesn't even have true immutability.
And they weren't designed for the paradigm so it'd be an uphill battle.

Though I suppose you could argue that a language with mutable references and borrowing like Rust could very well be pure and still benefit from being imperative.

I think my problem is I don't understand what functional programming really is.

You might of figured it out eventually. The way strtok works is not magic, you pass it NULL and you get back something you didn't give it on that call. It stores the position in an internal char *, which is shared by all your threads.

If 2 threads are using it at the same time no good can come from it.

What retard was responsible for strtok?

A cnile

>I don't understand what functional programming really is.
No unintentional side-effects (you only receive what you put in to a function.
No mutation. But you can do immutable mutation which is just returning a new, changed, immutable copy.
Instead of iteration and loops you use tools like map and filter to work with lists and other things.

Those are the three most popular aspects to it, but look at Agda or Idris or F* or even Pony if you want a language designed around it.

I honestly just read strtok tokenizes string and started using it without knowing what's going on behind the scenes. That's probably a mistake when programming in C.

I sort of wish gcc would recognize I'm using pthread, and using strtok inside the thread function and warn me. But I guess gcc has to stop somewhere

You might want to reference the glibc manual.

gnu.org/software/libc/manual/html_node/Finding-Tokens-in-a-String.html#Finding-Tokens-in-a-String

>char * strtok (char *restrict newstring, const char *restrict delimiters)
>Preliminary: | MT-Unsafe race:strtok | AS-Unsafe | AC-Safe | See POSIX Safety Concepts.

>Function: char * strtok_r (char *newstring, const char *delimiters, char **save_ptr)
>Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

The MT means Multi Thread.

the pragmatic answer is javascript

I didn't even know this was a thing. Those preliminary headers are very handy, thanks