all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
1
Task/Variables/0DESCRIPTION
Normal file
1
Task/Variables/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
Demonstrate the language's methods of variable declaration, initialization, assignment, datatypes, scope, referencing, and other variable related facilities.
|
||||
2
Task/Variables/1META.yaml
Normal file
2
Task/Variables/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Basic language learning
|
||||
1
Task/Variables/ALGOL-68/variables-1.alg
Normal file
1
Task/Variables/ALGOL-68/variables-1.alg
Normal file
|
|
@ -0,0 +1 @@
|
|||
int j;
|
||||
1
Task/Variables/ALGOL-68/variables-2.alg
Normal file
1
Task/Variables/ALGOL-68/variables-2.alg
Normal file
|
|
@ -0,0 +1 @@
|
|||
LONG REAL double1, double2, double3;
|
||||
2
Task/Variables/ALGOL-68/variables-3.alg
Normal file
2
Task/Variables/ALGOL-68/variables-3.alg
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
SHORT INT b1 := 2500;
|
||||
LONG INT elwood = 3*bsize, jake = bsize -2;
|
||||
1
Task/Variables/ALGOL-68/variables-4.alg
Normal file
1
Task/Variables/ALGOL-68/variables-4.alg
Normal file
|
|
@ -0,0 +1 @@
|
|||
FLEX[20]CHAR mystring;
|
||||
1
Task/Variables/ALGOL-68/variables-5.alg
Normal file
1
Task/Variables/ALGOL-68/variables-5.alg
Normal file
|
|
@ -0,0 +1 @@
|
|||
[]CHAR mytext = "The ALGOL 68 Language";
|
||||
2
Task/Variables/AWK/variables-1.awk
Normal file
2
Task/Variables/AWK/variables-1.awk
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
a = 1 # Here we declare a numeric variable
|
||||
fruit = "banana" # Here we declare a string datatype
|
||||
1
Task/Variables/AWK/variables-2.awk
Normal file
1
Task/Variables/AWK/variables-2.awk
Normal file
|
|
@ -0,0 +1 @@
|
|||
x = y = z = 3
|
||||
7
Task/Variables/AWK/variables-3.awk
Normal file
7
Task/Variables/AWK/variables-3.awk
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
function foo(j k) {
|
||||
# j is an argument passed from caller
|
||||
# k is a dummy not passed by caller, but because it is in the
|
||||
# argument list, it will have a scope local to the function
|
||||
k = length(j)
|
||||
print j "contains " k " characters"
|
||||
}
|
||||
7
Task/Variables/Ada/variables.ada
Normal file
7
Task/Variables/Ada/variables.ada
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
declare
|
||||
X : String := "Hello"; -- Create and initialize a local variable
|
||||
Y : Integer; -- Create an uninitialized variable
|
||||
Z : Integer renames Y: -- Rename Y (creates a view)
|
||||
begin
|
||||
Y := 1; -- Assign variable
|
||||
end; -- End of the scope
|
||||
1
Task/Variables/AppleScript/variables-1.applescript
Normal file
1
Task/Variables/AppleScript/variables-1.applescript
Normal file
|
|
@ -0,0 +1 @@
|
|||
set x to 1
|
||||
4
Task/Variables/AppleScript/variables-2.applescript
Normal file
4
Task/Variables/AppleScript/variables-2.applescript
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
global x
|
||||
set x to 1
|
||||
local y
|
||||
set y to 2
|
||||
15
Task/Variables/AppleScript/variables-3.applescript
Normal file
15
Task/Variables/AppleScript/variables-3.applescript
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
on localx()
|
||||
set x to 0 -- implicit local
|
||||
return x
|
||||
end localx
|
||||
|
||||
on globalx()
|
||||
set x to 0 -- implicit local
|
||||
return my x
|
||||
end globalx
|
||||
|
||||
on run
|
||||
set x to 1 -- top-level implicit global
|
||||
return {localx(), globalx()}
|
||||
end run
|
||||
--> RETURNS: {0, 1}
|
||||
1
Task/Variables/AppleScript/variables-4.applescript
Normal file
1
Task/Variables/AppleScript/variables-4.applescript
Normal file
|
|
@ -0,0 +1 @@
|
|||
property x : 1
|
||||
10
Task/Variables/AutoHotkey/variables.ahk
Normal file
10
Task/Variables/AutoHotkey/variables.ahk
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
x = hello ; assign verbatim as a string
|
||||
z := 3 + 4 ; assign an expression
|
||||
if !y ; uninitialized variables are assumed to be 0 or "" (blank string)
|
||||
Msgbox %x% ; variable dereferencing is done by surrounding '%' signs
|
||||
fx()
|
||||
{
|
||||
local x ; variable default scope in a function is local anyways
|
||||
global y ;
|
||||
static z=4 ; initialized once, then value is remembered between function calls
|
||||
}
|
||||
16
Task/Variables/BASIC/variables.basic
Normal file
16
Task/Variables/BASIC/variables.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
10 LET A=1.3
|
||||
20 LET B%=1.3: REM THE SIGIL INDICATES AN INTEGER, SO THIS WILL BE ROUNDED DOWN
|
||||
30 LET C$="0121": REM THE SIGIL INDICATES A STRING DATA TYPE. THE LEADING ZERO IS NOT TRUNCATED
|
||||
40 DIM D(10): REM CREATE AN ARRAY OF 10 DIGITS
|
||||
50 DIM E$(5.10): REM CREATE AN ARRAY OF 5 STRINGS, WITH A MAXIMUM LENGTH OF 10 CHARACTERS
|
||||
60 LET D(1)=1.3: REM ASSIGN THE FIRST ELEMENT OF D
|
||||
70 LET E$(3)="ROSE": REM ASSIGN A VALUE TO THE THIRD STRING
|
||||
80 PRINT D(3): REM UNASSIGNED ARRAY ELEMENTS HAVE A DEFAULT VALUE OF ZERO
|
||||
90 PRINT E$(3): REM TEN SPACES BECAUSE STRING ARRAYS ARE NOT DYNAMIC
|
||||
100 PRINT E$(3);"TTA CODE": REM THERE WILL BE SPACES BETWEEN ROSE AND ETTA
|
||||
110 DIM F%(10):REM INTEGERS USE LESS SPACE THAN FLOATING POINT VALUES
|
||||
120 PRINT G: REM THIS IS AN ERROR BECAUSE F HAS NOT BEEN DEFINED
|
||||
130 PRINT D(0): REM THIS IS AN ERROR BECAUSE ELEMENTS ARE NUMBERED FROM ONE
|
||||
140 LET D(11)=6: REM THIS IS AN ERROR BECAUSE D ONLY HAS 10 ELEMENTS
|
||||
150 PRINT F%: REM THIS IS AN ERROR BECAUSE WE HAVE NOT PROVIDED AN ELEMENT NUMBER
|
||||
160 END
|
||||
33
Task/Variables/BBC-BASIC/variables.bbc
Normal file
33
Task/Variables/BBC-BASIC/variables.bbc
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
REM BBC BASIC (for Windows) has the following scalar variable types;
|
||||
REM the type is explicitly indicated by means of a suffix character.
|
||||
REM Variable names must start with A-Z, a-z, _ or `, and may contain
|
||||
REM any of those characters plus 0-9 and @; they are case-sensitive.
|
||||
|
||||
A& = 123 : REM Unsigned 8-bit byte (0 to 255)
|
||||
A% = 12345678 : REM Signed 32-bit integer (-2147483648 to +2147483647)
|
||||
A = 123.45E6 : REM Variant 40-bit float or 32-bit integer (no suffix)
|
||||
A# = 123.45E6 : REM Variant 64-bit double or 32-bit integer
|
||||
A$ = "Abcdef" : REM String (0 to 65535 bytes)
|
||||
|
||||
REM Scalar variables do not need to be declared but must be initialised
|
||||
REM before being read, otherwise a 'No such variable' error is reported
|
||||
REM The static integer variables A% to Z% are permanently defined.
|
||||
|
||||
REM BBC BASIC also has indirection operators which allow variable-like
|
||||
REM entities to be created in memory:
|
||||
|
||||
DIM addr 7 : REM Allocate 8 bytes of heap
|
||||
?addr = 123 : REM Unsigned 8-bit byte (0 to 255)
|
||||
!addr = 12345 : REM Signed 32-bit integer (-2147483648 to +2147483647)
|
||||
|addr = 12.34 : REM Variant 40-bit or 64-bit float or 32-bit integer
|
||||
$addr = "Abc" : REM String terminated by CR (0 to 65535 bytes)
|
||||
$$addr = "Abc": REM String terminated by NUL (0 to 65535 bytes)
|
||||
|
||||
REM The integer indirection operators may be used in a dyadic form:
|
||||
offset = 4
|
||||
addr?offset = 12345678 : REM Unsigned 8-bit byte at addr+offset
|
||||
addr!offset = 12345678 : REM Signed 32-bit integer at addr+offset
|
||||
|
||||
REM All variables in BBC BASIC have global scope unless they are used
|
||||
REM as a formal parameter of a function or procedure, or are declared
|
||||
REM as LOCAL or PRIVATE. This is different from most other BASICs.
|
||||
1
Task/Variables/C++/variables.cpp
Normal file
1
Task/Variables/C++/variables.cpp
Normal file
|
|
@ -0,0 +1 @@
|
|||
int a;
|
||||
1
Task/Variables/C/variables-1.c
Normal file
1
Task/Variables/C/variables-1.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
int j;
|
||||
1
Task/Variables/C/variables-2.c
Normal file
1
Task/Variables/C/variables-2.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
double double1, double2, double3;
|
||||
2
Task/Variables/C/variables-3.c
Normal file
2
Task/Variables/C/variables-3.c
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
short b1 = 2500;
|
||||
long elwood = 3*BSIZE, jake = BSIZE -2;
|
||||
1
Task/Variables/C/variables-4.c
Normal file
1
Task/Variables/C/variables-4.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
char mystring[21];
|
||||
1
Task/Variables/C/variables-5.c
Normal file
1
Task/Variables/C/variables-5.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
const char * mytext = "The C Language";
|
||||
3
Task/Variables/D/variables.d
Normal file
3
Task/Variables/D/variables.d
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
float bite = 36.321; ///_Defines a floating-point number (float), "bite", with a value of 36.321
|
||||
float[3] bites; ///_Defines a static array of 3 floats
|
||||
float[] more_bites; ///_Defines a dynamic array of floats
|
||||
4
Task/Variables/DWScript/variables.dwscript
Normal file
4
Task/Variables/DWScript/variables.dwscript
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
var i := 123; // inferred type of i is Integer
|
||||
var s := 'abc'; // inferred type of s is String
|
||||
var o := TObject.Create; // inferred type of o is TObject
|
||||
var s2 := o.ClassName; // inferred type of s2 is String as that's the type returned by ClassName
|
||||
14
Task/Variables/Delphi/variables.delphi
Normal file
14
Task/Variables/Delphi/variables.delphi
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
var
|
||||
i: Integer;
|
||||
s: string;
|
||||
o: TObject;
|
||||
begin
|
||||
i := 123;
|
||||
s := 'abc';
|
||||
o := TObject.Create;
|
||||
try
|
||||
// ...
|
||||
finally
|
||||
o.Free;
|
||||
end;
|
||||
end;
|
||||
2
Task/Variables/E/variables-1.e
Normal file
2
Task/Variables/E/variables-1.e
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def x := 1
|
||||
x + x # returns 2
|
||||
3
Task/Variables/E/variables-2.e
Normal file
3
Task/Variables/E/variables-2.e
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def var x := 1
|
||||
x := 2
|
||||
x # returns 2
|
||||
7
Task/Variables/E/variables-3.e
Normal file
7
Task/Variables/E/variables-3.e
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def var x := 1
|
||||
x += 1 # equivalent to x := x + 1, or x := x.add(1)
|
||||
x # returns 2
|
||||
|
||||
def var list := ["x"]
|
||||
list with= "y" # equivalent to list := list.with("y")
|
||||
list # returns ["x", "y"]
|
||||
1
Task/Variables/E/variables-4.e
Normal file
1
Task/Variables/E/variables-4.e
Normal file
|
|
@ -0,0 +1 @@
|
|||
def [hair, eyes, var shirt, var pants] := ["black", "brown", "plaid", "jeans"]
|
||||
1
Task/Variables/E/variables-5.e
Normal file
1
Task/Variables/E/variables-5.e
Normal file
|
|
@ -0,0 +1 @@
|
|||
[shirt, pants] := ["white", "black"] # This does not do anything useful.
|
||||
2
Task/Variables/E/variables-6.e
Normal file
2
Task/Variables/E/variables-6.e
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def list := [def x := timer.now(), x] # two copies of the current time
|
||||
list[0] == x # x is still visible here; returns true
|
||||
10
Task/Variables/E/variables-7.e
Normal file
10
Task/Variables/E/variables-7.e
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def makeSum() {
|
||||
var a := 0
|
||||
var b := 0
|
||||
return [&a, &b, fn { a + b }]
|
||||
}
|
||||
|
||||
def [&x, &y, sum] := makeSum()
|
||||
x := 3
|
||||
y := 4
|
||||
sum() # returns 7
|
||||
8
Task/Variables/E/variables-8.e
Normal file
8
Task/Variables/E/variables-8.e
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def getUniqueId(&counter) {
|
||||
counter += 1
|
||||
return counter
|
||||
}
|
||||
|
||||
var idc := 0
|
||||
getUniqueId(&idc) # returns 1
|
||||
getUniqueId(&idc) # returns 2
|
||||
3
Task/Variables/Ela/variables-1.ela
Normal file
3
Task/Variables/Ela/variables-1.ela
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
x = 42
|
||||
|
||||
sum x y = x + y
|
||||
4
Task/Variables/Ela/variables-2.ela
Normal file
4
Task/Variables/Ela/variables-2.ela
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
sum x y = let z = x + y in z
|
||||
|
||||
sum x y = z
|
||||
where z = x + y
|
||||
11
Task/Variables/Factor/variables.factor
Normal file
11
Task/Variables/Factor/variables.factor
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
SYMBOL: foo
|
||||
|
||||
: use-foo ( -- )
|
||||
1 foo set
|
||||
foo get 2 + foo set ! foo now = 3
|
||||
foo get number>string print ;
|
||||
|
||||
:: named-param-example ( a b -- )
|
||||
a b + number>string print ;
|
||||
|
||||
: local-example ( -- str ) [let "a" :> b "c" :> a a " " b 3append ] ;
|
||||
3
Task/Variables/Forth/variables-1.fth
Normal file
3
Task/Variables/Forth/variables-1.fth
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
: hypot ( a b -- a^2 + b^2 )
|
||||
LOCALS| b a | \ note: reverse order from the conventional stack comment
|
||||
b b * a a * + ;
|
||||
2
Task/Variables/Forth/variables-2.fth
Normal file
2
Task/Variables/Forth/variables-2.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: hypot { a b -- a^2 + b^2 } \ text between "--" and "}" remains commentary
|
||||
a a * b b * + ;
|
||||
2
Task/Variables/Forth/variables-3.fth
Normal file
2
Task/Variables/Forth/variables-3.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: length { F: a F: b F: c -- len } \ floating point locals
|
||||
a a F* b b F* F+ c c F* F+ FSQRT ;
|
||||
28
Task/Variables/Fortran/variables.f
Normal file
28
Task/Variables/Fortran/variables.f
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
program test
|
||||
implicit none
|
||||
|
||||
integer :: i !scalar integer
|
||||
integer,dimension(10) :: ivec !integer vector
|
||||
real :: r !scalar real
|
||||
real,dimension(10) :: rvec !real vector
|
||||
character(len=:),allocatable :: char1, char2 !fortran 2003 allocatable strings
|
||||
|
||||
!assignments:
|
||||
|
||||
!-- scalars:
|
||||
i = 1
|
||||
r = 3.14
|
||||
|
||||
!-- vectors:
|
||||
ivec = 1 !(all elements set to 1)
|
||||
ivec(1:5) = 2
|
||||
|
||||
rvec(1:9) = 0.0
|
||||
rvec(10) = 1.0
|
||||
|
||||
!-- strings:
|
||||
char1 = 'hello world!'
|
||||
char2 = char1 !copy from one string to another
|
||||
char2(1:1) = 'H' !change first character
|
||||
|
||||
end program test
|
||||
24
Task/Variables/GAP/variables.gap
Normal file
24
Task/Variables/GAP/variables.gap
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# At top level, global variables are declared when they are assigned, so one only writes
|
||||
global_var := 1;
|
||||
|
||||
# In a function, local variables are declared like this
|
||||
func := function(n)
|
||||
local a;
|
||||
a := n*n;
|
||||
return n + a;
|
||||
end;
|
||||
|
||||
# One can test whether a variable is assigned
|
||||
IsBound(global_var);
|
||||
# true;
|
||||
|
||||
# And destroy a variable
|
||||
Unbind(global_var);
|
||||
|
||||
# This works with list elements too
|
||||
u := [11, 12, , 14];
|
||||
IsBound(u[4]);
|
||||
# true
|
||||
IsBound(u[3]);
|
||||
# false
|
||||
Unbind(u[4]);
|
||||
1
Task/Variables/Go/variables-1.go
Normal file
1
Task/Variables/Go/variables-1.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
x := 3
|
||||
2
Task/Variables/Go/variables-2.go
Normal file
2
Task/Variables/Go/variables-2.go
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var x int // declaration
|
||||
x = 3 // assignment
|
||||
4
Task/Variables/Go/variables-3.go
Normal file
4
Task/Variables/Go/variables-3.go
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
y := x+1 // y is int, assuming declaration above
|
||||
same := x == y // same declared as bool
|
||||
p := &same // type of p is pointer to bool
|
||||
pi := math.Floor(math.Pi) // math.Floor returns float64, so that is the type of pi
|
||||
2
Task/Variables/Go/variables-4.go
Normal file
2
Task/Variables/Go/variables-4.go
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var x, y int // two variables, initialized to zero.
|
||||
var p *int // initialized to nil
|
||||
4
Task/Variables/Go/variables-5.go
Normal file
4
Task/Variables/Go/variables-5.go
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
var (
|
||||
x, y int
|
||||
s string
|
||||
)
|
||||
4
Task/Variables/Go/variables-6.go
Normal file
4
Task/Variables/Go/variables-6.go
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
x, y = y, x // swap x and y
|
||||
sinX, cosX = math.Sincos(x) // Sincos function returns two values
|
||||
// map lookup optionally returns a second value indicating if the key was found.
|
||||
value, ok = mapObject[key]
|
||||
5
Task/Variables/Go/variables-7.go
Normal file
5
Task/Variables/Go/variables-7.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
func increase (x int) (more int) {
|
||||
x++
|
||||
more = x+x
|
||||
return
|
||||
}
|
||||
1
Task/Variables/Go/variables-8.go
Normal file
1
Task/Variables/Go/variables-8.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
x, y := 3, 4
|
||||
11
Task/Variables/Haskell/variables-1.hs
Normal file
11
Task/Variables/Haskell/variables-1.hs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
foobar = 15
|
||||
|
||||
f x = x + foobar
|
||||
where foobar = 15
|
||||
|
||||
f x = let foobar = 15
|
||||
in x + foobar
|
||||
|
||||
f x = do
|
||||
let foobar = 15
|
||||
return $ x + foobar
|
||||
7
Task/Variables/Haskell/variables-2.hs
Normal file
7
Task/Variables/Haskell/variables-2.hs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
main = do
|
||||
s <- getLine
|
||||
print (s, s)
|
||||
|
||||
-- The above is equivalent to:
|
||||
|
||||
main = getLine >>= \s -> print (s, s)
|
||||
4
Task/Variables/Haskell/variables-3.hs
Normal file
4
Task/Variables/Haskell/variables-3.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
funkshun True x = x + 1
|
||||
funkshun False x = x - 1
|
||||
|
||||
foobar = funkshun True 5 + funkshun False 5 -- 6 + 4
|
||||
4
Task/Variables/Haskell/variables-4.hs
Normal file
4
Task/Variables/Haskell/variables-4.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
funkshun m = case foo m of
|
||||
[a, b] -> a - b
|
||||
a : b : c : rest -> a + b - c + sum rest
|
||||
a -> sum a
|
||||
3
Task/Variables/Haskell/variables-5.hs
Normal file
3
Task/Variables/Haskell/variables-5.hs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
signum x | x > 0 = 1
|
||||
| x < 0 = -1
|
||||
| otherwise = 0
|
||||
10
Task/Variables/Haskell/variables-6.hs
Normal file
10
Task/Variables/Haskell/variables-6.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
dotProduct :: [Int] -> [Int] -> Int
|
||||
dotProduct ns ms = sum $ zipWith (+) ns ms
|
||||
-- Without the type signature, dotProduct would
|
||||
-- have a more general type.
|
||||
|
||||
foobar :: Num a => a
|
||||
foobar = 15
|
||||
-- Without the type signature, the monomorphism
|
||||
-- restriction would cause foobar to have a less
|
||||
-- general type.
|
||||
33
Task/Variables/HicEst/variables-1.hicest
Normal file
33
Task/Variables/HicEst/variables-1.hicest
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
! Strings and arrays must be declared.
|
||||
! Everything else is 8-byte float, READ/WRITE converts
|
||||
CHARACTER str="abcdef", str2*345, str3*1E6/"xyz"/
|
||||
REAL, PARAMETER :: named_constant = 3.1415
|
||||
REAL :: n=2, cols=4, vec(cols), mtx(n, cols)
|
||||
DATA vec/2,3,4,5/, mtx/1,2,3.1415,4, 5,6,7,8/
|
||||
|
||||
named = ALIAS(alpha, beta, gamma) ! gamma == named(3)
|
||||
ALIAS(vec,n, subvec,2) ! share subvec and vec(n...n+1)
|
||||
ALIAS(str,3, substr,n) ! share substr and str(3:3+n-1)
|
||||
|
||||
a = EXP(b + c) ! assign/initialze a=1, b=0, c=0
|
||||
str = "blahblah" ! truncate/expand if needed
|
||||
beta = "blahblah" ! illegal
|
||||
|
||||
CALL noArguments_noUSE ! global scope SUBROUTINE
|
||||
CALL Arguments_or_USE(a) ! local scope SUBROUTINE
|
||||
t = func() ! local scope FUNCTION
|
||||
|
||||
SUBROUTINE noArguments_noUSE() ! all global
|
||||
vec2 = $ ! 1,2,3,...
|
||||
END
|
||||
|
||||
SUBROUTINE Arguments_or_USE(var) ! all local
|
||||
USE : vec ! use global object
|
||||
var = SUM(vec)
|
||||
t = TIME() ! local, static, USEd by func()
|
||||
END
|
||||
|
||||
FUNCTION func() ! all local
|
||||
USE Arguments_or_USE : t ! use local object
|
||||
func = t
|
||||
END
|
||||
13
Task/Variables/HicEst/variables-2.hicest
Normal file
13
Task/Variables/HicEst/variables-2.hicest
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
global gvar # a global
|
||||
|
||||
procedure main(arglist) # arglist is a parameter of main
|
||||
local a,b,i,x # a, b, i, x are locals withing main
|
||||
static y # a static (silly in main)
|
||||
|
||||
x := arglist[1]
|
||||
a := 1.0
|
||||
i := 10
|
||||
b := [x,a,i,b]
|
||||
|
||||
# ... rest of program
|
||||
end
|
||||
1
Task/Variables/J/variables-1.j
Normal file
1
Task/Variables/J/variables-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
val=. 0
|
||||
12
Task/Variables/J/variables-2.j
Normal file
12
Task/Variables/J/variables-2.j
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fun =: 3 :0
|
||||
val1 =: 0
|
||||
val1 =. 2
|
||||
val2 =. 3
|
||||
val1, val2
|
||||
)
|
||||
fun''
|
||||
2 3
|
||||
val1
|
||||
0
|
||||
val2
|
||||
|value error
|
||||
6
Task/Variables/J/variables-3.j
Normal file
6
Task/Variables/J/variables-3.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
fun1 =: 3 :0
|
||||
val3=. 0
|
||||
val3=: 0
|
||||
)
|
||||
fun1''
|
||||
|domain error
|
||||
5
Task/Variables/J/variables-4.j
Normal file
5
Task/Variables/J/variables-4.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
fun2 =: 3 :0
|
||||
val4=. 0
|
||||
3 :'val4=:y' y
|
||||
)
|
||||
fun2 ''
|
||||
3
Task/Variables/Java/variables-1.java
Normal file
3
Task/Variables/Java/variables-1.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
int a;
|
||||
double b;
|
||||
AClassNameHere c;
|
||||
1
Task/Variables/Java/variables-2.java
Normal file
1
Task/Variables/Java/variables-2.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
int a, b, c;
|
||||
6
Task/Variables/Java/variables-3.java
Normal file
6
Task/Variables/Java/variables-3.java
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
int a = 5;
|
||||
double b;
|
||||
int c = 5, d = 6, e, f;
|
||||
String x = "test";
|
||||
String y = x;
|
||||
b = 3.14;
|
||||
10
Task/Variables/Java/variables-4.java
Normal file
10
Task/Variables/Java/variables-4.java
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
final String x = "blah";
|
||||
final String y;
|
||||
final double[] nums = new double[15];
|
||||
y = "test";
|
||||
x = "blahblah"; //not legal
|
||||
nums[5] = 2.5; //legal
|
||||
nums = new double[10]; //not legal
|
||||
final Date now = new java.util.Date();
|
||||
now.setTime(1234567890); //legal
|
||||
now = new Date(1234567890); //not legal
|
||||
67
Task/Variables/JavaScript/variables.js
Normal file
67
Task/Variables/JavaScript/variables.js
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
// a globally-scoped variable
|
||||
var a=1;
|
||||
|
||||
// global scope
|
||||
function one(){
|
||||
alert(a);
|
||||
}
|
||||
|
||||
// local scope
|
||||
function two(a){
|
||||
alert(a);
|
||||
}
|
||||
|
||||
// local scope again
|
||||
function three(){
|
||||
var a = 3;
|
||||
alert(a);
|
||||
}
|
||||
|
||||
// Intermediate: no such thing as block scope in javascript
|
||||
function four(){
|
||||
if(true){
|
||||
var a=4;
|
||||
}
|
||||
|
||||
alert(a); // alerts '4', not the global value of '1'
|
||||
}
|
||||
|
||||
|
||||
// Intermediate: object properties
|
||||
function Five(){
|
||||
this.a = 5;
|
||||
}
|
||||
|
||||
|
||||
// Advanced: closure
|
||||
var six = function(){
|
||||
var foo = 6;
|
||||
|
||||
return function(){
|
||||
// javascript "closure" means I have access to foo in here,
|
||||
// because it is defined in the function in which I was defined.
|
||||
alert(foo);
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
// Advanced: prototype-based scope resolution
|
||||
function Seven(){
|
||||
this.a = 7;
|
||||
}
|
||||
|
||||
// [object].prototype.property loses to [object].property in the scope chain
|
||||
Seven.prototype.a = -1; // won't get reached, because 'a' is set in the constructor above.
|
||||
Seven.prototype.b = 8; // Will get reached, even though 'b' is NOT set in the constructor.
|
||||
|
||||
|
||||
|
||||
// These will print 1-8
|
||||
one();
|
||||
two(2);
|
||||
three();
|
||||
four();
|
||||
alert(new Five().a);
|
||||
six();
|
||||
alert(new Seven().a);
|
||||
alert(new Seven().b);
|
||||
1
Task/Variables/Joy/variables-1.joy
Normal file
1
Task/Variables/Joy/variables-1.joy
Normal file
|
|
@ -0,0 +1 @@
|
|||
[] unstack
|
||||
1
Task/Variables/Joy/variables-2.joy
Normal file
1
Task/Variables/Joy/variables-2.joy
Normal file
|
|
@ -0,0 +1 @@
|
|||
42
|
||||
1
Task/Variables/Joy/variables-3.joy
Normal file
1
Task/Variables/Joy/variables-3.joy
Normal file
|
|
@ -0,0 +1 @@
|
|||
stack
|
||||
15
Task/Variables/Julia/variables-1.julia
Normal file
15
Task/Variables/Julia/variables-1.julia
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#declaration/assignment, declaration is optional
|
||||
x::Int32 = 1
|
||||
#datatypes are inferred dynamically, but can also be set thru convert functions and datatype literals
|
||||
x = 1 #x is inferred as Int, which is Int32 for 32-bit machines, Int64 for 64-bit machines
|
||||
#variable reference
|
||||
julia>x
|
||||
1
|
||||
|
||||
x = int8(1) #x is of type Int8
|
||||
x = 1.0 #x is Float64
|
||||
x = y = 1 #assign both x and y to 1
|
||||
global x = 1 #assigns 1 to global variable x (used inside scope blocks)
|
||||
local x = 1 #assigns 1 to local variable x (used inside scope blocks)
|
||||
x = 'a' #x is a 'Char' type, designated by single quotes
|
||||
x = "a" #x is a 1-element string, designated by double quotes
|
||||
2
Task/Variables/Julia/variables-2.julia
Normal file
2
Task/Variables/Julia/variables-2.julia
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
const e = 2.71828182845904523536
|
||||
const pi = 3.14159265358979323846
|
||||
29
Task/Variables/Liberty-BASIC/variables.liberty
Normal file
29
Task/Variables/Liberty-BASIC/variables.liberty
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
'In Liberty BASIC variables are either string or numeric.
|
||||
'A variable name can start with any letter and it can contain both letters and numerals, as well as dots (for example: user.firstname).
|
||||
'There is no practical limit to the length of a variable name... up to ~2M characters.
|
||||
'The variable names are case sensitive.
|
||||
|
||||
'assignments: -numeric variables. LB assumes integers unless assigned or calculated otherwise.
|
||||
'Because of its Smalltalk heritage, LB integers are of arbitrarily long precision.
|
||||
'They lose this if a calculation yields a non-integer, switching to floating point.
|
||||
i = 1
|
||||
r = 3.14
|
||||
|
||||
'assignments -string variables. Any string-length, from zero to ~2M.
|
||||
t$ ="21:12:45"
|
||||
flag$ ="TRUE"
|
||||
|
||||
'assignments -1D or 2D arrays
|
||||
'A default array size of 10 is available. Larger arrays need pre-'DIM'ming.
|
||||
height( 3) =1.87
|
||||
dim height( 50)
|
||||
height( 23) =123.5
|
||||
potential( 3, 5) =4.5
|
||||
name$( 4) ="John"
|
||||
|
||||
'There are no Boolean /bit variables as such.
|
||||
|
||||
'Arrays in a main program are global.
|
||||
'However variables used in the main program code are not visible inside functions and subroutines.
|
||||
'They can be declared 'global' if such visibility is desired.
|
||||
'Functions can receive variables by name or by reference.
|
||||
20
Task/Variables/Logo/variables.logo
Normal file
20
Task/Variables/Logo/variables.logo
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
make "g1 0
|
||||
name 2 "g2 ; same as make with parameters reversed
|
||||
global "g3 ; no initial value
|
||||
to func :x
|
||||
make "g4 4 ; still global
|
||||
localmake "L1 6
|
||||
local ["L2 "L3] ; local variables, collection syntax
|
||||
func2 :g4
|
||||
print :L2 ; 9, modified by func2
|
||||
print :L3 ; L3 has no value, was not modified by func2
|
||||
end
|
||||
to func2 :y
|
||||
make "g3 :y
|
||||
make "L2 :L1 + 3 ; dynamic scope: can see variables of callers
|
||||
localmake "L3 5 ; locally override L3 from caller
|
||||
(print :y :L1 :L2 :L3) ; 4 6 9 5
|
||||
end
|
||||
print :g4 ; 4
|
||||
print :L1 ; L1 has no value
|
||||
print name? "L1 ; false, L1 is not bound in the current scope
|
||||
12
Task/Variables/LotusScript/variables.lotusscript
Normal file
12
Task/Variables/LotusScript/variables.lotusscript
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
Sub Click()
|
||||
'a few declarations as example
|
||||
Dim s as New NotesSession ' declaring a New NotesSession actually returns the current, active NotesSession
|
||||
Dim i as Integer ' i = 0
|
||||
Dim s as String ' s= ""
|
||||
Dim v as Variant ' v is nothing
|
||||
Dim l as Long ' l = 0
|
||||
Dim doc as NotesDocument 'doc is EMTPY
|
||||
|
||||
'...
|
||||
|
||||
End Sub
|
||||
4
Task/Variables/Lua/variables-1.lua
Normal file
4
Task/Variables/Lua/variables-1.lua
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
a = 1 -- Here we declare a numeric variable
|
||||
fruit = "banana" -- Here we declare a string datatype
|
||||
needspeeling = True -- This is a boolean
|
||||
local b = 2 -- This variable declaration is prefixed with a scope modifier
|
||||
1
Task/Variables/Lua/variables-2.lua
Normal file
1
Task/Variables/Lua/variables-2.lua
Normal file
|
|
@ -0,0 +1 @@
|
|||
A, B, C, D, E = 2, 4, 6, 8, "It's never too late"
|
||||
17
Task/Variables/MATLAB/variables-1.m
Normal file
17
Task/Variables/MATLAB/variables-1.m
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
a = 4; % declare variable and initialize double value,
|
||||
s = 'abc'; % string
|
||||
i8 = int8(5); % signed byte
|
||||
u8 = uint8(5); % unsigned byte
|
||||
i16 = int16(5); % signed 2 byte
|
||||
u16 = uint16(5); % unsigned 2 byte integer
|
||||
i32 = int32(5); % signed 4 byte integer
|
||||
u32 = uint32(5);% unsigned 4 byte integers
|
||||
i64 = int64(5); % signed 8 byte integer
|
||||
u64 = uint64(5);% unsigned 8 byte integer
|
||||
f32 = float32(5); % single precission floating point number
|
||||
f64 = float64(5); % double precission floating point number , float 64 is the default data type.
|
||||
|
||||
c = 4+5i; % complex number
|
||||
colvec = [1;2;4]; % column vector
|
||||
crowvec = [1,2,4]; % row vector
|
||||
m = [1,2,3;4,5,6]; % matrix with size 2x3
|
||||
1
Task/Variables/MATLAB/variables-2.m
Normal file
1
Task/Variables/MATLAB/variables-2.m
Normal file
|
|
@ -0,0 +1 @@
|
|||
global b
|
||||
15
Task/Variables/Modula-3/variables-1.mod3
Normal file
15
Task/Variables/Modula-3/variables-1.mod3
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
MODULE Foo EXPORTS Main;
|
||||
|
||||
IMPORT IO, Fmt;
|
||||
|
||||
VAR foo: INTEGER := 5; (* foo is global (to the module). *)
|
||||
|
||||
PROCEDURE Foo() =
|
||||
VAR bar: INTEGER := 10; (* bar is local to the procedure Foo. *)
|
||||
BEGIN
|
||||
IO.Put("foo + bar = " & Fmt.Int(foo + bar) & "\n");
|
||||
END Foo;
|
||||
|
||||
BEGIN
|
||||
Foo();
|
||||
END Foo.
|
||||
1
Task/Variables/Modula-3/variables-2.mod3
Normal file
1
Task/Variables/Modula-3/variables-2.mod3
Normal file
|
|
@ -0,0 +1 @@
|
|||
PROCEDURE Foo(n: INTEGER) =
|
||||
1
Task/Variables/Modula-3/variables-3.mod3
Normal file
1
Task/Variables/Modula-3/variables-3.mod3
Normal file
|
|
@ -0,0 +1 @@
|
|||
PROCEDURE Foo(VAR n: INTEGER) =
|
||||
1
Task/Variables/OCaml/variables-1.ocaml
Normal file
1
Task/Variables/OCaml/variables-1.ocaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
let x = 28
|
||||
1
Task/Variables/OCaml/variables-2.ocaml
Normal file
1
Task/Variables/OCaml/variables-2.ocaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
let y = ref 28
|
||||
2
Task/Variables/OCaml/variables-3.ocaml
Normal file
2
Task/Variables/OCaml/variables-3.ocaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
!y (* access *)
|
||||
y := 34 (* modification *)
|
||||
9
Task/Variables/OCaml/variables-4.ocaml
Normal file
9
Task/Variables/OCaml/variables-4.ocaml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
let sum = (* sum is bound to 181 *)
|
||||
let a = 31
|
||||
and b = 150 in
|
||||
(a + b)
|
||||
|
||||
let sum () = (* sum is a function which returns 181 *)
|
||||
let a = 31
|
||||
and b = 150 in
|
||||
(a + b)
|
||||
3
Task/Variables/Objeck/variables.objeck
Normal file
3
Task/Variables/Objeck/variables.objeck
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a : Int;
|
||||
b : Int := 13;
|
||||
c := 7;
|
||||
1
Task/Variables/Openscad/variables.scad
Normal file
1
Task/Variables/Openscad/variables.scad
Normal file
|
|
@ -0,0 +1 @@
|
|||
mynumber=5+4; // This gives a value of nine
|
||||
7
Task/Variables/Oz/variables-1.oz
Normal file
7
Task/Variables/Oz/variables-1.oz
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
declare
|
||||
Var %% new variable Var, initially free
|
||||
{Show Var}
|
||||
Var = 42 %% now Var has the value 42
|
||||
{Show Var}
|
||||
Var = 42 %% the same value is assigned again: ok
|
||||
Var = 43 %% a different value is assigned: exception
|
||||
10
Task/Variables/Oz/variables-10.oz
Normal file
10
Task/Variables/Oz/variables-10.oz
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
declare
|
||||
V = 42
|
||||
in
|
||||
{Wait V} %% explicitly wait for V to become determined
|
||||
|
||||
if {IsDet V} then %% check whether V is determined; not recommended
|
||||
{Show determined}
|
||||
elseif {IsFree V} then %% check whether V is free; not recommended
|
||||
{Show free}
|
||||
end
|
||||
7
Task/Variables/Oz/variables-11.oz
Normal file
7
Task/Variables/Oz/variables-11.oz
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
declare
|
||||
A = {NewCell 42}
|
||||
OldVal
|
||||
in
|
||||
{Show @A} %% read a cell with @
|
||||
A := 43 %% change its value
|
||||
OldVal = A := 44 %% read and write at the same time (atomically)
|
||||
6
Task/Variables/Oz/variables-2.oz
Normal file
6
Task/Variables/Oz/variables-2.oz
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
declare
|
||||
A = 3
|
||||
B
|
||||
in
|
||||
A = B
|
||||
{Show B}
|
||||
5
Task/Variables/Oz/variables-3.oz
Normal file
5
Task/Variables/Oz/variables-3.oz
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
declare
|
||||
A = 3
|
||||
A = B %% Error: variable B not introduced
|
||||
in
|
||||
{Show B}
|
||||
2
Task/Variables/Oz/variables-4.oz
Normal file
2
Task/Variables/Oz/variables-4.oz
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
declare
|
||||
[A B C D] = [1 2 3 4] %% unification of two lists
|
||||
9
Task/Variables/Oz/variables-5.oz
Normal file
9
Task/Variables/Oz/variables-5.oz
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
functor
|
||||
export Function
|
||||
define
|
||||
ToplevelVariable = 42
|
||||
|
||||
fun {Function}
|
||||
42
|
||||
end
|
||||
end
|
||||
15
Task/Variables/Oz/variables-6.oz
Normal file
15
Task/Variables/Oz/variables-6.oz
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
fun {Function Arg}
|
||||
LocalVar1
|
||||
in
|
||||
LocalVar1 = if Arg == 42 then
|
||||
LocalVar2
|
||||
in
|
||||
LocalVar2 = yes
|
||||
LocalVar2
|
||||
else
|
||||
LocalVar3 = no %% variables can be initialized when declared
|
||||
in
|
||||
LocalVar3
|
||||
end
|
||||
LocalVar1
|
||||
end
|
||||
8
Task/Variables/Oz/variables-7.oz
Normal file
8
Task/Variables/Oz/variables-7.oz
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
if {IsEven 42} then
|
||||
{System.showInfo "Here, LocalVar is not visible."}
|
||||
local
|
||||
LocalVar = "Here, LocalVar IS visible"
|
||||
in
|
||||
{System.showInfo LocalVar}
|
||||
end
|
||||
end
|
||||
1
Task/Variables/Oz/variables-8.oz
Normal file
1
Task/Variables/Oz/variables-8.oz
Normal file
|
|
@ -0,0 +1 @@
|
|||
case "Rosetta code" of First|_ then {Show First} end %% prints "R"
|
||||
8
Task/Variables/Oz/variables-9.oz
Normal file
8
Task/Variables/Oz/variables-9.oz
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
declare
|
||||
A
|
||||
B = !!A %% B is a read-only view of A
|
||||
in
|
||||
thread
|
||||
B = 43 %% this blocks until A is known; then it fails because 43 \= 42
|
||||
end
|
||||
A = 42
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue