sample-code: update for new WIP syntax (it's more persnickety)
This commit is contained in:
parent
6b24980fc7
commit
b54826cdd5
@ -8,9 +8,5 @@ fn main() {
|
|||||||
|
|
||||||
/// Implements the classic recursive definition of fib()
|
/// Implements the classic recursive definition of fib()
|
||||||
fn fib(a: i64) -> i64 {
|
fn fib(a: i64) -> i64 {
|
||||||
if a > 1 {
|
if a > 1 { fib(a - 1) + fib(a - 2) } else { a }
|
||||||
fib(a - 1) + fib(a - 2)
|
|
||||||
} else {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ fn ascii() {
|
|||||||
}
|
}
|
||||||
print(n_digit(row), n_digit(col), ' ')
|
print(n_digit(row), n_digit(col), ' ')
|
||||||
}
|
}
|
||||||
print(" │")
|
print(" │");
|
||||||
for col in 0..16 {
|
for col in 0..16 {
|
||||||
print(ascii_picture(row << 4 | col))
|
print(ascii_picture(row << 4 | col))
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Implements format string evaluation in weak Conlang
|
//! Implements format string evaluation in weak Conlang
|
||||||
|
|
||||||
|
|
||||||
/// Formats a string
|
/// Formats a string
|
||||||
|
#[rustfmt::skip]
|
||||||
fn f(__fmt: &str) -> &str {
|
fn f(__fmt: &str) -> &str {
|
||||||
let __out = "";
|
let __out = "";
|
||||||
let __expr = "";
|
let __expr = "";
|
||||||
@ -10,13 +10,13 @@ fn f(__fmt: &str) -> &str {
|
|||||||
for __c in chars(__fmt) {
|
for __c in chars(__fmt) {
|
||||||
match __c {
|
match __c {
|
||||||
'{' => {
|
'{' => {
|
||||||
__depth += 1
|
__depth += 1;
|
||||||
if __depth <= 1 {
|
if __depth <= 1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'}' => {
|
'}' => {
|
||||||
__depth -= 1
|
__depth -= 1;
|
||||||
if __depth <= 0 {
|
if __depth <= 0 {
|
||||||
__out = fmt(__out, __label, eval(__expr));
|
__out = fmt(__out, __label, eval(__expr));
|
||||||
(__expr, __label) = ("", "");
|
(__expr, __label) = ("", "");
|
||||||
@ -24,11 +24,11 @@ fn f(__fmt: &str) -> &str {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
':' => if __depth == 1 && __label.len() == 0 {
|
':' => if __depth == 1 && __label.len() == 0 {
|
||||||
__label = __expr + __c
|
__label = __expr + __c;
|
||||||
continue
|
continue
|
||||||
},
|
},
|
||||||
'=' => if __depth == 1 && __label.len() == 0 {
|
'=' => if __depth == 1 && __label.len() == 0 {
|
||||||
__label = __expr + __c
|
__label = __expr + __c;
|
||||||
continue
|
continue
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -6,7 +6,7 @@ fn in_range(this: Ord, min: Ord, max: Ord) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn frequency(s: str) -> [i32; 128] {
|
fn frequency(s: str) -> [i32; 128] {
|
||||||
let letters = [0;128];
|
let letters = [0; 128];
|
||||||
for letter in s {
|
for letter in s {
|
||||||
if (letter).in_range(' ', letters.len() as char) {
|
if (letter).in_range(' ', letters.len() as char) {
|
||||||
letters[(letter as i32)] += 1;
|
letters[(letter as i32)] += 1;
|
||||||
@ -25,20 +25,22 @@ fn plot_freq(freq: [i32; 128]) -> str {
|
|||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
const msg: str ="letter_frequency.cl
|
const msg: str = "letter_frequency.cl
|
||||||
Computes the frequency of ascii characters in a block of text, and prints it bucket-sorted.
|
Computes the frequency of ascii characters in a block of text, and prints it bucket-sorted.
|
||||||
Press Ctrl+D to quit.";
|
Press Ctrl+D to quit.";
|
||||||
|
|
||||||
fn main () {
|
fn main() {
|
||||||
println(msg)
|
println(msg);
|
||||||
let lines = "";
|
let lines = "";
|
||||||
loop {
|
loop {
|
||||||
let line = get_line();
|
let line = get_line();
|
||||||
if line == "" { break() }
|
if line == "" {
|
||||||
|
break ();
|
||||||
|
}
|
||||||
lines += line;
|
lines += line;
|
||||||
}
|
}
|
||||||
|
|
||||||
let freq = frequency(lines)
|
let freq = frequency(lines);
|
||||||
let plot = plot_freq(freq)
|
let plot = plot_freq(freq);
|
||||||
println(plot)
|
println(plot)
|
||||||
}
|
}
|
||||||
|
@ -6,24 +6,28 @@ const EPSILON: f64 = 8.8541878188 / 1000000000000.0;
|
|||||||
|
|
||||||
/// Calcuates the absolute value of a number
|
/// Calcuates the absolute value of a number
|
||||||
fn f64_abs(n: f64) -> f64 {
|
fn f64_abs(n: f64) -> f64 {
|
||||||
let n = n as f64
|
let n = n as f64;
|
||||||
if n < (0.0) { -n } else { n }
|
if n < (0.0) {
|
||||||
|
-n
|
||||||
|
} else {
|
||||||
|
n
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Square root approximation using Newton's method
|
/// Square root approximation using Newton's method
|
||||||
fn sqrt(n: f64) -> f64 {
|
fn sqrt(n: f64) -> f64 {
|
||||||
let n = n as f64
|
let n = n as f64;
|
||||||
if n < 0.0 {
|
if n < 0.0 {
|
||||||
return 0.0 / 0.0 // TODO: NaN constant
|
return 0.0 / 0.0; // TODO: NaN constant
|
||||||
}
|
}
|
||||||
if n == 0.0 {
|
if n == 0.0 {
|
||||||
return 0.0
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let z = n
|
let z = n;
|
||||||
loop {
|
loop {
|
||||||
let adj = (z * z - n) / (2.0 * z)
|
let adj = (z * z - n) / (2.0 * z);
|
||||||
z -= adj
|
z -= adj;
|
||||||
if adj.f64_abs() < EPSILON {
|
if adj.f64_abs() < EPSILON {
|
||||||
break z;
|
break z;
|
||||||
}
|
}
|
||||||
@ -37,7 +41,9 @@ fn pythag(a: f64, b: f64) -> f64 {
|
|||||||
|
|
||||||
/// Quadratic formula: (-b ± √(b² - 4ac)) / 2a
|
/// Quadratic formula: (-b ± √(b² - 4ac)) / 2a
|
||||||
fn quadratic(a: f64, b: f64, c: f64) -> (f64, f64) {
|
fn quadratic(a: f64, b: f64, c: f64) -> (f64, f64) {
|
||||||
let a = a as f64; let b = b as f64; let c = c as f64;
|
let a = a as f64;
|
||||||
|
let b = b as f64;
|
||||||
|
let c = c as f64;
|
||||||
(
|
(
|
||||||
(-b + sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
|
(-b + sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
|
||||||
(-b - sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
|
(-b - sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
|
||||||
@ -46,11 +52,11 @@ fn quadratic(a: f64, b: f64, c: f64) -> (f64, f64) {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
for i in 0..10 {
|
for i in 0..10 {
|
||||||
println("sqrt(",i,") ≅ ",sqrt(i as f64))
|
println("sqrt(", i, ") ≅ ", sqrt(i as f64))
|
||||||
}
|
}
|
||||||
println("\nPythagorean Theorem")
|
println("\nPythagorean Theorem");
|
||||||
println("Hypotenuse of ⊿(5, 12): ", pythag(5.0, 12.0))
|
println("Hypotenuse of ⊿(5, 12): ", pythag(5.0, 12.0));
|
||||||
|
|
||||||
println("\nQuadratic formula")
|
println("\nQuadratic formula");
|
||||||
println("Roots of 10x² + 4x - 1: ", quadratic(10.0, 40, -1.0))
|
println("Roots of 10x² + 4x - 1: ", quadratic(10.0, 40, -1.0));
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
//! Disjoint Set Forest implementation of the Union Find algorithm
|
//! Disjoint Set Forest implementation of the Union Find algorithm
|
||||||
|
|
||||||
// enum TreeNode {
|
enum TreeNode {
|
||||||
// Empty,
|
Empty,
|
||||||
// Filled { parent: usize, rank: usize }
|
Filled { parent: usize, rank: usize }
|
||||||
// }
|
}
|
||||||
|
|
||||||
// TODO: Namespace based on type of first argument
|
// TODO: Namespace based on type of first argument
|
||||||
|
|
||||||
fn makeset(f, x) {
|
fn makeset(f, x) {
|
||||||
match (*f)[x] {
|
match (*f)[x] {
|
||||||
() => (*f)[x] = Filled { parent: x, rank: 0 },
|
() => {(*f)[x] = Filled { parent: x, rank: 0 }},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -52,11 +52,11 @@ fn show(f) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn test(f) {
|
fn test(f) {
|
||||||
"Union Find on Disjoint Set Forest".println()
|
"Union Find on Disjoint Set Forest".println();
|
||||||
for i in 0..10 { makeset(f, i) }
|
for i in 0..10 { makeset(f, i) }
|
||||||
for i in 10..20 { makeset(f, i) }
|
for i in 10..20 { makeset(f, i) }
|
||||||
show(f)
|
show(f);
|
||||||
println()
|
println();
|
||||||
|
|
||||||
for i in 1..10 { union(f, i*2-1, i*2) }
|
for i in 1..10 { union(f, i*2-1, i*2) }
|
||||||
for i in 5..10 { union(f, i*2+1, i*2) }
|
for i in 5..10 { union(f, i*2+1, i*2) }
|
||||||
@ -64,6 +64,6 @@ fn test(f) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let f = [();20]
|
let f = [();20];
|
||||||
f.test()
|
f.test()
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ use super::preamble::*;
|
|||||||
|
|
||||||
struct UnitLike;
|
struct UnitLike;
|
||||||
|
|
||||||
struct TupleLike(super::num::i32, super::test::char)
|
struct TupleLike(super::num::i32, super::test::char);
|
||||||
|
|
||||||
struct StructLike {
|
struct StructLike {
|
||||||
pub member1: UnitLike,
|
pub member1: UnitLike,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user