Skip to content

Instantly share code, notes, and snippets.

@VictorTaelin
Created April 23, 2022 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VictorTaelin/69c3c220edcefd312ab9771779e187c2 to your computer and use it in GitHub Desktop.
Save VictorTaelin/69c3c220edcefd312ab9771779e187c2 to your computer and use it in GitHub Desktop.
solucion
function push(new_state, states) {
if (states === null) {
return {bit: 0, state: new_state, older: null};
} else {
var {bit, state, older} = states;
if (bit === 0) {
return {bit: 1, state, older};
} else {
return {bit: 0, state: new_state, older: push(state, older)};
}
}
}
function show(states) {
if (states === null) {
return "";
} else {
return "x" + states.state + ", " + show(states.older);
}
}
var states = null;
for (var t = 0; t < 128; ++t) {
states = push(t, states);
console.log("[t="+t+"] "+show(states));
}
console.log(states);
@VictorTaelin
Copy link
Author

VictorTaelin commented Apr 23, 2022

rust

#[derive(Debug, Clone)]
pub enum Roll<T> {
  Cons {
    keep: bool,
    head: T,
    tail: Box<Roll<T>>,
  },
  Nil
}

pub fn roll_push<T>(elem: T, roll: Roll<T>) -> Roll<T> {
  match roll {
    Roll::Nil => {
      return Roll::Cons {
        keep: false,
        head: elem,
        tail: Box::new(Roll::Nil),
      };
    }
    Roll::Cons { keep, head, tail } => {
      if keep {
        return Roll::Cons {
          keep: false,
          head: elem,
          tail: Box::new(roll_push(head, *tail)),
        };
      } else {
        return Roll::Cons {
          keep: true,
          head: head,
          tail: tail,
        };
      }
    }
  }
}

pub fn roll_show<T: std::fmt::Display>(roll: Roll<T>) -> String {
  match roll {
    Roll::Nil => {
      return format!("");
    }
    Roll::Cons { keep, head, tail } => {
      return format!("{} {}", head, roll_show(*tail));
    }
  }
}


fn main() {
  let mut roll = Roll::Nil;
  for t in 0 .. 128 {
    println!("[t={}] {}", t, roll_show(roll.clone()));
    roll = roll_push(t, roll);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment