mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 14:03:10 -05:00
Merge pull request #22 from quexxon/apply-zig-fmt
Apply `zig fmt` to exercises and generate remaining patch files
This commit is contained in:
commit
a2b6b68a25
81 changed files with 422 additions and 196 deletions
|
@ -19,4 +19,3 @@ const std = @import("std");
|
||||||
fn main() void {
|
fn main() void {
|
||||||
std.debug.print("Hello world!\n", .{});
|
std.debug.print("Hello world!\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,5 +47,5 @@ pub fn main() void {
|
||||||
// is a string. The string may contain placeholders '{}', and the
|
// is a string. The string may contain placeholders '{}', and the
|
||||||
// second parameter is an "anonymous list literal" (don't worry
|
// second parameter is an "anonymous list literal" (don't worry
|
||||||
// about this for now!) with the values to be printed.
|
// about this for now!) with the values to be printed.
|
||||||
std.debug.print("{} {} {}\n", .{n, pi, negative_eleven});
|
std.debug.print("{} {} {}\n", .{ n, pi, negative_eleven });
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ pub fn main() void {
|
||||||
// Use the len property to get the length of the array:
|
// Use the len property to get the length of the array:
|
||||||
const length = some_primes.???;
|
const length = some_primes.???;
|
||||||
|
|
||||||
std.debug.print("First: {}, Fourth: {}, Length: {}\n",
|
std.debug.print("First: {}, Fourth: {}, Length: {}\n", .{
|
||||||
.{first, fourth, length});
|
first, fourth, length,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,7 @@ pub fn main() void {
|
||||||
const major_tom = major ??? tom;
|
const major_tom = major ??? tom;
|
||||||
|
|
||||||
// That's all the problems. Let's see our results:
|
// That's all the problems. Let's see our results:
|
||||||
std.debug.print("d={u} {s}{s}\n",.{d, laugh, major_tom});
|
std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom });
|
||||||
//
|
|
||||||
// Keen eyes will notice that we've put 'u' and 's' inside the '{}'
|
// Keen eyes will notice that we've put 'u' and 's' inside the '{}'
|
||||||
// placeholders in the format string above. This tells the
|
// placeholders in the format string above. This tells the
|
||||||
// print() function to format the values as a UTF-8 character and
|
// print() function to format the values as a UTF-8 character and
|
||||||
|
|
|
@ -20,5 +20,5 @@ pub fn main() void {
|
||||||
And the Spiders from Mars
|
And the Spiders from Mars
|
||||||
;
|
;
|
||||||
|
|
||||||
std.debug.print("{s}\n",.{lyrics});
|
std.debug.print("{s}\n", .{lyrics});
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
//
|
//
|
||||||
// a == b means "a equals b"
|
// a == b means "a equals b"
|
||||||
// a < b means "a is less than b"
|
// a < b means "a is less than b"
|
||||||
// a !=b means "a does not equal b"
|
// a != b means "a does not equal b"
|
||||||
//
|
//
|
||||||
// The important thing about Zig's "if" is that it *only* accepts
|
// The important thing about Zig's "if" is that it *only* accepts
|
||||||
// boolean values. It won't coerce numbers or other types of data
|
// boolean values. It won't coerce numbers or other types of data
|
||||||
|
|
|
@ -14,4 +14,3 @@ pub fn main() void {
|
||||||
|
|
||||||
std.debug.print("With the discount, the price is ${}.\n", .{price});
|
std.debug.print("With the discount, the price is ${}.\n", .{price});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
// a == b means "a equals b"
|
// a == b means "a equals b"
|
||||||
// a < b means "a is less than b"
|
// a < b means "a is less than b"
|
||||||
// a > b means "a is greater than b"
|
// a > b means "a is greater than b"
|
||||||
// a !=b means "a does not equal b"
|
// a != b means "a does not equal b"
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ pub fn main() void {
|
||||||
var n: u32 = 2;
|
var n: u32 = 2;
|
||||||
|
|
||||||
// Please use a condition that is true UNTIL "n" reaches 1024:
|
// Please use a condition that is true UNTIL "n" reaches 1024:
|
||||||
while ( ??? ){
|
while (???) {
|
||||||
// Print the current number
|
// Print the current number
|
||||||
std.debug.print("{} ", .{n});
|
std.debug.print("{} ", .{n});
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// while (condition) : (continue expression){
|
// while (condition) : (continue expression) {
|
||||||
//
|
//
|
||||||
// if(other condition) continue;
|
// if (other condition) continue;
|
||||||
//
|
//
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
@ -21,11 +21,11 @@ pub fn main() void {
|
||||||
|
|
||||||
// I want to print every number between 1 and 20 that is NOT
|
// I want to print every number between 1 and 20 that is NOT
|
||||||
// divisible by 3 or 5.
|
// divisible by 3 or 5.
|
||||||
while (n <= 20) : (n+=1) {
|
while (n <= 20) : (n += 1) {
|
||||||
// The '%' symbol is the "modulo" operator and it
|
// The '%' symbol is the "modulo" operator and it
|
||||||
// returns the remainder after division.
|
// returns the remainder after division.
|
||||||
if(n % 3 == 0) ???;
|
if (n % 3 == 0) ???;
|
||||||
if(n % 5 == 0) ???;
|
if (n % 5 == 0) ???;
|
||||||
std.debug.print("{} ", .{n});
|
std.debug.print("{} ", .{n});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
//
|
//
|
||||||
// You can force a loop to exit immediately with a "break" statement:
|
// You can force a loop to exit immediately with a "break" statement:
|
||||||
//
|
//
|
||||||
// while (condition) : (continue expression){
|
// while (condition) : (continue expression) {
|
||||||
//
|
//
|
||||||
// if(other condition) break;
|
// if (other condition) break;
|
||||||
//
|
//
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
@ -17,8 +17,8 @@ pub fn main() void {
|
||||||
|
|
||||||
// Oh dear! This while loop will go forever!?
|
// Oh dear! This while loop will go forever!?
|
||||||
// Please fix this so the print statement below gives the desired output.
|
// Please fix this so the print statement below gives the desired output.
|
||||||
while (true) : (n+=1) {
|
while (true) : (n += 1) {
|
||||||
if(???) ???;
|
if (???) ???;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Result: we want n=4
|
// Result: we want n=4
|
||||||
|
|
|
@ -16,13 +16,12 @@ pub fn main() void {
|
||||||
std.debug.print("A Dramatic Story: ", .{});
|
std.debug.print("A Dramatic Story: ", .{});
|
||||||
|
|
||||||
for (???) |???| {
|
for (???) |???| {
|
||||||
if(scene == 'h') std.debug.print(":-) ", .{});
|
if (scene == 'h') std.debug.print(":-) ", .{});
|
||||||
if(scene == 's') std.debug.print(":-( ", .{});
|
if (scene == 's') std.debug.print(":-( ", .{});
|
||||||
if(scene == 'n') std.debug.print(":-| ", .{});
|
if (scene == 'n') std.debug.print(":-| ", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.print("The End.\n", .{});
|
std.debug.print("The End.\n", .{});
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// Note that "for" loops also work on things called "slices"
|
// Note that "for" loops also work on things called "slices"
|
||||||
// which we'll see later.
|
// which we'll see later.
|
||||||
|
|
|
@ -19,10 +19,10 @@ function main() void {
|
||||||
??? (i <= stop_at) : (i += 1) {
|
??? (i <= stop_at) : (i += 1) {
|
||||||
if (i % 3 == 0) std.debug.print("Fizz", .{});
|
if (i % 3 == 0) std.debug.print("Fizz", .{});
|
||||||
if (i % 5 == 0) std.debug.print("Buzz", .{});
|
if (i % 5 == 0) std.debug.print("Buzz", .{});
|
||||||
if ( !(i % 3 == 0) and !(i % 5 == 0) ) {
|
if (!(i % 3 == 0) and !(i % 5 == 0)) {
|
||||||
std.debug.print("{}", .{???});
|
std.debug.print("{}", .{???});
|
||||||
}
|
}
|
||||||
std.debug.print(", ", .{});
|
std.debug.print(", ", .{});
|
||||||
}
|
}
|
||||||
std.debug.print("\n",.{});
|
std.debug.print("\n", .{});
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// writing one of our own:
|
// writing one of our own:
|
||||||
//
|
//
|
||||||
// fn foo(n: u8) u8 {
|
// fn foo(n: u8) u8 {
|
||||||
// return n+1;
|
// return n + 1;
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// The foo() function above takes a number "n" and returns a number that is
|
// The foo() function above takes a number "n" and returns a number that is
|
||||||
|
@ -23,7 +23,6 @@ pub fn main() void {
|
||||||
std.debug.print("Answer to the Ultimate Question: {}\n", .{answer});
|
std.debug.print("Answer to the Ultimate Question: {}\n", .{answer});
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// Please define the deepThought() function below.
|
// Please define the deepThought() function below.
|
||||||
//
|
//
|
||||||
// We're just missing a couple things. One thing we're NOT missing is the
|
// We're just missing a couple things. One thing we're NOT missing is the
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
// example that takes two parameters. As you can see, parameters
|
// example that takes two parameters. As you can see, parameters
|
||||||
// are declared just like any other types ("name": "type"):
|
// are declared just like any other types ("name": "type"):
|
||||||
//
|
//
|
||||||
// fn myFunction( number: u8, is_lucky: bool ) {
|
// fn myFunction(number: u8, is_lucky: bool) {
|
||||||
// ...
|
// ...
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
const std = @import( "std" );
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
std.debug.print("Powers of two: {} {} {} {}\n", .{
|
std.debug.print("Powers of two: {} {} {} {}\n", .{
|
||||||
|
@ -18,7 +18,6 @@ pub fn main() void {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// Please give this function the correct input parameter(s).
|
// Please give this function the correct input parameter(s).
|
||||||
// You'll need to figure out the parameter name and type that we're
|
// You'll need to figure out the parameter name and type that we're
|
||||||
// expecting. The output type has already been specified for you.
|
// expecting. The output type has already been specified for you.
|
||||||
|
|
|
@ -5,16 +5,15 @@
|
||||||
//
|
//
|
||||||
// Both of these are simply labeled "loop" below.
|
// Both of these are simply labeled "loop" below.
|
||||||
//
|
//
|
||||||
const std = @import( "std" );
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const my_numbers = [4]u16{ 5,6,7,8 };
|
const my_numbers = [4]u16{ 5, 6, 7, 8 };
|
||||||
|
|
||||||
printPowersOfTwo(my_numbers);
|
printPowersOfTwo(my_numbers);
|
||||||
std.debug.print("\n", .{});
|
std.debug.print("\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// You won't see this every day: a function that takes an array with
|
// You won't see this every day: a function that takes an array with
|
||||||
// exactly four u16 numbers. This is not how you would normally pass
|
// exactly four u16 numbers. This is not how you would normally pass
|
||||||
// an array to a function. We'll learn about slices and pointers in
|
// an array to a function. We'll learn about slices and pointers in
|
||||||
|
@ -28,7 +27,6 @@ fn printPowersOfTwo(numbers: [4]u16) ??? {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// This function bears a striking resemblance to twoToThe() in the last
|
// This function bears a striking resemblance to twoToThe() in the last
|
||||||
// exercise. But don't be fooled! This one does the math without the aid
|
// exercise. But don't be fooled! This one does the math without the aid
|
||||||
// of the standard library!
|
// of the standard library!
|
||||||
|
|
|
@ -16,7 +16,7 @@ const MyNumberError = error{
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
var nums = [_]u8{2,3,4,5,6};
|
var nums = [_]u8{ 2, 3, 4, 5, 6 };
|
||||||
|
|
||||||
for (nums) |n| {
|
for (nums) |n| {
|
||||||
std.debug.print("{}", .{n});
|
std.debug.print("{}", .{n});
|
||||||
|
@ -40,7 +40,7 @@ pub fn main() void {
|
||||||
// Notice how this function can return any member of the MyNumberError
|
// Notice how this function can return any member of the MyNumberError
|
||||||
// error set.
|
// error set.
|
||||||
fn numberFail(n: u8) MyNumberError {
|
fn numberFail(n: u8) MyNumberError {
|
||||||
if(n > 4) return MyNumberError.TooBig;
|
if (n > 4) return MyNumberError.TooBig;
|
||||||
if(n < 4) return MyNumberError.TooSmall; // <---- this one is free!
|
if (n < 4) return MyNumberError.TooSmall; // <---- this one is free!
|
||||||
return MyNumberError.TooFour;
|
return MyNumberError.TooFour;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const MyNumberError = error{ TooSmall };
|
const MyNumberError = error{TooSmall};
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
var my_number: ??? = 5;
|
var my_number: ??? = 5;
|
||||||
|
@ -27,4 +27,3 @@ pub fn main() void {
|
||||||
|
|
||||||
std.debug.print("I compiled!", .{});
|
std.debug.print("I compiled!", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,13 +8,13 @@
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const MyNumberError = error{ TooSmall };
|
const MyNumberError = error{TooSmall};
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
var a: u32 = addTwenty(44) catch 22;
|
var a: u32 = addTwenty(44) catch 22;
|
||||||
var b: u32 = addTwenty(4) ??? 22;
|
var b: u32 = addTwenty(4) ??? 22;
|
||||||
|
|
||||||
std.debug.print("a={}, b={}", .{a,b});
|
std.debug.print("a={}, b={}", .{ a, b });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Please provide the return type from this function.
|
// Please provide the return type from this function.
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub fn main() void {
|
||||||
var b: u32 = makeJustRight(14) catch 0;
|
var b: u32 = makeJustRight(14) catch 0;
|
||||||
var c: u32 = makeJustRight(4) catch 0;
|
var c: u32 = makeJustRight(4) catch 0;
|
||||||
|
|
||||||
std.debug.print("a={}, b={}, c={}", .{a,b,c});
|
std.debug.print("a={}, b={}, c={}", .{ a, b, c });
|
||||||
}
|
}
|
||||||
|
|
||||||
// In this silly example we've split the responsibility of making
|
// In this silly example we've split the responsibility of making
|
||||||
|
@ -37,7 +37,9 @@ pub fn main() void {
|
||||||
// detectProblems() Returns the number or an error.
|
// detectProblems() Returns the number or an error.
|
||||||
//
|
//
|
||||||
fn makeJustRight(n: u32) MyNumberError!u32 {
|
fn makeJustRight(n: u32) MyNumberError!u32 {
|
||||||
return fixTooBig(n) catch |err| { return err; };
|
return fixTooBig(n) catch |err| {
|
||||||
|
return err;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fixTooBig(n: u32) MyNumberError!u32 {
|
fn fixTooBig(n: u32) MyNumberError!u32 {
|
||||||
|
@ -65,4 +67,3 @@ fn detectProblems(n: u32) MyNumberError!u32 {
|
||||||
if (n > 20) return MyNumberError.TooBig;
|
if (n > 20) return MyNumberError.TooBig;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,11 +19,10 @@ pub fn main() void {
|
||||||
var b: u32 = addFive(14) catch 0;
|
var b: u32 = addFive(14) catch 0;
|
||||||
var c: u32 = addFive(4) catch 0;
|
var c: u32 = addFive(4) catch 0;
|
||||||
|
|
||||||
std.debug.print("a={}, b={}, c={}", .{a,b,c});
|
std.debug.print("a={}, b={}, c={}", .{ a, b, c });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addFive(n: u32) MyNumberError!u32 {
|
fn addFive(n: u32) MyNumberError!u32 {
|
||||||
//
|
|
||||||
// This function needs to return any error which might come back from detect().
|
// This function needs to return any error which might come back from detect().
|
||||||
// Please use a "try" statement rather than a "catch".
|
// Please use a "try" statement rather than a "catch".
|
||||||
//
|
//
|
||||||
|
@ -37,4 +36,3 @@ fn detect(n: u32) MyNumberError!u32 {
|
||||||
if (n > 20) return MyNumberError.TooBig;
|
if (n > 20) return MyNumberError.TooBig;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,6 @@ pub fn main() void {
|
||||||
|
|
||||||
for (animals) |a| printAnimal(a);
|
for (animals) |a| printAnimal(a);
|
||||||
|
|
||||||
|
|
||||||
std.debug.print("done.\n", .{});
|
std.debug.print("done.\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,9 +20,18 @@ fn printAnimal(animal: u8) void {
|
||||||
|
|
||||||
std.debug.print(") ", .{}); // <---- how!?
|
std.debug.print(") ", .{}); // <---- how!?
|
||||||
|
|
||||||
if (animal == 'g'){ std.debug.print("Goat", .{}); return; }
|
if (animal == 'g') {
|
||||||
if (animal == 'c'){ std.debug.print("Cat", .{}); return; }
|
std.debug.print("Goat", .{});
|
||||||
if (animal == 'd'){ std.debug.print("Dog", .{}); return; }
|
return;
|
||||||
|
}
|
||||||
|
if (animal == 'c') {
|
||||||
|
std.debug.print("Cat", .{});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (animal == 'd') {
|
||||||
|
std.debug.print("Dog", .{});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
std.debug.print("Unknown", .{});
|
std.debug.print("Unknown", .{});
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
//
|
|
||||||
var counter: u32 = 0;
|
var counter: u32 = 0;
|
||||||
|
|
||||||
const MyErr = error{ GetFail, IncFail };
|
const MyErr = error{ GetFail, IncFail };
|
||||||
|
@ -25,7 +24,7 @@ pub fn main() void {
|
||||||
var a: u32 = makeNumber() catch return;
|
var a: u32 = makeNumber() catch return;
|
||||||
var b: u32 = makeNumber() catch return;
|
var b: u32 = makeNumber() catch return;
|
||||||
|
|
||||||
std.debug.print("Numbers: {}, {}\n", .{a,b});
|
std.debug.print("Numbers: {}, {}\n", .{ a, b });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn makeNumber() MyErr!u32 {
|
fn makeNumber() MyErr!u32 {
|
||||||
|
|
|
@ -22,8 +22,6 @@
|
||||||
// return GameError.TooManyPlayers;
|
// return GameError.TooManyPlayers;
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
//
|
|
||||||
//
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
|
|
|
@ -26,9 +26,15 @@ pub fn main() void {
|
||||||
|
|
||||||
for (operations) |op| {
|
for (operations) |op| {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
1 => { current_value += 1; },
|
1 => {
|
||||||
2 => { current_value -= 1; },
|
current_value += 1;
|
||||||
3 => { current_value *= current_value; },
|
},
|
||||||
|
2 => {
|
||||||
|
current_value -= 1;
|
||||||
|
},
|
||||||
|
3 => {
|
||||||
|
current_value *= current_value;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.print("{} ", .{current_value});
|
std.debug.print("{} ", .{current_value});
|
||||||
|
|
|
@ -23,7 +23,7 @@ const MyNumberError = error{
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
var nums = [_]u8{2,3,4,5,6};
|
var nums = [_]u8{ 2, 3, 4, 5, 6 };
|
||||||
|
|
||||||
for (nums) |num| {
|
for (nums) |num| {
|
||||||
std.debug.print("{}", .{num});
|
std.debug.print("{}", .{num});
|
||||||
|
@ -43,7 +43,7 @@ pub fn main() void {
|
||||||
// This time we'll have numberMaybeFail() return an error union rather
|
// This time we'll have numberMaybeFail() return an error union rather
|
||||||
// than a straight error.
|
// than a straight error.
|
||||||
fn numberMaybeFail(n: u8) MyNumberError!u8 {
|
fn numberMaybeFail(n: u8) MyNumberError!u8 {
|
||||||
if(n > 4) return MyNumberError.TooBig;
|
if (n > 4) return MyNumberError.TooBig;
|
||||||
if(n < 4) return MyNumberError.TooSmall;
|
if (n < 4) return MyNumberError.TooSmall;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const NumError = error{ IllegalNumber };
|
const NumError = error{IllegalNumber};
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const stdout = std.io.getStdOut().writer();
|
const stdout = std.io.getStdOut().writer();
|
||||||
|
@ -19,6 +19,6 @@ pub fn main() void {
|
||||||
|
|
||||||
// Just don't modify this function. It's "perfect" the way it is. :-)
|
// Just don't modify this function. It's "perfect" the way it is. :-)
|
||||||
fn getNumber() NumError!u32 {
|
fn getNumber() NumError!u32 {
|
||||||
if( false ) return NumError.IllegalNumber;
|
if (false) return NumError.IllegalNumber;
|
||||||
return 42;
|
return 42;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// Please complete the enum!
|
// Please complete the enum!
|
||||||
const Ops = enum{ ??? };
|
const Ops = enum { ??? };
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const operations = [_]Ops{
|
const operations = [_]Ops{
|
||||||
|
@ -29,16 +29,22 @@ pub fn main() void {
|
||||||
Ops.inc,
|
Ops.inc,
|
||||||
Ops.pow,
|
Ops.pow,
|
||||||
Ops.dec,
|
Ops.dec,
|
||||||
Ops.dec
|
Ops.dec,
|
||||||
};
|
};
|
||||||
|
|
||||||
var current_value: u32 = 0;
|
var current_value: u32 = 0;
|
||||||
|
|
||||||
for (operations) |op| {
|
for (operations) |op| {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
Ops.inc => { current_value += 1; },
|
Ops.inc => {
|
||||||
Ops.dec => { current_value -= 1; },
|
current_value += 1;
|
||||||
Ops.pow => { current_value *= current_value; },
|
},
|
||||||
|
Ops.dec => {
|
||||||
|
current_value -= 1;
|
||||||
|
},
|
||||||
|
Ops.pow => {
|
||||||
|
current_value *= current_value;
|
||||||
|
},
|
||||||
// No "else" needed! Why is that?
|
// No "else" needed! Why is that?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ const std = @import("std");
|
||||||
// #RRGGBB
|
// #RRGGBB
|
||||||
//
|
//
|
||||||
// Please define and use a pure blue value Color:
|
// Please define and use a pure blue value Color:
|
||||||
const Color = enum(u32){
|
const Color = enum(u32) {
|
||||||
red = 0xff0000,
|
red = 0xff0000,
|
||||||
green = 0x00ff00,
|
green = 0x00ff00,
|
||||||
blue = ???,
|
blue = ???,
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// We'll use an enum to specify the character class.
|
// We'll use an enum to specify the character class.
|
||||||
const Class = enum{
|
const Class = enum {
|
||||||
wizard,
|
wizard,
|
||||||
thief,
|
thief,
|
||||||
bard,
|
bard,
|
||||||
|
@ -32,7 +32,7 @@ const Class = enum{
|
||||||
|
|
||||||
// Please add a new property to this struct called "health" and make
|
// Please add a new property to this struct called "health" and make
|
||||||
// it a u8 integer type.
|
// it a u8 integer type.
|
||||||
const Character = struct{
|
const Character = struct {
|
||||||
class: Class,
|
class: Class,
|
||||||
gold: u32,
|
gold: u32,
|
||||||
experience: u32,
|
experience: u32,
|
||||||
|
@ -54,6 +54,6 @@ pub fn main() void {
|
||||||
|
|
||||||
std.debug.print("Your wizard has {} health and {} gold.", .{
|
std.debug.print("Your wizard has {} health and {} gold.", .{
|
||||||
glorp_the_wise.health,
|
glorp_the_wise.health,
|
||||||
glorp_the_wise.gold
|
glorp_the_wise.gold,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,14 +8,14 @@
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const Class = enum{
|
const Class = enum {
|
||||||
wizard,
|
wizard,
|
||||||
thief,
|
thief,
|
||||||
bard,
|
bard,
|
||||||
warrior,
|
warrior,
|
||||||
};
|
};
|
||||||
|
|
||||||
const Character = struct{
|
const Character = struct {
|
||||||
class: Class,
|
class: Class,
|
||||||
gold: u32,
|
gold: u32,
|
||||||
health: u8,
|
health: u8,
|
||||||
|
@ -45,7 +45,8 @@ pub fn main() void {
|
||||||
|
|
||||||
// Printing all RPG characters in a loop:
|
// Printing all RPG characters in a loop:
|
||||||
for (chars) |c, num| {
|
for (chars) |c, num| {
|
||||||
std.debug.print("Character {} - G:{} H:{} XP:{}\n",
|
std.debug.print("Character {} - G:{} H:{} XP:{}\n", .{
|
||||||
.{num+1, c.gold, c.health, c.experience});
|
num + 1, c.gold, c.health, c.experience,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,5 +32,5 @@ pub fn main() void {
|
||||||
// (See the "cheatsheet" above for ideas.)
|
// (See the "cheatsheet" above for ideas.)
|
||||||
num2 = ???;
|
num2 = ???;
|
||||||
|
|
||||||
std.debug.print("num1: {}, num2: {}\n", .{num1, num2});
|
std.debug.print("num1: {}, num2: {}\n", .{ num1, num2 });
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,5 +23,5 @@ pub fn main() void {
|
||||||
const a: u8 = 12;
|
const a: u8 = 12;
|
||||||
const b: *u8 = &a; // fix this!
|
const b: *u8 = &a; // fix this!
|
||||||
|
|
||||||
std.debug.print("a: {}, b: {}\n", .{a, b.*});
|
std.debug.print("a: {}, b: {}\n", .{ a, b.* });
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,5 +37,5 @@ pub fn main() void {
|
||||||
p.* += 1;
|
p.* += 1;
|
||||||
p = &bar;
|
p = &bar;
|
||||||
p.* += 1;
|
p.* += 1;
|
||||||
std.debug.print("foo={}, bar={}\n", .{foo, bar});
|
std.debug.print("foo={}, bar={}\n", .{ foo, bar });
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@ pub fn main() void {
|
||||||
makeFive(&num);
|
makeFive(&num);
|
||||||
std.debug.print("num: {}, ", .{num});
|
std.debug.print("num: {}, ", .{num});
|
||||||
|
|
||||||
|
|
||||||
// Now something interesting. Let's pass a reference to a
|
// Now something interesting. Let's pass a reference to a
|
||||||
// specific array value:
|
// specific array value:
|
||||||
makeFive(&more_nums[2]);
|
makeFive(&more_nums[2]);
|
||||||
|
|
|
@ -34,17 +34,17 @@
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const Class = enum{
|
const Class = enum {
|
||||||
wizard,
|
wizard,
|
||||||
thief,
|
thief,
|
||||||
bard,
|
bard,
|
||||||
warrior,
|
warrior,
|
||||||
};
|
};
|
||||||
|
|
||||||
const Character = struct{
|
const Character = struct {
|
||||||
class: Class,
|
class: Class,
|
||||||
gold: u32,
|
gold: u32,
|
||||||
health: u8 = 100, // <--- You can also fields a default value!
|
health: u8 = 100, // <--- You can also provide fields a default value!
|
||||||
experience: u32,
|
experience: u32,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -57,10 +57,9 @@ pub fn main() void {
|
||||||
|
|
||||||
// FIX ME!
|
// FIX ME!
|
||||||
// Please pass our Character "glorp" to printCharacter():
|
// Please pass our Character "glorp" to printCharacter():
|
||||||
printCharacter( ??? );
|
printCharacter(???);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Note how this function's "c" parameter is a pointer to a Character struct.
|
// Note how this function's "c" parameter is a pointer to a Character struct.
|
||||||
fn printCharacter(c: *Character) void {
|
fn printCharacter(c: *Character) void {
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
//
|
//
|
||||||
const std = @import("std"); // single quotes
|
const std = @import("std"); // single quotes
|
||||||
|
|
||||||
const Elephant = struct{
|
const Elephant = struct {
|
||||||
letter: u8,
|
letter: u8,
|
||||||
tail: *Elephant = undefined,
|
tail: *Elephant = undefined,
|
||||||
visited: bool = false,
|
visited: bool = false,
|
||||||
|
|
|
@ -31,7 +31,7 @@ pub fn main() void {
|
||||||
// integer value from deepThought() OR the number 42:
|
// integer value from deepThought() OR the number 42:
|
||||||
var answer: u8 = result;
|
var answer: u8 = result;
|
||||||
|
|
||||||
std.debug.print("The Ultimate Answer: {}.\n",.{answer});
|
std.debug.print("The Ultimate Answer: {}.\n", .{answer});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deepThought() ?u8 {
|
fn deepThought() ?u8 {
|
||||||
|
@ -39,7 +39,6 @@ fn deepThought() ?u8 {
|
||||||
// But we'll leave this as-is. Sorry Deep Thought.
|
// But we'll leave this as-is. Sorry Deep Thought.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// Blast from the past:
|
// Blast from the past:
|
||||||
//
|
//
|
||||||
// Optionals are a lot like error union types which can either
|
// Optionals are a lot like error union types which can either
|
||||||
|
|
|
@ -1 +1,12 @@
|
||||||
|
37c37
|
||||||
|
< const n: u8 = 50;
|
||||||
|
---
|
||||||
|
> var n: u8 = 50;
|
||||||
|
40c40
|
||||||
|
< const pi: u8 = 314159;
|
||||||
|
---
|
||||||
|
> const pi: u32 = 314159;
|
||||||
|
42c42
|
||||||
|
< const negative_eleven: u8 = -11;
|
||||||
|
---
|
||||||
|
> const negative_eleven: i8 = -11;
|
||||||
|
|
|
@ -1 +1,12 @@
|
||||||
|
30c30
|
||||||
|
< const some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
|
||||||
|
---
|
||||||
|
> var some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
|
||||||
|
43c43
|
||||||
|
< const fourth = some_primes[???];
|
||||||
|
---
|
||||||
|
> const fourth = some_primes[3];
|
||||||
|
47c47
|
||||||
|
< const length = some_primes.???;
|
||||||
|
---
|
||||||
|
> const length = some_primes.len;
|
||||||
|
|
|
@ -1 +1,8 @@
|
||||||
|
23c23
|
||||||
|
< const leet = ???;
|
||||||
|
---
|
||||||
|
> const leet = le ++ et;
|
||||||
|
28c28
|
||||||
|
< const bit_pattern = [_]u8{ ??? } ** 3;
|
||||||
|
---
|
||||||
|
> const bit_pattern = [_]u8{ 1, 0, 0, 1 } ** 3;
|
||||||
|
|
|
@ -1 +1,12 @@
|
||||||
|
22c22
|
||||||
|
< const d: u8 = ziggy[???];
|
||||||
|
---
|
||||||
|
> const d: u8 = ziggy[4];
|
||||||
|
26c26
|
||||||
|
< const laugh = "ha " ???;
|
||||||
|
---
|
||||||
|
> const laugh = "ha " ** 3;
|
||||||
|
33c33
|
||||||
|
< const major_tom = major ??? tom;
|
||||||
|
---
|
||||||
|
> const major_tom = major ++ " " ++ tom;
|
||||||
|
|
|
@ -1 +1,8 @@
|
||||||
|
18,20c18,20
|
||||||
|
< Ziggy played guitar
|
||||||
|
< Jamming good with Andrew Kelley
|
||||||
|
< And the Spiders from Mars
|
||||||
|
---
|
||||||
|
> \\Ziggy played guitar
|
||||||
|
> \\Jamming good with Andrew Kelley
|
||||||
|
> \\And the Spiders from Mars
|
||||||
|
|
|
@ -1 +1,14 @@
|
||||||
|
16c16
|
||||||
|
< const x: u8 = 1;
|
||||||
|
---
|
||||||
|
> var x: u8 = 1;
|
||||||
|
27c27
|
||||||
|
< lang[???] = letters[x];
|
||||||
|
---
|
||||||
|
> lang[1] = letters[x];
|
||||||
|
29,30c29,30
|
||||||
|
< x = ???;
|
||||||
|
< lang[2] = letters[???];
|
||||||
|
---
|
||||||
|
> x = 5;
|
||||||
|
> lang[2] = letters[x];
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
26c26
|
||||||
|
< if (foo) {
|
||||||
|
---
|
||||||
|
> if (foo == 1) {
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
13c13
|
||||||
|
< var price: u8 = if ???;
|
||||||
|
---
|
||||||
|
> var price: u8 = if (discount) 17 else 20;
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
24c24
|
||||||
|
< while (???) {
|
||||||
|
---
|
||||||
|
> while (n < 1024) {
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
28c28
|
||||||
|
< while (n < 1000) : ??? {
|
||||||
|
---
|
||||||
|
> while (n < 1000) : (n *= 2) {
|
||||||
|
|
|
@ -1 +1,6 @@
|
||||||
|
27,28c27,28
|
||||||
|
< if (n % 3 == 0) ???;
|
||||||
|
< if (n % 5 == 0) ???;
|
||||||
|
---
|
||||||
|
> if (n % 3 == 0) continue;
|
||||||
|
> if (n % 5 == 0) continue;
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
21c21
|
||||||
|
< if (???) ???;
|
||||||
|
---
|
||||||
|
> if (n == 4) break;
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
18c18
|
||||||
|
< for (???) |???| {
|
||||||
|
---
|
||||||
|
> for (story) |scene| {
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
27c27
|
||||||
|
< for (bits) |bit, ???| {
|
||||||
|
---
|
||||||
|
> for (bits) |bit, i| {
|
||||||
|
|
|
@ -1 +1,16 @@
|
||||||
|
12c12
|
||||||
|
< const std = import standard library;
|
||||||
|
---
|
||||||
|
> const std = @import("std");
|
||||||
|
14c14
|
||||||
|
< function main() void {
|
||||||
|
---
|
||||||
|
> pub fn main() void {
|
||||||
|
19c19
|
||||||
|
< ??? (i <= stop_at) : (i += 1) {
|
||||||
|
---
|
||||||
|
> while (i <= stop_at) : (i += 1) {
|
||||||
|
23c23
|
||||||
|
< std.debug.print("{}", .{???});
|
||||||
|
---
|
||||||
|
> std.debug.print("{}", .{i});
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
31c31
|
||||||
|
< ??? deepThought() ??? {
|
||||||
|
---
|
||||||
|
> fn deepThought() u8 {
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
25c25
|
||||||
|
< fn twoToThe(???) u32 {
|
||||||
|
---
|
||||||
|
> fn twoToThe(my_number: u32) u32 {
|
||||||
|
|
|
@ -1 +1,18 @@
|
||||||
|
24,25c24,25
|
||||||
|
< fn printPowersOfTwo(numbers: [4]u16) ??? {
|
||||||
|
< loop (numbers) |n| {
|
||||||
|
---
|
||||||
|
> fn printPowersOfTwo(numbers: [4]u16) void {
|
||||||
|
> for (numbers) |n| {
|
||||||
|
34c34
|
||||||
|
< fn twoToThe(number: u16) ??? {
|
||||||
|
---
|
||||||
|
> fn twoToThe(number: u16) u16 {
|
||||||
|
38c38
|
||||||
|
< loop (n < number) : (n += 1) {
|
||||||
|
---
|
||||||
|
> while (n < number) : (n += 1) {
|
||||||
|
42c42
|
||||||
|
< return ???;
|
||||||
|
---
|
||||||
|
> return total;
|
||||||
|
|
|
@ -1 +1,8 @@
|
||||||
|
12c12
|
||||||
|
< ???,
|
||||||
|
---
|
||||||
|
> TooSmall,
|
||||||
|
29c29
|
||||||
|
< if (???) {
|
||||||
|
---
|
||||||
|
> if (number_error == MyNumberError.TooSmall) {
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
22c22
|
||||||
|
< var my_number: ??? = 5;
|
||||||
|
---
|
||||||
|
> var my_number: MyNumberError!u8 = 5;
|
||||||
|
|
|
@ -1 +1,8 @@
|
||||||
|
15c15
|
||||||
|
< var b: u32 = addTwenty(4) ??? 22;
|
||||||
|
---
|
||||||
|
> var b: u32 = addTwenty(4) catch 22;
|
||||||
|
22c22
|
||||||
|
< fn addTwenty(n: u32) ??? {
|
||||||
|
---
|
||||||
|
> fn addTwenty(n: u32) MyNumberError!u32 {
|
||||||
|
|
|
@ -1 +1,10 @@
|
||||||
|
62c62,68
|
||||||
|
< return detectProblems(n) ???
|
||||||
|
---
|
||||||
|
> return detectProblems(n) catch |err| {
|
||||||
|
> if (err == MyNumberError.TooSmall) {
|
||||||
|
> return 10;
|
||||||
|
> }
|
||||||
|
>
|
||||||
|
> return err;
|
||||||
|
> };
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
29c29
|
||||||
|
< var x = detect(n);
|
||||||
|
---
|
||||||
|
> var x = try detect(n);
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
22c22
|
||||||
|
< stdout.print("Hello world!\n", .{});
|
||||||
|
---
|
||||||
|
> try stdout.print("Hello world!\n", .{});
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
23c23
|
||||||
|
< std.debug.print("Two\n", .{});
|
||||||
|
---
|
||||||
|
> defer std.debug.print("Two\n", .{});
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
21c21
|
||||||
|
< std.debug.print(") ", .{}); // <---- how!?
|
||||||
|
---
|
||||||
|
> defer std.debug.print(") ", .{}); // <---- how!?
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
35c34
|
||||||
|
< std.debug.print("failed!\n", .{});
|
||||||
|
---
|
||||||
|
> errdefer std.debug.print("failed!\n", .{});
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
48a49
|
||||||
|
> else => std.debug.print("?", .{}),
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
33a34
|
||||||
|
> else => '!',
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
37a38
|
||||||
|
> else => unreachable,
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
36a37
|
||||||
|
> MyNumberError.TooSmall => std.debug.print("<4. ", .{}),
|
||||||
|
|
|
@ -1 +1,8 @@
|
||||||
|
12c12
|
||||||
|
< pub fn main() void {
|
||||||
|
---
|
||||||
|
> pub fn main() !void {
|
||||||
|
15c15
|
||||||
|
< const my_num: u32 = getNumber();
|
||||||
|
---
|
||||||
|
> const my_num: u32 = try getNumber();
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
23c23
|
||||||
|
< const Ops = enum { ??? };
|
||||||
|
---
|
||||||
|
> const Ops = enum { dec, inc, pow };
|
||||||
|
|
|
@ -1 +1,12 @@
|
||||||
|
32c32
|
||||||
|
< blue = ???,
|
||||||
|
---
|
||||||
|
> blue = 0x0000ff,
|
||||||
|
54c54
|
||||||
|
< \\ <span style="color: #{}">Blue</span>
|
||||||
|
---
|
||||||
|
> \\ <span style="color: #{x:0>6}">Blue</span>
|
||||||
|
59c59
|
||||||
|
< @enumToInt(???), // Oops! We're missing something!
|
||||||
|
---
|
||||||
|
> @enumToInt(Color.blue), // Oops! We're missing something!
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
38a39
|
||||||
|
> health: u8,
|
||||||
|
46a48
|
||||||
|
> .health = 100,
|
||||||
|
|
|
@ -1 +1,7 @@
|
||||||
|
44a45,50
|
||||||
|
> chars[1] = Character{
|
||||||
|
> .class = Class.bard,
|
||||||
|
> .gold = 10,
|
||||||
|
> .health = 100,
|
||||||
|
> .experience = 20,
|
||||||
|
> };
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
33c33
|
||||||
|
< num2 = ???;
|
||||||
|
---
|
||||||
|
> num2 = num1_pointer.*;
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
24c24
|
||||||
|
< const b: *u8 = &a; // fix this!
|
||||||
|
---
|
||||||
|
> const b: *const u8 = &a; // fix this!
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
34c34
|
||||||
|
< ??? p: ??? = undefined;
|
||||||
|
---
|
||||||
|
> var p: *u8 = undefined;
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
31c31
|
||||||
|
< ??? = 5; // fix me!
|
||||||
|
---
|
||||||
|
> x.* = 5; // fix me!
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
60c60
|
||||||
|
< printCharacter(???);
|
||||||
|
---
|
||||||
|
> printCharacter(&glorp);
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
21a22
|
||||||
|
> var elephantB = Elephant{ .letter = 'B' };
|
||||||
|
27a29
|
||||||
|
> elephantB.tail = &elephantC;
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
32c32
|
||||||
|
< var answer: u8 = result;
|
||||||
|
---
|
||||||
|
> var answer: u8 = result orelse 42;
|
||||||
|
|
Loading…
Reference in a new issue