Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Improve my bland List

Name: Anonymous 2012-02-04 2:25

class List
        attr_reader :car, :cdr, :size
        def initialize(*args)
                if args.size.zero? then
                        @car = nil; @cdr = nil; @size = 0
                elsif args.size == 1
                        @car = args[0]; @cdr = List.new; @size = 1
                else
                        @car = args[0]
                        @cdr = List.new *args[1 .. args.size - 1]
                        @size = args.size
                end
        end
        def [](n)
                raise ArgumentError unless n.class == Fixnum
                unless n > @size - 1 then
                        if n.zero? then @car
                        else @cdr[n - 1] end
                end
        end
        def []=(n,a)
                raise ArgumentError unless n.class == Fixnum
                unless n > @size - 1 then
                        if n.zero? then @car = a
                        else @cdr[n-1] = a end
                else raise ArgumentError
                end
        end
        def length
                @size
        end
        def null?
                @cdr.nil?
        end
        def member?(a)
                if a.nil? then true end
                unless self.null? then
                        if @car == a then true
                        else @cdr.member? a end
                else false
                end
        end
        def include?(a)
                self.member? a
        end
end

Name: Anonymous 2012-02-04 3:17

I would improve it by not writing it in Ruby

But if you really prefer, you can "not write it in Ruby" very slowly, to simulate the effect of having it written in Ruby

Name: Anonymous 2012-02-04 3:28

>>2

The point is to write it in Ruby because Ruby doesn't come with a Linked List implementation by default, like C++, Scheme and Ada do.

Name: Anonymous 2012-02-04 4:06

>>3
typedef struct node
{
    struct node *next;
    struct node *prev;
} node;


C doesn't have a default implementation of Linked Lists. YAY! I ACHIEVED SOMETHING! live long and suck it

Name: Anonymous 2012-02-04 4:13

>>4

C++ DOES have a default version of linked lists though, and C++ is pretty much C but better. Also, your C doubly linked list cannot store data, just other lists.

Name: Anonymous 2012-02-04 4:25

>>4
Your nodes don't have any value.

Name: Anonymous 2012-02-04 4:32

>>4
IHBT

Name: Anonymous 2012-02-04 4:51

>>5
C++ is pretty much C but better.
1/10

Name: Anonymous 2012-02-04 5:13

>>5
C++ is pretty much C but butter.

Name: Anonymous 2012-02-04 5:47

>>1
It makes no sense to have car and cdr defined on a list datatype. They're operations on pairs.

Name: Anonymous 2012-02-04 6:01

>>6
Your nads don't have any value

Name: Anonymous 2012-02-04 6:06

>>10
So basically if you want to simulate lisp lists you only make a pair class like so:

class Cons
  attr_accessor :car, :cdr
  def initialize(car, cdr = nil)
    @car, @cdr = car, cdr
  end
end

def list(*values)
  node = start = Cons.new(values.shift)
    for value in values
      next_node = Cons.new(value)
      node.cdr = next_node
      node = next_node
    end
    return start
end

l = list(1,2,3,4)

puts l.cdr.cdr.car # => 3

Name: Anonymous 2012-02-04 6:08

>>4
>>6
>>7
>>11

He'd obviously allocate space for the payload of struct node *n = ... at n + 1.

Name: Anonymous 2012-02-04 6:19

>>12

class Cons
  attr_accessor :car, :cdr
  def initialize(car = nil, cdr = nil)
    @car, @cdr = car, cdr
  end
end

def list(*values)
  last_node = Cons.new(values.pop)
  values.reverse.inject(last_node) {|l,v| Cons.new(v,l)}
end

Name: Anonymous 2012-02-04 13:20

>>10

Then explain why
(cdr (list 1 2 3))
is valid Scheme.

>>12

Yes, but I wanted to add a little bit of Ruby touch by giving it more than the standard "just car and cdr" crap. Any element in my lists can give you the value at any index like an array (but still in O(n), sadly)

Name: Anonymous 2012-02-04 14:26

>>15
(cdr (list 1 2 3))
This works like the list function in >>14. There's no list datatype involved. It just chains together pairs and then returns the first pair.

Name: Anonymous 2012-02-04 14:32

more than the standard "just car and cdr" crap
You can extend >>14 just like in lisp by writing functions that operate on the structure returned by the list function.

Of course the clean way to do it in ruby is defining a list class but then you hide the implementation and use names like head and tail that actually make sense for a list.

Name: Anonymous 2012-02-04 14:48

>>17


But then you hide the implementation

What are you talking about? In Ruby, interface and implementation are the same thing.

Name: Anonymous 2012-02-04 14:49

>>4

typedef struct node
{
    struct node *next;
    struct node *prev;
    void *value
} node;

Name: kodak_gallery_programmer !!qmiXqQhekkGXVVD 2012-02-04 15:01

>>19
You're still a dumbass.

Name: Anonymous 2012-02-04 15:03

>>6,7

I find your lack of creativity disturbing.


struct singly_linked_list {
  struct singly_linked_list* next;
};

struct doubly_linked_list {
  struct singly_linked_list forward_list;
  struct singly_linked_list backward_list;
};

struct doubly_linked_int_list {
  struct doubly_linked_list neighbors;
  int value;
};

Name: Anonymous 2012-02-04 15:04

>>20
You mad jobless_gallery_programmer-kun?


I'm in no way saying that's the best implementation of a doubly linked list for C or that it even closely resembles that of a Con cell. How about you go back to the local bar and drown away your sorrows faggot.

Name: Anonymous 2012-02-04 15:04

Linked
Linked is shit, array based is always better.

Name: Anonymous 2012-02-04 15:07

>>23
Enjoy your O(n) removes and subsets.

Name: 4 2012-02-04 15:08

>>19
You're not me, faggot!

Name: Anonymous 2012-02-04 15:08

>>24
Except that you can easily make an O(1) implementation, just because you're a stupid piece of shit doesn't mean that everybody else are too.

Name: Anonymous 2012-02-04 15:11


#include <stdio.h>

int car(int *arr){
    return *arr;
}

int *cdr(int *arr){
    return arr+sizeof(int);
}

int main(void){
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
    int i;
    for(i=0;i<10;++i){
        printf("%d ",car(arr));
        arr = cdr(arr);
    }
}



Am i doin it rite? Java programmer here

Name: Anonymous 2012-02-04 15:12

>>18
I mean you expose the interface of a list, not the interface of a pair. Something like this:

class List
  def initialize(*values)
    if values.empty?
      @list = nil
      return
    end
    last_node = Cons.new(values.pop)
    @list = values.reverse.inject(last_node) {|l,v| Cons.new(v,l)}
  end

  class Cons
    attr_accessor :car, :cdr
    def initialize(car = nil, cdr = nil)
      @car, @cdr = car, cdr
    end
  end

  def head
    @list.car
  end

  def tail
    es = []
    e = @list.cdr
    while e
      es << e.car
      e = e.cdr
    end
    List.new(*es)
  end

  def length
    e = @list
    l = 0
    while e
      l += 1
      e = e.cdr
    end
    return l
  end
end

Name: Anonymous 2012-02-04 15:14

>>27

I like this. It's great for immutable lists, but it doesn't support the cons operation so well. So some other list interface would be needed to make efficient use of it.

Name: NOSQL ENHANCED 2012-02-04 15:23

Oh! an array with O(1) of every kind of operation to go along with its O(1) access. Perhaps its search is also O(1), it is concurrency safe, has a 2 foot penis, gets all the bitches, and maxes out scores at donkey kong arcades.

Name: Anonymous 2012-02-04 15:34

>>26
Show me an O(1) remove operation for a array that doesn't just sub in NULL.

Name: Anonymous 2012-02-04 15:40

>>29
I don't know the exact definition of a Cons operation but is it something like:

int *cons(int *arr,int n){
    int *narr = malloc(sizeof(int)*(sizeof(arr)/sizeof(int))+1);
    int i;
    narr[0] = n;
    for(i=0;i<(sizeof(arr)/sizeof(int));++i)
        narr[i+1] = arr[i];
    return narr;
}


Assuming this only works for ints, did i do that write or did i get it completely wrong?

Name: Anonymous 2012-02-04 15:44

>>32
write
right * derp

Name: Anonymous 2012-02-04 16:22

>>31

No, you do it. Hint: one method involves giving up maintaining the order of the elements in the array, which is perfectly acceptable when you are just representing a set, and the ordering is not important within the set. If the size of the elements are small, around the size of a pointer, then this will save more memory than a doubly linked list, although reallocating the array can cause stalls. But this can be avoided if you can anticipate in advance a reasonable upper bound for the amount of elements in the set.

ok, so can you think of an implementation?

Name: Anonymous 2012-02-04 16:36

int *cdr(int *arr){ return arr+sizeof(int); }
arr + sizeof(int)
no

Name: Anonymous 2012-02-04 18:03

>>34

>I can do O(1) remove
>prove it
>lol no u


Fuck off faggot, go back to /g/.

Name: Anonymous 2012-02-04 18:09

>>36
You replace the element removed with the last element in the list. The place where the last element used to be becomes NULL.
As the other guy said this only will work out as long as you don't care about order in your list.

Name: Anonymous 2012-02-04 18:18

>>34
That makes it a set, not a list. What you're saying is essentially "ordered linked lists suck, unordered array-based lists are better."

Name: Anonymous 2012-02-04 18:18

>>31
Show me an O(1) remove operation for a array that doesn't just sub in NULL.
So basically you're asking him to show you an O(1) remove for an array that can't be an O(1) remove for an array?

Name: Anonymous 2012-02-04 19:18

>>38

yeah, but people often uses lists when ordering is not an issue. Lists are awesome when merging sorted collections in some way. If you need it sorted, and you need to search for random keys, and you need to support random deletion and insertion, then balanced ordered trees are better. If you need it sorted, and will be accessing random keys, but never inserting or removing, then a sorted array with binary search is pretty good, compact, and fast. If you can drop the sorted requirement, then hash tables are nice.

Name: Anonymous 2012-02-04 19:25

>>40
>yeah, but people often uses lists when ordering is not an issue

Linked lists *preserves the order*. If you aren't concerned about order, then you would use something like a bag. In which case some of the latter operations will run faster.

Name: Anonymous 2012-02-04 19:26

>>40
This is also incorrect

Lists are awesome when merging sorted collections in some way

This isn't true for certain types of linked lists in C.

Name: Anonymous 2012-02-04 20:19

>>41
>>42
And this basic argument, in the context of C/C++/Java, can be applied to lists in general.

Name: Anonymous 2012-02-04 20:46

Change out old element with NULL (or some invalid value), then re-order only when you run out of space and have to re-allocate, so it's amortized O(1).

This is really basic shit.

Name: Anonymous 2012-02-04 20:55

>>39
And waste all that memory? What are you, a Java programmer?

Go get scrubbed by a midget, you mental toilet.

Name: Anonymous 2012-02-04 22:08

>>31
By having to arrays. Why? Because it can be proven mathematically that the operations on form a Universal Turing Machine.

Name: Anonymous 2012-02-04 22:14

>>42

example? I'm sure if it could exist in C, it could exist elsewhere as well.

Although, come to think of it, you can merge sorted arrays just as easily as linked list, but with linked list, you can do more in place modifications, whereas with arrays you have to copy content out to a third array. Although that isn't that big of a deal if you can reuse arrays.

Name: Anonymous 2012-02-04 22:24

>>44

amortized order 1 time isn't always acceptable.

Name: Anonymous 2012-02-04 22:46

>>44
You sound like frozen void. Do you make your arrays 2000000.... in size as well?

Name: Anonymous 2012-02-04 22:51

Someone say O(1) remove for an array?


class Array
  def remove(n)
    # Example implementation. Real implementation will have certain exemptions
    # Such as a case where n == 0 or such. I'm just being lazy here.
    self[0..n-1] + self[n+1 .. size-1]
  end
end

Name: Anonymous 2012-02-04 22:53

>>48
amortized order 1 time isn't always acceptable.
Then certainly the immense overhead of linked structures aren't acceptable either.


>>49
You sound like frozen void.
Irrelevant.

Do you make your arrays 2000000.... in size as well?
No you fucking retard.

Name: Anonymous 2012-02-04 23:05

>>51
Then certainly the immense overhead of linked structures aren't acceptable either.
A JIT compiler can replace immutable linked lists with mutable arrays where appropriate.

Name: Anonymous 2012-02-04 23:08

>>44
Set approach is better and you don't deal with random NULLs in the middle of the array where one isn't expected.

Name: Anonymous 2012-02-04 23:11

IF YOU WOULD ALL JUST USE x86 ASM BY INTEL THEN YOU WOULDN'T HAVE THESE PROBLEMS FUCKING PEASANTS


x86 SUPREMACY

Name: Anonymous 2012-02-04 23:19

>>52
So you agree that arrays are superior to linked structures, and that any issue with arrays may be fixed by just adding more arrays?

Name: Anonymous 2012-02-04 23:19

>>54
x86 asm is there so you can write high level things on top of it, not for direct use, you mental midget

Name: Anonymous 2012-02-04 23:20

>>56
Except that there are several real world use cases where writing direct assembly is useful, try getting a job you fucking retard.

Name: Anonymous 2012-02-04 23:21

Consider the following: Imagine if DRAM was a linked-listed. We would be able to do O(1) inserts and have true expandable arrays

Name: Anonymous 2012-02-04 23:22

>>56
not for direct use

tell that to everyone in the 80s
tell that to all the embedded designers

Name: Anonymous 2012-02-04 23:23

>>58
Consider the following: Imagine if DRAM was a linked-listed
Consider imagining that DRAM was a linked-listed? I want whatever you're having.

Name: Anonymous 2012-02-04 23:24

>>59
We're no longer in the 80s, and I don't think anyone sane uses x86 for embedded designs.

Name: Anonymous 2012-02-04 23:27

x86 and other assembly languages are directly useful for things like encryption, specifically when vector intrinsics just don't cut it.

Name: Anonymous 2012-02-04 23:28

>>61
Any sane embedded designer that wants a good fast product would be using [insert_instruction_set_here] to get the job done correctly and efficiently.

Name: Anonymous 2012-02-04 23:42

>>63
For mobile bullshit devices (I fucking hate smartphones), x86 is too power-hungry and won't work. For little CPUs embedded in industrial machinery, going full retard with a complex Intel CPU is stupid while you could do the same thing with some little microcontroller with 128K of embedded DRAM. Face it, x86 is made for desktops and (to some extent) laptops.

Name: Anonymous 2012-02-04 23:49

>>64
Are you retarded? I said [insert_instruction_set_here] for a god damn reason you fuck, of course i realize x86 isn't used in most of those products.

Name: Anonymous 2012-02-04 23:52

>>64
The jews at intel are holding back desktops!

We would be much better off with a RISC style set

Name: Anonymous 2012-02-05 0:00

>>66
you dumb goys are too cretin to invest into your own CPUs

suffer from the x86 supremacy, faggots

Name: Anonymous 2012-02-05 0:01

>>51
The overhead of providing links is not that large if the size of the objects is much larger than a pointer, or two pointers. You'll be using a lot of extra memory if you try to do a linked list of characters though, and if you tried to store 500MB of characters in a linked list. But maybe that could be useful for some type of splicing algorithm. It kind of reminds me of DNA or something.

But there are times where it is important every operation must finish below a certain amount of time, even if it makes each operation slower. Being really fast for a while, and then suddenly taking time proportional  to the number of prior operations could be bad when you need to keep things in synch, like a media player or something.

Name: Anonymous 2012-02-05 0:04

>>68
I'm not talking about memory overhead, although it's kind of linked with what I'm saying (don't mind the pun). Linked structures are just inherently cache inefficient, arrays do things much faster on modern architectures.

Name: Anonymous 2012-02-05 0:17

>>69
Arrays are made by the JEWS

Name: Anonymous 2012-02-05 1:01

data List a = Cons a (List a) | Nil

We have a weiner.

Name: Anonymous 2012-02-05 2:59

>>69
No, linked structures are not inherently cache inefficient. There's nothing that says that linked structures somehow can't be close in memory.

Copying garbage collectors routinely move objects that refer to each other into a common shared region. Some fancy LISP collectors in the old and days of you're even removed the cdr pointer with the following pair itself (and so on).

In C you can easily put your list nodes in an array and allocate them from there. If the nodes get too spread out, move the nodes to one end (in sorted order).

Name: Anonymous 2012-02-05 3:08

>>72

hmm, nice!

Name: Anonymous 2012-02-05 7:17

Cons = Struct.new(:car, :cdr)

Name: Anonymous 2012-02-07 19:07

>>74


def list *args
        Struct.new(:car, :cdr).new(args[0], list(*args[1 .. args.size - 1])) unless args.size.zero?

Name: Anonymous 2012-02-07 19:08

>>75

forgot my end

Name: Anonymous 2012-02-07 19:40

Check em salami-face

Name: Anonymous 2012-02-08 0:22

>>77

My un-dubs are better than your dubs.

Name: Anonymous 2012-02-26 20:48

>>78
Or are they?

Don't change these.
Name: Email:
Entire Thread Thread List