It's a good day.
Check it out/give me feedback: termbin.com
Just wrote my first linkedlist struct in C
Other urls found in this thread:
en.wikipedia.org
termbin.com
ada-auth.org
en.wikibooks.org
www2.adacore.com
en.wikipedia.org
en.wikipedia.org
en.wikipedia.org
felixcloutier.com
en.wikipedia.org
twitter.com
>>>/prog/
I thought I'd post on there, but it's slow as hell, and there are tons of posts that ought to belong on /prog/ here.
enjoy blowing your stack and crashing the program when you have more than a few elements
Retarded.
...
Does his nodeLen function look amenable to tco?
It's obviously an exercise in programming. It's retarded, but so is Hello World.
Well as long as the student realizes the shortfalls, then lesson learned.
Hello World will soon become the new FizzBuzz as more retards "learn2code".
These days you cannot even send someone a Hello World program to their device (iPhone) without paying Apple $100 for the privilege of signing the application, and that's if they still allow apps outside their appstore.
It's over. Programmers used to be able to share programs with people, now we live in a sanitized world where everything has to be approved by corporate. We lost. We fought amongst ourselves in silly KDE vs GNOME type battles, meanwhile the enemy plotted our demise with the iPhone revolution. We ceded ground to the hordes, and now they police our internet. There's no hope.
Explicit recursion is just as bad as GOTO
There is literally nothing wrong with goto
As apposed to what, explicit looping? recursion schemes?
You tell me
Use Android, fag.
Never use recursion, unless you know how to not blow a stack.
look faggot take a look at the 101
en.wikipedia.org
The code posted is literally not in tail position and cannot be TCOd. The compiler is doing something totally different here.
#include #include
What did he mean by this?
Yes, recursion schemes offer constructs for handling and reasoning about recursion just like while and for provide abstractions over using GOTO to loop.
Ah yes like the good old Zygohistomorphic prepromorphism pattern
No thanks
If it turns recursion into iteration, who cares how it happens?
Good, we need more C anti-rust shills here
scalar_list.ads:generic type Item is private; with function "+" (Left, Right : in Item) return Item;package Scalar_List is type Cell; type Node is access all Cell; type Cell is tagged record X : Item; Next : Node; end record; List_Out_Of_Bounds : exception; procedure Append (This : in Node; X : in Item); function Nth (This : in Node; Depth : in Natural) return Node; procedure Get (This : in Node; Depth : in Natural; X : out Item); procedure Set (This : in Node; Depth : in Natural; X : in Item); procedure Increment (This : in Node; Depth : in Natural; X : in Item); function Length (This : in Node) return Natural;end Scalar_List;scalar_list.adb:package body Scalar_List is procedure Append (This : in Node; X : in Item) is N : Node := new Cell'(X => X, Next => null); P : Node := This; begin while P.Next /= null loop P := P.Next; end loop; P.Next := N; end Append; function Nth (This : in Node; Depth : in Natural) return Node is D : Natural := Depth; P : Node := This; begin while D > 0 loop D := D - 1; P := P.Next; if P = null then raise List_Out_Of_Bounds; end if; end loop; return P; end Nth; procedure Get (This : in Node; Depth : in Natural; X : out Item) is begin X := Nth (This, Depth).X; end Get; procedure Set (This : in Node; Depth : in Natural; X : in Item) is begin Nth (This, Depth).X := X; end Set; procedure Increment (This : in Node; Depth : in Natural; X : in Item) is N : Node := Nth (This, Depth); begin N.X := N.X + X; end Increment; function Length (This : in Node) return Natural is Depth : Natural := 0; P : Node := This; begin while P /= null loop Depth := Depth + 1; P := P.Next; end loop; return Depth; end Length;end Scalar_List;listex.adb:with Ada.Text_IO; use Ada.Text_IO;with Scalar_List;procedure Listex is subtype Faggotry is Float range 0.0 .. 10.0; package Poster_List is new Scalar_List (Item => Faggotry, "+" => "+"); Posters : Poster_List.Node := new Poster_List.Cell'(X => 10.0, Next => null); F : Faggotry;begin Poster_List.Append (Posters, 3.0); Poster_List.Append (Posters, 2.0); Poster_List.Append (Posters, 0.0); Poster_List.Append (Posters, 4.0); Poster_List.Get (Posters, 0, F); Put_Line ("OP : " & Faggotry'Image (F)); Poster_List.Get (Posters, 3, F); Put_Line ("Ada: " & Faggotry'Image (F));end Listex;output:$ ./listexOP : 1.00000E+01Ada: 0.00000E+00want methods? want higher order functions? want to use one that closes over a stack-allocated variable (without dynamic memory allocation)? Want this to not leak and use explicit deallocation? Want this to not leak because it gets deallocated when the head of the list goes out of scope? Want to have a numeric type list instead of a list of "something with a + function, lol"?
You can do that with Ada, too.
Not GenZ, that's for sure.
Not all recursion can be turned into plain looping
What are good resources for getting into ada?
OP again
Pls no bulli
Is there a way to do this in C? I think that would require an anonymous function?
godspeed
Language shill, to be expected
I wrote a new version that "should" be tail-call optimized: termbin.com
How about you make it iterative instead of relying on an optimization that neither GCC nor LLVM guarantee?
a way to do what in C? explicit recursion? That's called a function call. You're doing it already. GOTO? that's a feature of the language. The syntax is 'goto some_label;', after you've prefaced some other statement with label:
so you prepared yourself to ignore any comments about other languages? Languages aren't cults ... with some exceptions. Relax and prepare to defend your decisions instead of blindly adhere to them.
"I don't know jack about jack but I won't know anything if I don't finish learning something" is a good enough reason to stick with C for now.
Later, learn Ada. You'll appreciate it more.
I started with "Introduction to Ada Programming" and "Programming in Ada 2012", both books.
ada-auth.org
Calm down, Ada shill.
really?
wrong
no
I AM FUCKING CALM YOU PIECE OF SHIT
yes, obviously. Write something recursive that walks a tree. Like a filesystem. An example of "non-plain" iteration would be iteration with a stack that you manage yourself.
Congrats for being retarded.
en.wikipedia.org
en.wikipedia.org
...
I actually don't know. What is ""non-plain" iteration"?
Jesus Christ.
Q: I have some tail-recursive code. How do I make this iterative?
A: put an infinite loop around it, replace your returns with loop exits, and replace your tail-recursive calls with variable assignment.
Q: oh wow, that's actually really easy. Tail-recursive code is basically already iterative code, huh.
A: In SICP terms you have an iterative process either way.
Q: I have some recursive code that isn't tail-recursive. Is this already iterative code like in the last example?
A: nope.
Q: phwaaaaaaah?
A: non-tail-recursive recursive code is using the stack as a data structure. So when you take out the recursion, you need a replacement store for all that data.
Thanks dude. I already know this.
You can turn any recursive algorithm into an iterative algorithm. It doesn't matter how much you try to redefine what looping means.
Get used to over exaggerated smugness in CS, it’s not going away.
k. When you stop LARPing and actually write some code, you'll care about the difference.
Mikee, is this you?
yes it's me. every other poster in Zig Forums that isn't you is actually me.
Looks fine to me, but some points:
Tail-call optimization is not guaranteed by the standard, so your compiler might do it, but it also might not. If a problem can be solved iteratively that's generally the way to go. There is nothing wrong with recursion if the problem itself is recursive. And if you are doing this just as an exercise in recursion that's OK as well. Just be mindful of the possible issue.
Initializing a struct the old way (as explained in K&R) is dangerous because the order of fields could change. Designated intializers allow you to explicitly name each field:
struct Node d = {.x = 4.0, .next = NULL};
You should have a 'prepend' function to complement 'append':
struct Node prepend(struct Node* list, double value) { return (struct Node) {.x = value, .next = list};};
This function does not alter the remainder of the list, so it can be used to safely add new items to the list without affecting any code that's using older items. This is a big advantage of linked lists over other data structures. You should also have a 'head' and 'tail' like they usually do in functional programming languages.
double head(struct Node* list) { return list->x;}double tail(struct Node* list) { return list->next;}
The 'head' is not very interesting, but 'tail' allows you to "drop" values from the list without mutating it.
REEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
YOU CAN'T DO THAT BECAUSE IS ISN'T PLAIN LOOPING.
YOU CAN'T JUST TURN BLAHS INTO FOOS
yeah it's real weird how non-LARPers keep wanting to distinguish between
A) completely trivial refactoring, and
B) the addition of a data structure that you must now manage and make some decisions about, and which might not just have data in it but, also functions
why would people distinguish between no work and some work? it's a real mystery. Must be some smug CS bullshit.
Yeah. I'm the LARPer.
yeah you are.
no u
...
typing on the computer is not live action role playing, you double niggers
How are you typing non-live?
How is arguing with stupid niggers on here not action?
How is pretending to be a programmer not role playing?
it is, but it's not live action, though
How are you typing non-live?
How is arguing with stupid niggers on here not action?
...
@Anonymous
wow just wow. First you can't even understand how to iterate in a functionally elegant style and then you start with the racism. You are so pathetic.
If this is bait, stop shitposting, if this isn't, go back to your shithole. Nigger.
Here's a nicer append; I haven't written much Ada yet, but this is O(1) time complexity:
procedure Append (This : in out Node; X : in Item) is N : Node := new Cell'(X => X, Next => This); begin This := N; end Append;
I haven't checked this compiles, but it looks like it should.
Looks like you're writing a prepend.
Oh. I usually use Common Lisp and there's not much of a distinction there, because CL:APPEND is variadic. If you just care about getting things into the list and don't need the ordering of the append that was written, I'd rather use this.
You seem confused about the terminology. CL:APPEND is a typical non-destructive append, and it has to walk it's way to the end of the leftmost list O(n).
Also. If you want Append and Prepend to operate in O(1) time, you'll need to use a double ended queue (dequeue).
en.wikipedia.org
good job, user. keep learning.
...
Yeah. If you were just adding a single element, you'd use CL:CONS or CL:LIST*. I mentioned CL:APPEND in part because of the similar names. It's also worth mentioning the destructive append, CL:NCONC, still doesn't modify the rightmost list.
dequeue is better than list in every way prove me wrong
if the extra functionality of a dqueue is of no use to you, then it's just a worse implementation of a list.
hey friend. dont let the negative people here get you down. you've written more code than at least half the people in this thread. :)
congratulations! just keep learning and you'll go places ^_^
Try searching "include statement c" on your favorite search engine.
Actually user that is not the c language but rather a processor script. You can use it in anything.
Not likely user
Yeah, pretty unlikely.
Unless he means in the last month in which case he got me at least.
There aren't actually that many C programmers. They mostly are cpp programmers since they write code that is for the C preprocessor.
Anything that has the exact same tokenization rules as c that is.
typedef struct { void *car, *cdr; } pear;struct pear *pare(void *car, void *cdr){ pear *p = malloc(sizeof(pear)); return (*p = (pear){car, cdr}, p);}void *fold(void *(f*)(void*,void*), void *i, struct pear *p){ for (; p; i = f(p->car, i), p=p->cdr); return i;}pear *reverse(pear *p) { return fold(&pare, NULL, p); }
wow such hard.
...
Wrong. It is used in Haskell all the time.
You have to be good at c to be able to write it this badly.
lol, why are you even declaring a pointer in your first function, just return from malloc because it already returns a pointer anyways.
So that he can initialize the cons cell.
You're right.
My dumb ass error.