How would one implement Lazy Evaluation in C?

后端 未结 9 1903
野的像风
野的像风 2021-02-02 13:24

Take for example,

The follow python code:

def multiples_of_2():
  i = 0
  while True:
    i = i + 2
    yield i

How do we translate thi

相关标签:
9条回答
  • 2021-02-02 13:58

    I have implemented my own lazy eval, with respects to solving the hamming's problem.

    Heres my code for anyone whos interested:

    #include <stdio.h>
    #include <stdlib.h>
    
    // Hamming problem in C.
    
    typedef struct gen {
      int tracker;
      int (*next)(struct gen *g);
    } generator;
    
    int twos_gen(struct gen *g) {
      g->tracker = g->tracker + 2;
      return g->tracker;
    }
    
    generator* twos_stream() {
      generator *g = malloc(sizeof(generator));
      g->next = twos_gen;
      g->tracker = 0;
      return g;
    }
    
    int threes_gen(struct gen *g) {
      g->tracker = g->tracker + 3;
      return g->tracker;
    }
    
    generator* threes_stream() {
      generator *g = malloc(sizeof(generator));
      g->next = threes_gen;
      g->tracker = 0;
      return g;
    }
    
    int fives_gen(struct gen *g) {
      g->tracker = g->tracker + 5;
      return g->tracker;
    }
    
    generator* fives_stream() {
      generator *g = malloc(sizeof(generator));
      g->next = fives_gen;
      g->tracker = 0;
      return g;
    }
    
    int smallest(int a, int b, int c) {
      if (a < b) {
        if (c < a) return c;
        return a;
      }
      else {
        if (c < b) return c;
        return b;
      }
    }
    
    int hamming_gen(struct gen *g) {
      generator* twos = twos_stream();
      generator* threes = threes_stream();
      generator* fives = fives_stream();
    
      int c2 = twos->next(twos);
      int c3 = threes->next(threes);
      int c5 = fives->next(fives);
    
      while (c2 <= g->tracker) c2 = twos->next(twos);
      while (c3 <= g->tracker) c3 = threes->next(threes);
      while (c5 <= g->tracker) c5 = fives->next(fives);
    
      g->tracker = smallest(c2,c3,c5);
      return g->tracker;
    }
    
    generator* hammings_stream() {
      generator *g = malloc(sizeof(generator));
      g->next = hamming_gen;
      g->tracker = 0;
      return g;
    }
    
    int main() {
      generator* hammings = hammings_stream();
      int i = 0;
      while (i<10) {
        printf("Hamming No: %d\n",hammings->next(hammings));
        i++;
      }
    }
    
    0 讨论(0)
  • 2021-02-02 13:59

    You could try to encapsulate this in a struct:

    typedef struct s_generator {
        int current;
        int (*func)(int);
    } generator;
    
    int next(generator* gen) {
        int result = gen->current;
        gen->current = (gen->func)(gen->current);
        return result;
    }
    

    Then you define you multiples with:

    int next_multiple(int current) { return 2 + current; }
    generator multiples_of_2 = {0, next_multiple};
    

    You get the next multiple by calling

    next(&multiples_of_2);
    
    0 讨论(0)
  • 2021-02-02 14:08
    int multiples_of_2() {
        static int i = 0;
        i += 2;
        return i;
    }
    

    The static int i behaves like a global variable but is visible only within the contect of multiples_of_2().

    0 讨论(0)
提交回复
热议问题