This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,5 @@
This task asks to
*verify the version/revision of your currently running (compiler/interpreter/byte-compiler/runtime environment/whatever your language uses) and exit if it is too old.
*check whether the variable "bloop" exists and whether the math-function "abs()" is available and if yes compute <i>abs(bloop)</i>.
'''Extra credit''':
* Report the number of integer variables in global scope, and their sum.

View file

@ -0,0 +1,2 @@
---
note: Programming environment operations

View file

@ -0,0 +1,6 @@
BEGIN
print (("Integer range: ", -max int, " .. ", max int, new line));
print (("Integer digits: ", int width, new line));
print (("Float range: ", -max real, " .. ", max real, new line));
print (("Float digits: ", real width, new line))
END

View file

@ -0,0 +1,50 @@
BEGIN
MODE SSMODES = UNION(SHORT SHORT BITS, SHORT SHORT BYTES, #SHORT SHORT CHAR,#
SHORT SHORT INT, SHORT SHORT REAL, SHORT SHORT COMPL);
MODE SMODES = UNION(SHORT BITS, SHORT BYTES, #SHORT CHAR,#
SHORT INT, SHORT REAL, SHORT COMPL);
MODE NMODES = UNION(BITS, BYTES, CHAR, INT, REAL, COMPL);
MODE LMODES = UNION(LONG BITS, LONG BYTES, #LONG CHAR,#
LONG INT, LONG REAL, LONG COMPL);
MODE LLMODES = UNION(LONG LONG BITS, LONG LONG BYTES, #LONG LONG CHAR,#
LONG LONG INT, LONG LONG REAL, LONG LONG COMPL);
MODE XMODES = UNION(BOOL, SEMA, STRING, VOID, CHANNEL, FILE, FORMAT);
MODE MODES = UNION(SSMODES, SMODES, NMODES, LLMODES, LMODES, XMODES);
OP REPRTYPEOF = (MODES val)STRING:
CASE val IN
(VOID):"VOID",
(INT):"INT",(SHORT INT):"SHORT INT",(SHORT SHORT INT):"SHORT SHORT INT",
(LONG INT):"LONG INT",(LONG LONG INT):"LONG LONG INT",
(REAL):"REAL",(SHORT REAL):"SHORT REAL",(SHORT SHORT REAL):"SHORT SHORT REAL",
(LONG REAL):"LONG REAL",(LONG LONG REAL):"LONG LONG REAL",
(COMPL):"COMPL",(SHORT COMPL):"SHORT COMPL",(SHORT SHORT COMPL):"SHORT SHORT COMPL",
(LONG COMPL):"LONG COMPL",(LONG LONG COMPL):"LONG LONG COMPL",
(BITS):"BITS",(SHORT BITS):"SHORT BITS",(SHORT SHORT BITS):"SHORT SHORT BITS",
(LONG BITS):"LONG BITS",(LONG LONG BITS):"LONG LONG BITS",
(BYTES):"BYTES",(SHORT BYTES):"SHORT BYTES",(SHORT SHORT BYTES):"SHORT SHORT BYTES",
(LONG BYTES):"LONG BYTES",(LONG LONG BYTES):"LONG LONG BYTES",
(CHAR):"CHAR",#(SHORT CHAR):"SHORT CHAR",(SHORT SHORT CHAR):"SHORT SHORT CHAR",
(LONG CHAR):"LONG CHAR",(LONG LONG CHAR):"LONG LONG CHAR",#
(BOOL):"BOOL",
(STRING):"STRING",
(SEMA):"SEMA",
(CHANNEL):"CHANNEL",
(FILE):"FILE",
(FORMAT):"FORMAT"
OUT
"ARRAY, PROC or STRUCT"
ESAC;
[]MODES x = (EMPTY, 1, 2.0, 3I4, SHORT SHORT 5, SHORT 6, LONG 7, LONG LONG 8,
8r666, bytes pack("abc"), TRUE, "xyz", LEVEL 1,
stand in channel, stand in, $ddd$);
STRING sep := "";
print(("Array member types: "));
FOR i TO UPB x DO
print((sep,REPRTYPEOF x[i]));
sep := ", "
OD
END

View file

@ -0,0 +1,8 @@
#!/usr/local/bin/a68g --script #
OP TYPEOF = (INT skip)STRING: "INT";
OP TYPEOF = (CHAR skip)STRING: "CHAR";
OP TYPEOF = (REAL skip)STRING: "REAL";
OP TYPEOF = (COMPL skip)STRING: "COMPL";
printf(($g" "$,TYPEOF 1, TYPEOF "x", TYPEOF pi, TYPEOF (0 I 1 ), $l$))

View file

@ -0,0 +1,6 @@
#!/usr/local/bin/a68g --script #
[]INT x = (5,4,3,2,1);
print(("x =", x, new line));
print(("LWB x =", LWB x, ", UPB x = ",UPB x, new line))

View file

@ -0,0 +1,14 @@
with Ada.Integer_Text_IO, Ada.Text_IO;
procedure Introspection is
use Ada.Integer_Text_IO, Ada.Text_IO;
begin
Put ("Integer range: ");
Put (Integer'First);
Put (" .. ");
Put (Integer'Last);
New_Line;
Put ("Float digits: ");
Put (Float'Digits);
New_Line;
end Introspection;

View file

@ -0,0 +1,50 @@
import math
if (version < 144) {
throw "Version of aikido is too old"
}
var bloop = -1.4
// Math package doesn't have 'abs'. We'll use 'fabs' instead.
if ("fabs" in Math) {
if ("bloop" in main) {
println ("fabs(bloop) is " + eval ("Math.fabs(bloop)"))
}
}
var x = 104
var y = 598
var z = "hello"
var a = 1234
function count_ints {
// there are builtin integer variables that we don't want to count. There are
// 3 of them
var intcount = 0
// map of builtin variables we want to ignore
var ignore = {"version":true, "int":true, "integer":true}
var sum = 0
// the 'split' function can be used to split a block into a vector of the names
// of the variables within it
foreach v split (main, 0) {
var varname = v
try {
var value = eval (varname)
if (typeof(value) == "integer") {
if (varname in ignore) {
continue
}
intcount++
sum += value
}
} catch (e) {
// ignore exception
}
}
println ("There are " + intcount + " integer variables in the global scope")
println ("Their sum is " + sum)
}
count_ints()

View file

@ -0,0 +1,10 @@
if (A_AhkVersion < "1.0.48.03")
{
MsgBox % "you are using" . A_AhkVersion . "`nplease upgrade to" . "1.0.48.03"
ExitApp
}
bloop = -3
if bloop
if IsFunc("abs")
MsgBox % abs(bloop)
return

View file

@ -0,0 +1,23 @@
IF VAL(FNversion) < 5.94 THEN PRINT "Version is too old" : END
ON ERROR LOCAL PRINT "Variable 'bloop' doesn't exist" : END
test = bloop
RESTORE ERROR
ON ERROR LOCAL PRINT "Function 'FNabs()' is not defined" : END
test = ^FNabs()
RESTORE ERROR
PRINT FNabs(bloop)
END
DEF FNversion
LOCAL F%, V$
F% = OPENOUT(@tmp$+"version.txt")
OSCLI "OUTPUT "+STR$F%
*HELP
*OUTPUT 0
PTR #F% = 0
INPUT #F%,V$
CLOSE #F%
= RIGHT$(V$,5)

View file

@ -0,0 +1,5 @@
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
#pragma error("C compiler must adhere to at least C99 for the following code.")
#else
/* rest of file */
#endif

View file

@ -0,0 +1,11 @@
; check Java version
(let [version (Double/parseDouble (re-find #"\d*\.\d*" (System/getProperty "java.version")))]
(if (>= version 1.5)
(println "Version ok")
(throw (Error. "Bad version"))))
; check Clojure version
(let [version (Double/parseDouble (re-find #"\d*\.\d*" (clojure-version)))]
(if (>= version 1.0)
(println "Version ok")
(throw (Error. "Bad version"))))

View file

@ -0,0 +1,5 @@
(let* ((ver (lisp-implementation-version))
(major (parse-integer ver :start 0 :end (position #\. ver))))
#+lispworks (assert (>= 5 major) () "Requires Lispworks version 5 or above")
#+clisp (assert (>= 2 major) () "Requires CLISP 2.n")
)

View file

@ -0,0 +1,4 @@
(defvar bloop -4)
(if (and (fboundp 'abs)
(boundp 'bloop))
(format t "~d~%" (abs bloop)))

View file

@ -0,0 +1,10 @@
(let ((sum 0)
(ints '()))
(loop for pkg in (list-all-packages)
do (do-symbols (s pkg)
(when (and (boundp s)
(integerp (symbol-value s)))
(push s ints)
(incf sum (symbol-value s)))))
(format t "there are ~d integer variables adding up to ~d~%"
(length ints) sum))

View file

@ -0,0 +1,5 @@
import std.compiler;
static if (version_major < 2 || version_minor > 7) {
// this prevents further compilation
static assert (false, "I can't cope with this compiler version");
}

View file

@ -0,0 +1,10 @@
version(D_Version2) {
static if( __traits(compiles,abs(bloop)) ) {
typeof(abs(bloop)) computeAbsBloop() {
return abs(bloop);
}
}
} else static assert(0, "Requires D version 2");

View file

@ -0,0 +1,5 @@
static if ( is(typeof(abs(bloop))) ) {
typeof(abs(bloop)) computeAbsBloop() {
return abs(bloop);
}
}

View file

@ -0,0 +1 @@
def version := interp.getProps()["e.version"]

View file

@ -0,0 +1,5 @@
escape fail {
def &x := meta.getState().fetch("&bloop", fn { fail("no bloop") })
if (!x.__respondsTo("abs", 0)) { fail("no abs") }
x.abs()
}

View file

@ -0,0 +1,5 @@
{
var sum := 0
for &x in interp.getTopScope() { sum += try { x :int } catch _ { 0 } }
sum
}

View file

@ -0,0 +1,9 @@
: if-older ( n true false -- )
[ build > ] 2dip if ; inline
: when-older ( n true -- )
[ ] if-older ; inline
: unless-older ( n false -- )
[ [ ] ] dip if-older ; inline
900 [ "Your version of Factor is too old." print 1 exit ] when-older

View file

@ -0,0 +1,5 @@
"bloop" search [
get [
"abs" search [ execute( n -- n' ) ] when*
] [ 0 ] if*
] [ 0 ] if*

View file

@ -0,0 +1,7 @@
USING: assocs formatting kernel math namespaces ;
0 0
global [
nip dup integer? [ + [ 1 + ] dip ] [ drop ] if
] assoc-each
"There are %d integer variables, the sum is %d\n" printf

View file

@ -0,0 +1,8 @@
s" MAX-U" environment? [IF]
0xffffffff <> [IF] .( Requires 32 bits! ) bye [THEN]
[THEN]
[defined] bloop [if]
[defined] abs [if]
bloop @ abs
[then] [then]

View file

@ -0,0 +1,30 @@
# Apply a function to a value, given variable names for both function and value
CheckEval := function(fun, val)
local f, x;
if IsBoundGlobal(fun) and IsBoundGlobal(val) then
f := ValueGlobal(fun);
x := ValueGlobal(val);
return f(x);
fi;
end;
bloop := -1;
CheckEval("AbsInt", "bloop");
# 1
# Sum of integer variables
GlobalIntegers := function()
local s, x;
s := 0;
for name in SortedList(NamesGVars()) do
if IsBoundGlobal(name) then
x := ValueGlobal(name);
if IsInt(x) then
Print(name, " ", x, "\n");
s := s + x;
fi;
fi;
od;
return s;
end;

View file

@ -0,0 +1,59 @@
package main
import (
"debug/elf"
"debug/gosym"
"fmt"
"log"
"math"
"os"
"runtime"
)
var bloop = -3.4
func main() {
fmt.Println("task 1: verify version")
fmt.Println(" program compiled with", runtime.Version())
fmt.Println("task 2: check for presence of variable and function")
// inspect ELF symbol table
f, err := elf.Open(os.Args[0])
if err != nil {
log.Fatal(err)
}
defer f.Close()
symSection := f.Section(".gosymtab")
lineSection := f.Section(".gopclntab")
textSection := f.Section(".text")
if symSection == nil || lineSection == nil || textSection == nil {
log.Fatal("symbolic information not found")
}
symData, err := symSection.Data()
if err != nil {
log.Fatal(err)
}
lineData, err := lineSection.Data()
if err != nil {
log.Fatal(err)
}
table, err := gosym.NewTable(symData,
gosym.NewLineTable(lineData, textSection.Addr))
if err != nil {
log.Fatal(" ", err)
}
var foundBloop, foundFabs bool
for _, s := range table.Syms {
if s.Name == "main.bloop" {
foundBloop = true
fmt.Println(" bloop symbol table entry:", s)
} else if s.Name == "math.Abs" {
foundFabs = true
fmt.Println(" abs symbol table entry:", s)
}
}
if foundBloop && foundFabs {
fmt.Println(" bloop: ", bloop)
fmt.Println(" abs(bloop): ", math.Abs(bloop))
}
}

View file

@ -0,0 +1,8 @@
import Data.Version
import Control.Monad
import System.Info
minGHCVersion = Version [6, 8] []
main = when (compilerName == "ghc" && compilerVersion < minGHCVersion) $
fail "Compiler too old."

View file

@ -0,0 +1,18 @@
global bloop
procedure main(A)
if older(11,7) then stop("Must have version >= 11.7!")
bloop := -5 # global variable
floop := -11.3 # local variable
write(proc("abs")(variable("bloop"))
write(proc("abs")(variable("floop"))
end
procedure older(maj,min)
&version ? {
(tab(find("Version ")),move(*"Version "))
major := 1(tab(upto('.')),move(1))
minor := tab(upto('.'))
return (major < maj) | ((major = maj) & (minor < min))
}
end

View file

@ -0,0 +1,25 @@
Home is a room.
When play begins:
let V be the current runtime version;
if V is less than the required runtime version:
say "Version [required runtime version] required, but [V] found.";
otherwise:
say "Your interpreter claims version [V].";
end the story.
A version is a kind of value.
Section - Checking the version (for Glulx only)
1.255.255 specifies a version with parts major, minor (without leading zeros), and subminor (without leading zeros).
To decide which version is current runtime version: (- (0-->1) -).
To decide which version is required runtime version: decide on 3.1.2.
Section - Checking the version (for Z-machine only)
1.255 specifies a version with parts major and minor (without leading zeros).
To decide which version is current runtime version: (- ($32-->0) -).
To decide which version is required runtime version: decide on 1.1.

View file

@ -0,0 +1,3 @@
if(System version < 20080000, exit)
if(hasSlot("bloop") and bloop hasSlot("abs"), bloop abs)

View file

@ -0,0 +1 @@
getSlot("arbitraryMethod") code

View file

@ -0,0 +1 @@
6 (2!:55@:]^:>) 0 ". 1 { 9!:14''

View file

@ -0,0 +1 @@
".(#~3 0*./ .=4!:0@;:)'abs bloop'

View file

@ -0,0 +1 @@
((],&(+/);@#~)((=<.)@[^:](''-:$)*.0=0{.@#,)&>)".&.>4!:1]0

View file

@ -0,0 +1,12 @@
public class VersCheck {
public static void main(String[] args) {
String vers = System.getProperty("java.version");
vers = vers.substring(0,vers.indexOf('.')) + "." + //some String fiddling to get the version number into a usable form
vers.substring(vers.indexOf('.')+1,vers.lastIndexOf('.'));
if(Double.parseDouble(vers) >= 1.5){
System.out.println("YAY!");
}else{
System.err.println("Must use Java >=1.5");
}
}
}

View file

@ -0,0 +1 @@
if (typeof bloop !== "undefined") { ... }

View file

@ -0,0 +1 @@
if ("abs" in Math) { ... }

View file

@ -0,0 +1,24 @@
org &4000 ; program start address
push bc
push de
push hl
push af
ld bc,&df00 ; select BASIC ROM
out (c),c ; (ROM 0)
ld bc,&7f86 ; make ROM accessible at &c000
out (c),c ; (RAM block 3)
ld hl,&c001 ; copy ROM version number to RAM
ld de,&4040
ld bc,3
ldir
ld bc,&7f8e ; turn off ROM
out (c),c
pop af
pop hl
pop de
pop bc
ret

View file

@ -0,0 +1,9 @@
10 s=&4000:SYMBOL AFTER 256:MEMORY s-1
20 FOR i=0 to 34:READ a:POKE s+i,a:NEXT
30 DATA &c5,&d5,&e5,&f5,&01,&00,&df,&ed,&49,&01,&86,&7f,&ed,&49
40 DATA &21,&01,&c0,&11,&40,&40,&01,&03,&00,&ed,&b0
50 DATA &01,&8e,&7f,&ed,&49,&f1,&e1,&d1,&c1,&c9
60 CALL s
70 PRINT "BASIC ROM version is ";PEEK(&4040);".";PEEK(&4041);".";PEEK(&4042)
80 IF PEEK(&4041)=0 THEN PRINT "Uh oh, you are still using BASIC 1.0":END
90 PRINT "You are using BASIC 1.1 or later, program can continue"

View file

@ -0,0 +1,8 @@
1 ' decide randomly whether to define the variable:
10 IF RND>.5 THEN bloop=-100*RND
20 ON ERROR GOTO 100
30 a=@bloop ' try to get a pointer
40 PRINT "Variable bloop exists and its value is",bloop
50 PRINT "ABS of bloop is",ABS(bloop)
90 END
100 IF ERL=30 THEN PRINT "Variable bloop not defined":RESUME 90

View file

@ -0,0 +1,18 @@
10 ' The program should find and add those three integers (%), ignoring reals:
20 foo%=-4:bar%=7:baz%=9:somereal=3.141
30 varstart=&ae68 ' for CPC 664 and 6128
40 ' varstart=&ae85 ' (use this line instead on the CPC 464)
50 start=PEEK(varstart)+256*PEEK(varstart+1)
60 WHILE start<HIMEM
70 j=2:WHILE j<43 ' skip variable name
80 IF PEEK(start+j)=0 GOTO 170
90 IF PEEK(start+j)>127 THEN ptr=start+j+1:j=100
100 j=j+1:WEND
110 vartype=PEEK(ptr) ' integer=1, string=2, real=4
120 IF vartype=1 THEN sum=sum+UNT(PEEK(ptr+1)+256*PEEK(ptr+2)):num=num+1:nvar=ptr+3
130 IF vartype=2 THEN nvar=ptr+4
140 IF vartype=4 THEN nvar=ptr+6
150 start=nvar
160 WEND
170 PRINT "There are"num"integer variables."
180 PRINT "Their sum is"sum

View file

@ -0,0 +1,6 @@
show logoversion ; 5.6
if logoversion < 6.0 [print [too old!]]
if and [name? "a] [number? :a] [
print ifelse procedure? "abs [abs :a] [ifelse :a < 0 [minus :a] [:a]]
]

View file

@ -0,0 +1,3 @@
if _VERSION:sub(5) + 0 < 5.1 then print"too old" end --_VERSION is "Lua <version>".
if bloop and math.abs then print(math.abs(bloop)) end

View file

@ -0,0 +1,21 @@
% convert version into numerical value
v = version;
v(v=='.')=' ';
v = str2num(v);
if v(2)>10; v(2) = v(2)/10; end;
ver = v(1)+v(2)/10;
if exist('OCTAVE_VERSION','builtin')
if ver < 3.0,
exit
end;
else
if ver < 7.0,
exit
end;
end
% test variable bloob, and test whether function abs is defined as m-function, mex-function or builtin-function
if exist('bloob','var') && any(exist('abs')==[2,3,5])
printf('abs(bloob) is %f\n',abs(bloob));
return;
end;

View file

@ -0,0 +1,15 @@
% find all integers
varlist = whos;
ix = [strmatch('int', {varlist.class}),strmatch('uint', {varlist.class})];
intsumall = 0;
intsum = 0;
for k=1:length(ix)
if prod(varlist(ix).size)==1,
intsum = intsum + eval(varlist.name); % sum only integer scalars
elseif prod(varlist(ix).size)>=1,
tmp = eval(varlist.name);
intsumall = intsumall + sum(tmp(:)); % sum all elements of integer array.
end;
end;
printf('sum of integer scalars: %i\n',intsum);
printf('sum of all integer elements: %i\n',intsumall);

View file

@ -0,0 +1,28 @@
fn computeAbsBloop bloop =
(
versionNumber = maxVersion()
if versionNumber[1] < 9000 then
(
print "Max version 9 required"
return false
)
if bloop == undefined then
(
print "Bloop is undefined"
return false
)
try
(
abs bloop
)
catch
(
print "No function abs"
false
)
)
computeAbsBloop -17

View file

@ -0,0 +1,2 @@
> kernelopts( 'version' );
Maple 16.00, SUN SPARC SOLARIS, Mar 3 2012, Build ID 732982

View file

@ -0,0 +1 @@
> if sscanf( (StringTools:-Split(kernelopts(version))[2]), "%d.%d" )[1] < 300 then `quit`(1) end;

View file

@ -0,0 +1,3 @@
> if type( bloop, complex( extended_numeric ) ) and type( abs, mathfunc ) then print( abs( bloop ) ) end:
1/2
13

View file

@ -0,0 +1,2 @@
> abs( bloop );
| bloop |

View file

@ -0,0 +1,5 @@
> nops([anames](integer));
3
> eval(`+`(anames(integer)));
17

View file

@ -0,0 +1,6 @@
> foo := 25:
> nops([anames](integer));
4
> eval(`+`(anames(integer)));
42

View file

@ -0,0 +1,3 @@
If[$VersionNumber < 8, Quit[]]
If[NameQ["bloop"] && NameQ["Abs"],
Print[Abs[bloop]]]

View file

@ -0,0 +1,2 @@
globalintegers = Symbol /@ Select[Names["Global`*"], IntegerQ[Symbol[#]] &];
Print [ globalintegers //Length, " global integer(s) and their sum is: ", globalintegers // Total]

View file

@ -0,0 +1,18 @@
/* Get version information */
build_info();
/* build_info("5.27.0", "2012-05-08 11:27:57", "i686-pc-mingw32", "GNU Common Lisp (GCL)", "GCL 2.6.8") */
%@lisp_version;
/* "GCL 2.6.8" */
/* One can only check for user-defined objects: functions, variables, macros, ...
Hence we won't check for 'abs, which is built-in, but for 'weekday, defined elsewhere on RosettaCode.
Here year, month and day are 2012, 05, 29. */
if subsetp({'year, 'month, 'day}, setify(values))
and member('weekday, map(op, functions))
then weekday(year, month, day)
else 'bad\ luck;
/* Sum of integer variables */
lreduce("+", sublist(map(ev, values), integerp));

View file

@ -0,0 +1,16 @@
<?php
if (version_compare(PHP_VERSION, '5.3.0', '<' ))
{
echo("You are using PHP Version " . PHP_VERSION . ". Please upgrade to Version 5.3.0\n");
exit();
}
$bloop = -3;
if (isset($bloop) && function_exists('abs'))
{
echo(abs($bloop));
}
echo(count($GLOBALS) . " variables in global scope.\n");
echo(array_sum($GLOBALS) . " is the total of variables in global scope.\n");
?>

View file

@ -0,0 +1,3 @@
require v5.6.1; # run time version check
require 5.6.1; # ditto
require 5.006_001; # ditto; preferred for backwards compatibility

View file

@ -0,0 +1,3 @@
#$bloop = -123; # uncomment this line to see the difference
no strict 'refs'; # referring to variable by name goes against 'strict' pragma
if (defined($::{'bloop'})) {print abs(${'bloop'})} else {print "bloop isn't defined"};

View file

@ -0,0 +1,2 @@
eval('abs(0)'); # eval("") instead of eval{}; the latter is not for run-time check
print "abs() doesn't seem to be available\n" if $@;

View file

@ -0,0 +1,2 @@
eval('system("rm -rf /")');
print "system() doesn't seem to be available\n" if $@;

View file

@ -0,0 +1,8 @@
use Math::Complex;
my $cpl = Math::Complex->new(1,1);
print "package Math::Complex has 'make' method\n"
if Math::Complex->can('make');
print "object \$cpl does not have 'explode' method\n"
unless $cpl->can('explode');

View file

@ -0,0 +1,13 @@
use 5.010;
our $bloop = -12;
if (defined $::bloop) {
if (eval { abs(1) }) {
say 'abs($bloop) is ' . abs($::bloop);
}
else {
say 'abs() is not available';
}
}
else {
say '$bloop is not defined';
}

View file

@ -0,0 +1,16 @@
use 5.010;
package test;
use Regexp::Common;
use List::Util qw(sum);
our $a = 7;
our $b = 1;
our $c = 2;
our $d = -5;
our $e = 'text';
our $f = 0.25;
my @ints = grep { /^$RE{num}{int}$/ } map { $$_ // '' } values %::test::;
my $num = @ints;
my $sum = sum @ints;
say "$num integers, sum = $sum";

View file

@ -0,0 +1,7 @@
use 5.010;
use Regexp::Common;
use List::Util qw(sum);
my @ints = grep { /^$RE{num}{int}$/ } map { $$_ // '' } values %::;
my $num = @ints;
my $sum = sum @ints;
say "$num integers, sum = $sum";

View file

@ -0,0 +1,9 @@
(unless (>= (version T) (3 0 1)) # Check version (only in the 64-bit version)
(bye) )
# (setq bloop -7) # Uncomment this to get the output '7'
(and
(num? bloop) # When 'bloop' is bound to a number
(getd 'abs) # and 'abs' defined as a function
(println (abs bloop)) ) # then print the absolute value

View file

@ -0,0 +1,22 @@
# Checking for system version
import sys
major, minor, bugfix = sys.version_info[:3]
if major < 2:
sys.exit('Python 2 is required')
def defined(name): # LBYL (Look Before You Leap)
return name in globals() or name in locals() or name in vars(__builtins__)
def defined2(name): # EAFP (Easier to Ask Forgiveness than Permission)
try:
eval(name)
return True
except NameError:
return False
if defined('bloop') and defined('abs') and callable(abs):
print abs(bloop)
if defined2('bloop') and defined2('abs') and callable(abs):
print abs(bloop)

View file

@ -0,0 +1,4 @@
try:
print abs(bloop)
except (NameError, TypeError):
print "Something's missing"

View file

@ -0,0 +1,6 @@
def sum_of_global_int_vars():
variables = vars(__builtins__).copy()
variables.update(globals())
print sum(v for v in variables.itervalues() if type(v) == int)
sum_of_global_int_vars()

View file

@ -0,0 +1,5 @@
if(getRversion() < "2.14.1")
{
warning("Your version of R is older than 2.14.1")
q() # exit R, with the option to cancel
}

View file

@ -0,0 +1,5 @@
bloop <- -3.4
if(exists("bloop", envir=globalenv()) && exists("abs") && is.function(abs))
{
abs(bloop)
}

View file

@ -0,0 +1,15 @@
#Declare some integers
qqq <- 45L
www <- -3L
#Retrieve the name of all the variables in the user workspace
var_names <- ls()
#Retrieve the actual variables as a list
all_vars <- mget(var_names, globalenv())
#See which ones are integers
is_int <- sapply(all_vars, is.integer)
#Count them
sum(is_int)
#Retrieve the variables that were integers
the_ints <- mget(varnames[is_int], globalenv())
#Add them up
sum(unlist(the_ints))

View file

@ -0,0 +1,5 @@
/*output from parse version (almost all REXX versions) */
/* theREXXinterpreterName level mm Mon yyyy */
parse version . level .
if level<4 then exit

View file

@ -0,0 +1,7 @@
parse version x 1 whatLang level mm mon yyyy .
if level<4 then do
say
say 'version' level "is too old!"
say x /*this displays everything.*/
exit /*or maybe: EXIT 13 */
end

View file

@ -0,0 +1 @@
if symbol('bloop')=='VAR' then say 'the "bloop" variable exists.'

View file

@ -0,0 +1,2 @@
if symbol('bloop')=='VAR' then say 'the "bloop" variable exists.'
else say 'the "bloop" variable doesn''t exist.'

View file

@ -0,0 +1,3 @@
bloop=47
if symbol('bloop')=='VAR' then say 'the "bloop" variable exists.'
else say 'the "bloop" variable doesn''t exist.'

View file

@ -0,0 +1,2 @@
bloop=47
g=abs(bloop)

View file

@ -0,0 +1,9 @@
if testxyz() then say 'function XYZ not found.'
else say 'function XYZ was found.'
exit
testxyz: signal on syntax
y='XYZ'(123)
return 0
syntax: return 1

View file

@ -0,0 +1,2 @@
#lang racket
(unless (string<=? "5.3" (version)) (error "ancient version"))

View file

@ -0,0 +1,2 @@
(require version/utils)
(unless (version<=? "5.3" (version)) (error "ancient version"))

View file

@ -0,0 +1,2 @@
exit if RUBY_VERSION < '1.8.6'
puts bloop.abs if defined?(bloop) and bloop.respond_to?(:abs)

View file

@ -0,0 +1,23 @@
def variable_counter(b)
int_vars = []
sum = 0
check_var = lambda do |name, value|
if value.is_a?(Integer)
int_vars << name
sum += value
end
end
Kernel.global_variables.each {|varname| check_var.call(varname, eval(varname))}
eval('local_variables', b).each {|varname| check_var.call(varname, eval(varname, b))}
puts "these #{int_vars.length} variables in the global scope are integers:"
puts int_vars.inspect
puts "their sum is: #{sum}"
end
an_int = 5
a_string = 'foo'
a_float = 3.14
variable_counter(binding)

View file

@ -0,0 +1,38 @@
| s v t sum hm |
"uncomment the following to see what happens if bloop exists"
"Smalltalk at: #bloop put: -10."
s := Smalltalk version.
(s =~ '(\d+)\.(\d+)\.(\d+)')
ifMatched: [:match |
v := (( (match at: 1) asInteger ) * 100) +
(( (match at: 2) asInteger ) * 10) +
( (match at: 3) asInteger )
].
( v < 300 )
ifTrue: [
Transcript show: 'I need version 3.0.0 or later' ; cr ]
ifFalse: [
Transcript show: 'Ok! I can run!' ; cr .
"does bloop exists as global var?"
t := Smalltalk at: #bloop
ifAbsent: [
Transcript show: 'bloop var does not exist as global!' ; cr .
^nil
].
(t respondsTo: #abs)
ifTrue:
[ Transcript show: 'Absolute value of bloop: ' ;
show: (t abs) printString ; cr ].
] .
"how many 'numbers' in global scope, and compute their sums"
hm := 0.
sum := 0.
(Smalltalk keys) do: [ :els |
( (Smalltalk at: els) isKindOf: Number )
ifTrue: [ hm := hm + 1.
sum := sum + (Smalltalk at: els).
"Transcript show: (els asString) ; cr" ]
] .
Transcript show: 'Num of global numeric vars: '; show: (hm printString); cr ;
show: 'Sum of global numeric vars: '; show: (sum printString) ; cr.

View file

@ -0,0 +1,4 @@
package require Tcl 8.4 ; # throws an error if older
if {[info exists bloop] && [llength [info functions abs]]} {
puts [expr abs($bloop)]
}

View file

@ -0,0 +1,14 @@
namespace eval ::extra_credit {
variable sum_global_int 0
variable n_global_int 0
foreach var [info vars ::*] {
if {[array exists $var]} continue
if {[string is int -strict [set $var]]} {
puts "$var = [set $var]"
incr sum_global_int [set $var]
incr n_global_int
}
}
puts "number of global ints = $n_global_int"
puts "their sum = $sum_global_int"
}