September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,14 @@
|
|||
if x == 0:
|
||||
foo()
|
||||
elif x == 1:
|
||||
bar()
|
||||
elif x == 2:
|
||||
baz()
|
||||
else:
|
||||
qux()
|
||||
|
||||
x
|
||||
| 0 -> foo()
|
||||
| 1 -> bar()
|
||||
| 2 -> baz()
|
||||
| _ -> qux()
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
' lstack top value == 0 ? skip next instruction : don’t skip next instruction.
|
||||
" lstack top value > 0 ? skip next instruction : don’t skip next instruction.
|
||||
K lstack top value == 2nd value ? skip next instruction : don’t skip next instruction.
|
||||
L lstack top value > 2nd value ? skip next instruction : don’t skip next instruction.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
_`Enter integer n:`T'p`n = 0`>N`Enter integer m:`T'p`m = 0`>` and `Kp`m = n`;
|
||||
>`n > 0`d >`m > 0`d >Lp`m > n`;
|
||||
>`m < n`;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Enter integer n:
|
||||
i3
|
||||
n > 0
|
||||
Enter integer m:
|
||||
i0
|
||||
m = 0 and m < n
|
||||
|
|
@ -1 +0,0 @@
|
|||
const i = 5; static if (i == 7) { ... } else { ... }
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
auto i = 5;
|
||||
// is(T: U) tests if T is implicitly castable to U.
|
||||
// typeof(var) is the type of the variable.
|
||||
// also: is(T==U) checks if T is U.
|
||||
static if (is(typeof(i) : int)) {
|
||||
...
|
||||
} else {
|
||||
...
|
||||
}
|
||||
11
Task/Conditional-structures/DM/conditional-structures-1.dm
Normal file
11
Task/Conditional-structures/DM/conditional-structures-1.dm
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
if (condition)
|
||||
// Do thing, DM uses indentation for control flow.
|
||||
|
||||
if (condition)
|
||||
// Do thing
|
||||
|
||||
else if (condition)
|
||||
// Do thing
|
||||
|
||||
else
|
||||
// Do thing
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
// x will be 1 if condition is a true value, 2 otherwise.
|
||||
var/x = condition ? 1 : 2
|
||||
14
Task/Conditional-structures/DM/conditional-structures-3.dm
Normal file
14
Task/Conditional-structures/DM/conditional-structures-3.dm
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
switch (value)
|
||||
if (0)
|
||||
// Do thing if zero
|
||||
// DM does not have fall through of switch cases, so explicit break is not required.
|
||||
if (1, 2, 3)
|
||||
// Multiple values can be allowed by using commas
|
||||
|
||||
if (10 to 20)
|
||||
// Ranges are also allowed.
|
||||
// Ranges include the bounds (10 and 20 here),
|
||||
// and are checked in order if there is potential for overlap.
|
||||
|
||||
else
|
||||
// Fallback if nothing was matched.
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
set var 'Hello World'
|
||||
if test $var = 'Hello World'
|
||||
echo 'Welcome.'
|
||||
else if test $var = 'Bye World'
|
||||
echo 'Bye.'
|
||||
else
|
||||
echo 'Huh?'
|
||||
end
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
switch actually
|
||||
case az
|
||||
echo The word is "az".
|
||||
case 'a*z'
|
||||
echo Begins with a and ends with z.
|
||||
case 'a*'
|
||||
echo Begins with a.
|
||||
case 'z*'
|
||||
echo Ends with z.
|
||||
case '*'
|
||||
echo Neither begins with a or ends with z.
|
||||
end
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// conventional 'if/else if/else' statement
|
||||
if (args.isEmpty()) println("No arguments were supplied")
|
||||
else if (args.size == 1) println("One argument was supplied")
|
||||
else println("${args.size} arguments were supplied")
|
||||
|
||||
print("Enter an integer : ")
|
||||
val i = readLine()!!.toInt()
|
||||
|
||||
// 'when' statement (similar to 'switch' in C family languages)
|
||||
when (i) {
|
||||
0, 1 -> println("0 or 1")
|
||||
in 2 .. 9 -> println("Between 2 and 9")
|
||||
else -> println("Out of range")
|
||||
}
|
||||
|
||||
// both of these can be used as expressions as well as statements
|
||||
val s = if (i < 0) "negative" else "non-negative"
|
||||
println("$i is $s")
|
||||
val t = when {
|
||||
i > 0 -> "positive"
|
||||
i == 0 -> "zero"
|
||||
else -> "negative"
|
||||
}
|
||||
println("$i is $t")
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(set 'x 1)
|
||||
(if (= x 1) (println "is 1"))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(set 'x 0)
|
||||
(if (= x 1) (println "is 1") (println "not 1"))
|
||||
|
|
@ -0,0 +1 @@
|
|||
if arg~isa(.string) & arg~left(1) == "*" then call processArg arg
|
||||
|
|
@ -0,0 +1 @@
|
|||
if arg~isa(.string), arg~left(1) == "*" then call processArg arg
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
if y then x=6 /* Y must be either 0 or 1 */
|
||||
|
||||
|
||||
if t**2>u then x=y
|
||||
else x=-y
|
||||
|
||||
|
||||
|
||||
if t**2>u then do j=1 to 10; say prime(j); end
|
||||
else x=-y
|
||||
|
||||
|
||||
|
||||
if z>w+4 then do
|
||||
z=abs(z)
|
||||
say 'z='z
|
||||
end
|
||||
else do; z=0; say 'failed.'; end
|
||||
|
||||
|
||||
|
||||
if x>y & c*d<sqrt(pz) |,
|
||||
substr(abc,4,1)=='@' then if z=0 then call punt
|
||||
else nop
|
||||
else if z<0 then z=-y
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
/*the WHEN conditional operators are the same as */
|
||||
/*the IF conditional operators. */
|
||||
|
||||
select
|
||||
when t<0 then z=abs(u)
|
||||
when t=0 & y=0 then z=0
|
||||
when t>0 then do
|
||||
y=sqrt(z)
|
||||
z=u**2
|
||||
end
|
||||
|
||||
/*if control reaches this point and none of the WHENs */
|
||||
/*were satisfiied, a SYNTAX condition is raised (error).*/
|
||||
end
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
select
|
||||
when a=='angel' then many='host'
|
||||
when a=='ass' | a=='donkey' then many='pace'
|
||||
when a=='crocodile' then many='bask'
|
||||
when a=='crow' then many='murder'
|
||||
when a=='lark' then many='ascension'
|
||||
when a=='quail' then many='bevy'
|
||||
when a=='wolf' then many='pack'
|
||||
otherwise say
|
||||
say '*** error! ***'
|
||||
say a "isn't one of the known thingys."
|
||||
say
|
||||
exit 13
|
||||
end
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
if name="Pete" then
|
||||
-- do something
|
||||
elsif age>50 then
|
||||
-- do something
|
||||
elsif age<20 then
|
||||
-- do something
|
||||
else
|
||||
-- do something
|
||||
end if
|
||||
|
|
@ -0,0 +1 @@
|
|||
var = iff(flag?true_expr:false_expr)
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
switch v [with fallthrough] do
|
||||
case 1,2:
|
||||
-- do something
|
||||
case 3 then
|
||||
-- do something
|
||||
fallthrough
|
||||
case 4:
|
||||
-- do something
|
||||
break
|
||||
default:
|
||||
-- do something
|
||||
end switch
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#ilASM{
|
||||
[32]
|
||||
mov eax,[var]
|
||||
[64]
|
||||
mov rax,[var]
|
||||
[PE32]
|
||||
push eax -- uExitCode
|
||||
call "kernel32.dll","ExitProcess"
|
||||
[PE64]
|
||||
mov rcx,rax -- uExitCode
|
||||
call "kernel32.dll","ExitProcess"
|
||||
[ELF32]
|
||||
mov ebx,eax -- error_code (p1)
|
||||
mov eax,1 -- sys_exit(ebx=int error_code)
|
||||
int 0x80
|
||||
-- xor ebx,ebx -- (common requirement after int 0x80)
|
||||
[ELF64]
|
||||
mov rdi,rax -- error_code (p1)
|
||||
mov rax,60 -- sys_exit(rdi=int error_code)
|
||||
syscall
|
||||
[]
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
#Single line example
|
||||
#x is assumed to be scalar
|
||||
if(x < 3) message("x is less than 3") else if(x < 5) message("x is greater than or equal to 3 but less than 5") else message("x is greater than or equal to 5")
|
||||
#Block example
|
||||
if(x < 3)
|
||||
{
|
||||
x <- 3
|
||||
warning("x has been increased to 3")
|
||||
} else
|
||||
{
|
||||
y <- x^2
|
||||
}
|
||||
#It is important that the else keyword appears on the same line as the closing '}' of the if block.
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
#ifelse is a vectorised version of the if/else flow controllers, similar to the C-style ternary operator.
|
||||
x <- sample(1:10, 10)
|
||||
ifelse(x > 5, x^2, 0)
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
# Character input
|
||||
calories <- function(food) switch(food, apple=47, pizza=1500, stop("food not known"))
|
||||
calories("apple") # 47
|
||||
calories("banana") # throws an error
|
||||
# Numeric input
|
||||
alphabet <- function(number) switch(number, "a", "ab", "abc")
|
||||
alphabet(0) # null response
|
||||
alphabet(1) # "a"
|
||||
alphabet(2) # "ab"
|
||||
alphabet(3) # "abc"
|
||||
alphabet(4) # null response
|
||||
# Note that no 'otherwise' option is allowed when the input is numeric.
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
// This function will only be compiled if we are compiling on Linux
|
||||
#[cfg(target_os = "linux")]
|
||||
fn running_linux() {
|
||||
println!("This is linux");
|
||||
}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn running_linux() {
|
||||
println!("This is not linux");
|
||||
}
|
||||
|
||||
// If we are on linux, we must be using glibc
|
||||
#[cfg_attr(target_os = "linux", target_env = "gnu")]
|
||||
// We must either be compiling for ARM or on a little endian machine that doesn't have 32-bit pointers pointers, on a
|
||||
// UNIX like OS and only if we are doing a test build
|
||||
#[cfg(all(
|
||||
any(target_arch = "arm", target_endian = "little"),
|
||||
not(target_pointer_width = "32"),
|
||||
unix,
|
||||
test
|
||||
))]
|
||||
fn highly_specific_function() {}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
fn main() {
|
||||
if cfg!(target_os = "linux") {
|
||||
// Do something
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
trait PrintType {
|
||||
fn print_type();
|
||||
}
|
||||
|
||||
impl PrintType for char {
|
||||
fn print_type() {
|
||||
println!("char");
|
||||
}
|
||||
}
|
||||
|
||||
impl PrintType for f64 {
|
||||
fn print_type() {
|
||||
println!("64-bit float");
|
||||
}
|
||||
}
|
||||
|
||||
fn prints_type_of_args<T,U>(arg1: T, arg2: U)
|
||||
where T: PrintType
|
||||
U: PrintType
|
||||
{
|
||||
arg1.print_type();
|
||||
arg2.print_type();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
prints_type_of_args('a', 2.0);
|
||||
prints_type_of_args('a', 'b');
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
if some_conditional {
|
||||
do_stuff();
|
||||
} else if some_other_conditional
|
||||
do_other_stuff();
|
||||
} else {
|
||||
destroy_humanity();
|
||||
}
|
||||
|
||||
// If statements are also expressions and will yield the value of the last expression in each block
|
||||
let x = if y > z { y + 1 } else { z * 4 };
|
||||
|
||||
// Pattern matching may also be used
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
fn some_function(p: Option<Point>) {
|
||||
if let Some(Point { x: x_coord, y: y_coord }) = p {
|
||||
// Do something with x_coord and y_coord
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
fn some_other_function(p: Option<Point>) {
|
||||
match p {
|
||||
Some(Point { x: 0, y: 0 }) => println!("Point is on origin"),
|
||||
Some(Point { x: 0, y: _ }) | Some(Point { x: _, y: 0 }) => println!("Point is on an axis"),
|
||||
Some(Point {x: a, y: b}) if a == b println!("x and y are the same value"),
|
||||
Some(Point {x: ref mut a, y: ref b}) if x > 4 && y < 2 => println!("we got a mutable reference to x-value and an immutable reference to y-value."),
|
||||
op @ Some(p) => println!("op is the Option<Point> while p is the contained Point"),
|
||||
None => println!("We didn't get a point"),
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fn prints_args_dynamic(arg1: &PrintType, arg2: &PrintType) {
|
||||
arg1.print_type();
|
||||
arg2.print_type();
|
||||
}
|
||||
fn main() {
|
||||
prints_args_dynamic(&'a', &2.0);
|
||||
prints_args_dynamic(&6.3,&'c');
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
composite
|
||||
consonant
|
||||
|
|
@ -1 +0,0 @@
|
|||
(case <key> <clause1> <clause2> ...)
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
(display
|
||||
(case (* 2 3)
|
||||
((2 3 5 7) "prime")
|
||||
((1 4 6 8 9) "composite")))
|
||||
(newline)
|
||||
(display
|
||||
(case (car (list c d))
|
||||
((a e i o u) "vowel")
|
||||
((w y) "semivowel")
|
||||
(else "consonant")))
|
||||
(newline)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(if (eq s "Rosetta Code")
|
||||
"The well-known programming chrestomathy site"
|
||||
"Some other website, maybe, I dunno" )
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(case s
|
||||
("Rosetta Code" "Ah yes, the chrestomathy site")
|
||||
("Stack Overflow" "Oh dear me, having problems are you?")
|
||||
("Github" "Say no more")
|
||||
(else "Sorry, never heard of it") )
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(cond
|
||||
((eq s "Rosetta Code") "Chrestomathy site")
|
||||
((> n 37) "Some other appropriate value, presumably")
|
||||
(t "If you're seeing me, s wasn't equal to Rosetta Code and n must have been 37 or below") )
|
||||
13
Task/Conditional-structures/Zkl/conditional-structures.zkl
Normal file
13
Task/Conditional-structures/Zkl/conditional-structures.zkl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
if (x) y else z;
|
||||
if(a)b else if (c) else d; etc
|
||||
x:=(if (a) b else c);
|
||||
|
||||
a and b or c // usually the same as if(a) b else c, beware if b evals to False
|
||||
|
||||
switch(x){
|
||||
case(1){...}
|
||||
case("2"){...} // matches anything
|
||||
case(a)[fallthrough]{...} // no break, no break has to be explicit
|
||||
case(b){...}
|
||||
else {...} // case a C's default, has to be at the end
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue