tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,5 @@
'''Null''' (or '''nil''') is the computer science concept of an undefined or unbound object. Some languages have an explicit way to access the null object, and some don't. Some languages distinguish the null object from [[undefined values]], and some don't.
Show how to access null in your language by checking to see if an object is equivalent to the null object.
''This task is not about whether a variable is defined. The task is about "null"-like values in various languages, which may or may not be related to the defined-ness of variables in your language.''

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1,19 @@
REF STRING no result = NIL;
STRING result := "";
IF no result :=: NIL THEN print(("no result :=: NIL", new line)) FI;
IF result :/=: NIL THEN print(("result :/=: NIL", new line)) FI;
IF no result IS NIL THEN print(("no result IS NIL", new line)) FI;
IF result ISNT NIL THEN print(("result ISNT NIL", new line)) FI;
COMMENT using the UNESCO/IFIP/WG2.1 ALGOL 68 character set
result := °;
IF REF STRING(result) :≠: ° THEN print(("result ≠ °", new line)) FI;
END COMMENT
# Note the following gotcha: #
REF STRING var := NIL;
IF var ISNT NIL THEN print(("The address of var ISNT NIL",new line)) FI;
IF var IS REF STRING(NIL) THEN print(("The address of var IS REF STRING(NIL)",new line)) FI

View file

@ -0,0 +1,2 @@
if (object == null)
trace("object is null");

View file

@ -0,0 +1,5 @@
with Ada.Text_Io;
if Object = null then
Ada.Text_Io.Put_line("object is null");
end if;

View file

@ -0,0 +1,5 @@
DEF x : PTR TO object
-> ...
IF object <> NIL
-> ...
ENDIF

View file

@ -0,0 +1,7 @@
if x is missing value then
display dialog "x is missing value"
end if
if x is null then
display dialog "x is null"
end if

View file

@ -0,0 +1,2 @@
If (object == null)
MsgBox, object is null

View file

@ -0,0 +1,13 @@
PROCtestobjects
END
DEF PROCtestobjects
PRIVATE a(), b(), s{}, t{}
DIM a(123)
DIM s{a%, b#, c$}
IF !^a() <= 1 PRINT "a() is null" ELSE PRINT "a() is not null"
IF !^b() <= 1 PRINT "b() is null" ELSE PRINT "b() is not null"
IF !^s{} <= 1 PRINT "s{} is null" ELSE PRINT "s{} is not null"
IF !^t{} <= 1 PRINT "t{} is null" ELSE PRINT "t{} is not null"
ENDPROC

View file

@ -0,0 +1 @@
{ nil { nil? } { "Whew!\n" } { "Something is terribly wrong!\n" } ifte << }

View file

@ -0,0 +1,5 @@
#include <iostream>
#include <cstdlib>
if (object == 0) {
std::cout << "object is null";
}

View file

@ -0,0 +1,12 @@
#include <boost/optional.hpp>
#include <iostream>
boost::optional<int> maybeInt()
int main()
{
boost::optional<int> maybe = maybeInt();
if(!maybe)
std::cout << "object is null\n";
}

View file

@ -0,0 +1,11 @@
#include <stdio.h>
int main()
{
char *object = 0;
if (object == NULL) {
puts("object is null");
}
return 0;
}

View file

@ -0,0 +1,2 @@
(let [x nil]
(println "Object is" (if (nil? x) "nil" "not nil")))

View file

@ -0,0 +1 @@
(find (ns-interns *ns*) 'foo)

View file

@ -0,0 +1 @@
(ns-unmap *ns* 'foo)

View file

@ -0,0 +1 @@
(if (condition) (do-this))

View file

@ -0,0 +1,5 @@
(defmethod car* ((arg cons))
(car arg))
(defmethod car* ((arg null))
nil)

View file

@ -0,0 +1,2 @@
(defmethod car* ((arg t)) ;; can just be written (defmethod car* (arg) ...)
(error "CAR*: ~s is neither a cons nor nil" arg))

View file

@ -0,0 +1,17 @@
MODULE ObjectNil;
IMPORT StdLog;
TYPE
Object = POINTER TO ObjectDesc;
ObjectDesc = RECORD
END;
VAR
x: Object; (* default initialization to NIL *)
PROCEDURE DoIt*;
BEGIN
IF x = NIL THEN
StdLog.String("x is NIL");StdLog.Ln
END
END DoIt;
END ObjectNil.

View file

@ -0,0 +1,12 @@
import std.stdio;
class K {}
void main() {
K k;
if (k is null)
writeln("k is null");
k = new K;
if (k !is null)
writeln("Now k is not null");
}

View file

@ -0,0 +1,6 @@
// the following are equivalent
if lObject = nil then
...
if not Assigned(lObject) then
...

View file

@ -0,0 +1 @@
object == null

View file

@ -0,0 +1 @@
: is-f? ( obj -- ? ) f = ;

View file

@ -0,0 +1,7 @@
fansh> x := null
fansh> x == null
true
fansh> x = 1
1
fansh> x == null
false

View file

@ -0,0 +1,21 @@
package main
import "fmt"
var (
s []int // slice type
p *int // pointer type
f func() // function type
i interface{} // interface type
m map[int]int // map type
c chan int // channel type
)
func main() {
fmt.Println(s == nil)
fmt.Println(p == nil)
fmt.Println(f == nil)
fmt.Println(i == nil)
fmt.Println(m == nil)
fmt.Println(c == nil)
}

View file

@ -0,0 +1,3 @@
undefined -- undefined value provided by the standard library
error "oops" -- another undefined value
head [] -- undefined, you can't take the head of an empty list

View file

@ -0,0 +1 @@
data Maybe a = Nothing | Just a

View file

@ -0,0 +1,3 @@
case thing of
Nothing -> "It's Nothing. Or null, whatever."
Just v -> "It's not Nothing; it is " ++ show v ++ "."

View file

@ -0,0 +1,10 @@
procedure main()
nulltest("a",a) # unassigned variables are null by default
nulltest("b",b := &null) # explicit assignment is possible
nulltest("c",c := "anything")
nulltest("c",c := &null) # varibables can't be undefined
end
procedure nulltest(name,var)
return write(name, if /var then " is" else " is not"," null.")
end

View file

@ -0,0 +1 @@
if(object == nil, "object is nil" println)

View file

@ -0,0 +1 @@
isUndefined=: _1 = nc@boxxopen

View file

@ -0,0 +1,5 @@
isUndefined 'foo'
1
foo=:9
isUndefined 'foo'
0

View file

@ -0,0 +1,10 @@
1 1 0 1#3 4 _ 5
3 4 5
I.1 1 0 1
0 1 3
0 1 3 { 3 4 _ 5
3 4 5
1 1 0 1 #inv 3 4 5
3 4 0 5
1 1 0 1 #!._ inv 3 4 5
3 4 _ 5

View file

@ -0,0 +1,4 @@
// here "object" is a reference
if (object == null) {
System.out.println("object is null");
}

View file

@ -0,0 +1,6 @@
if (object === null) {
alert("object is null");
// The object is nothing
}
typeof null === "object"; // This stands since the beginning of JavaScript

View file

@ -0,0 +1,6 @@
(1;;2) ~ (1 ; _n ; 2) / ~ is ''identical to'' or ''match'' .
1
_n ~' ( 1 ; ; 2 ) / ''match each''
0 1 0
additional properties : _n@i and _n?i are i; _n`v is _n

View file

@ -0,0 +1,6 @@
to test :thing
if empty? :thing [print [list or word is empty]]
end
print empty? [] ; true
print empty? "|| ; true

View file

@ -0,0 +1,2 @@
isnil = (object == nil)
print(isnil)

View file

@ -0,0 +1 @@
if obj == undefined then print "Obj is undefined"

View file

@ -0,0 +1,18 @@
CACHE>WRITE $DATA(VARI)
0
CACHE>SET VARI="HELLO" WRITE $DATA(VARI)
1
CACHE>NEW VARI WRITE $DATA(VARI) ;Change to a new scope
0
CACHE 1S1>SET VARI(1,2)="DOWN" WRITE $DATA(VARI)
10
CACHE 1S1>WRITE $DATA(VARI(1))
10
CACHE 1S1>WRITE $D(VARI(1,2))
1
CACHE 1S1>SET VARI(1)="UP" WRITE $DATA(VARI(1))
11
<CACHE 1S1>QUIT ;Leave the scope
<CACHE>W $DATA(VARI)," ",VARI
1 HELLO

View file

@ -0,0 +1 @@
x=Null;

View file

@ -0,0 +1,3 @@
x =.
x = (1 + 2;)
FullForm[x]

View file

@ -0,0 +1 @@
SameQ[x,Null]

View file

@ -0,0 +1 @@
x===Null

View file

@ -0,0 +1,4 @@
x =.;
ValueQ[x]
x = 3;
ValueQ[x]

View file

@ -0,0 +1,2 @@
False
True

View file

@ -0,0 +1 @@
VAR foo := NIL

View file

@ -0,0 +1 @@
VAR foo: REF INTEGER := NIL;

View file

@ -0,0 +1,3 @@
IF foo = NIL THEN
IO.Put("Object is nil.\n");
END;

View file

@ -0,0 +1,6 @@
/* NetRexx */
options replace format comments java crossref symbols binary
robject = Rexx -- create an object for which the value is undefined
say String.valueOf(robject) -- will report the text "null"
if robject = null then say 'Really, it''s "null"!'

View file

@ -0,0 +1 @@
type 'a option = None | Some of 'a

View file

@ -0,0 +1,3 @@
match v with
| None -> "unbound value"
| Some _ -> "bounded value"

View file

@ -0,0 +1,4 @@
# here "object" is a reference
if(object = Nil) {
"object is null"->PrintLine();
};

View file

@ -0,0 +1,4 @@
// here "object" is an object pointer
if (object == nil) {
NSLog("object is nil");
}

View file

@ -0,0 +1 @@
[nil fooBar];

View file

@ -0,0 +1,4 @@
declare
X
in
{Show X+2} %% blocks

View file

@ -0,0 +1,4 @@
declare
X = {Value.failed dontTouchMe}
in
{Wait X} %% throws dontTouchMe

View file

@ -0,0 +1,6 @@
declare
X = just("Data")
in
case X of nothing then skip
[] just(Result) then {Show Result}
end

View file

@ -0,0 +1 @@
foo!='foo

View file

@ -0,0 +1,3 @@
$x = NULL;
if (is_null($x))
echo "\$x is null\n";

View file

@ -0,0 +1,7 @@
declare x fixed decimal (10);
...
if ^valid(x) then signal error;
declare y picture 'A9XAAA9';
...
if ^valid(y) then signal error;

View file

@ -0,0 +1,8 @@
my $var;
say $var.WHAT; # Any()
$var = 42;
say $var.WHAT; # Int()
say $var.defined; # True
$var = Nil;
say $var.WHAT; # Any()
say $var.defined # False

View file

@ -0,0 +1,4 @@
my Mu $junction;
say $junction.WHAT; # Mu()
$junction = 1 | 2 | 3;
say $junction.WHAT; # Junction()

View file

@ -0,0 +1,5 @@
my Str $str;
say $str.WHAT; # Str()
$str = "I am a string.";
say $str.WHAT; # Str()
$str = 42; # (fails)

View file

@ -0,0 +1 @@
print defined($x) ? 'Defined' : 'Undefined', ".\n";

View file

@ -0,0 +1 @@
say $number // "unknown";

View file

@ -0,0 +1,2 @@
(if (not MyNewVariable)
(handle value-is-NIL) )

View file

@ -0,0 +1,2 @@
(unless MyNewVariable
(handle value-is-NIL) )

View file

@ -0,0 +1,12 @@
> mapping bar;
> bar;
Result: 0
> bar = ([ "foo":0 ]);
> bar->foo;
Result 0;
> zero_type(bar->foo);
Result: 0
> bar->baz;
Result: 0
> zero_type(bar->baz);
Result: 1

View file

@ -0,0 +1,3 @@
if ($null -eq $object) {
...
}

View file

@ -0,0 +1,3 @@
If variable = #Null
Debug "Variable has no value"
EndIf

View file

@ -0,0 +1,5 @@
x = None
if x is None:
print "x is None"
else:
print "x is not None"

View file

@ -0,0 +1,6 @@
is.null(NULL) # TRUE
is.null(123) # FALSE
is.null(NA) # FALSE
123==NULL # Empty logical value, with a warning
foo <- function(){} # function that does nothing
foo() # returns NULL

View file

@ -0,0 +1,3 @@
x: none
print ["x" either none? x ["is"]["isn't"] "none."]

View file

@ -0,0 +1,13 @@
/*REXX program demonstrates null strings, and also undefined values. */
if symbol('ABC')=="VAR" then say 'variable ABC is defined, value='abc"<<<"
else say "variable ABC isn't defined."
xyz=47
if symbol('XYZ')=="VAR" then say 'variable XYZ is defined, value='xyz"<<<"
else say "variable XYZ isn't defined."
drop xyz
if symbol('XYZ')=="VAR" then say 'variable XYZ is defined, value='xyz"<<<"
else say "variable XYZ isn't defined."
cat=''
if symbol('CAT')=="VAR" then say 'variable CAT is defined, value='cat"<<<"
else say "variable CAT isn't defined."

View file

@ -0,0 +1 @@
puts "object is null" if object.nil?

View file

@ -0,0 +1,22 @@
scala> Nil
res0: scala.collection.immutable.Nil.type = List()
scala> Nil == List()
res1: Boolean = true
scala> Null
<console>:8: error: not found: value Null
Null
^
scala> null
res3: Null = null
scala> None
res4: None.type = None
scala> Unit
res5: Unit.type = object scala.Unit
scala> val a = println()
a: Unit = ()

View file

@ -0,0 +1 @@
(null? object)

View file

@ -0,0 +1 @@
Nil isNil = True.

View file

@ -0,0 +1,6 @@
object isNil ifTrue: [ "true block" ]
ifFalse: [ "false block" ].
nil isNil ifTrue: [ 'true!' displayNl ]. "output: true!"
foo isNil ifTrue: [ 'ouch' displayNl ].
x := (foo == nil).
x := foo isNil

View file

@ -0,0 +1,6 @@
foo := nil.
foo class. "-> UndefinedObject"
foo respondsTo: #'bar'. "asking if a message is implemented"
foo class compile:'fancyOperation ^ 123'.
foo fancyOperation "->123"

View file

@ -0,0 +1 @@
datatype 'a option = NONE | SOME of 'a

View file

@ -0,0 +1,2 @@
case v of NONE => "unbound value"
| SOME _ => "bounded value"

View file

@ -0,0 +1 @@
if {$value eq ""} ...

View file

@ -0,0 +1,3 @@
if {![info exist nullvar]} ...
if {![info exists arr(nullval)]} ...
if {![dict exists $dic nullval]} ...