langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,2 @@
# String.create 10 ;;
- : string = "\000\023\000\000\001\000\000\000\000\000"

View file

@ -0,0 +1,2 @@
# "Now just remind me" ^ " how the horse moves again?" ;;
- : string = "Now just remind me how the horse moves again?"

View file

@ -0,0 +1,6 @@
# let str = "some text" ;;
val str : string = "some text"
(* modifying a character, OCaml strings are mutable *)
# str.[0] <- 'S' ;;
- : unit = ()

View file

@ -0,0 +1,5 @@
# str = "Some text" ;;
- : bool = true
# "Hello" > "Ciao" ;;
- : bool = true

View file

@ -0,0 +1,2 @@
# String.copy str ;;
- : string = "Some text"

View file

@ -0,0 +1,8 @@
# let string_is_empty s = (s = "") ;;
val string_is_empty : string -> bool = <fun>
# string_is_empty str ;;
- : bool = false
# string_is_empty "" ;;
- : bool = true

View file

@ -0,0 +1,2 @@
# str ^ "!" ;;
- : string = "Some text!"

View file

@ -0,0 +1 @@
Buffer.add_char str c

View file

@ -0,0 +1,2 @@
# String.sub str 5 4 ;;
- : string = "text"

View file

@ -0,0 +1,7 @@
# #load "str.cma";;
# let replace str occ by =
Str.global_replace (Str.regexp_string occ) by str
;;
val replace : string -> string -> string -> string = <fun>
# replace "The white dog let out a single, loud bark." "white" "black" ;;
- : string = "The black dog let out a single, loud bark."

View file

@ -0,0 +1,22 @@
/* PL/I has immediate facilities for all those operations except for */
/* replace. */
s = t; /* assignment */
s = t || u; /* catenation - append one or more bytes. */
if length(s) = 0 then ... /* test for an empty string. */
if s = t then ... /* compare strings. */
u = substr(t, i, j); /* take a substring of t beginning at the */
/* i-th character andcontinuing for j */
/* characters. */
substr(u, i, j) = t; /* replace j characters in u, beginning */
/* with the i-th character. */
/* In string t, replace every occurrence of string u with string v. */
replace: procedure (t, u, v);
declare (t, u, v) character (*) varying;
do until (k = 0);
k = index(t, u);
if k > 0 then
t = substr(t, 1, k-1) || v || substr(t, k+length(u));
end;
end replace;

View file

@ -0,0 +1,26 @@
const
greeting = 'Hello';
var
s1: string;
s2: ansistring;
s3: pchar;
begin
{ Assignments }
s1 := 'Mister Presiden'; (* typo is on purpose. See below! *)
{ Comparisons }
if s2 > 'a' then
writeln ('The first letter of ', s1, ' is later than a');
{ Cloning and copying }
s2 := greeting;
{ Check if a string is empty }
if s1 = '' then
writeln('This string is empty!');
{ Append a byte to a string }
s1 := s1 + 't';
{ Extract a substring from a string }
s3 := copy(S2, 2, 4); (* s3 receives ello *)
{ String replacement } (* the unit StrUtils of the FreePascal rtl has AnsiReplaceStr *)
s1 := AnsiReplaceStr('Thees ees a text weeth typos', 'ee', 'i');
{ Join strings}
s3 := greeting + ' and how are you, ' + s1 + '?';
end.

View file

@ -0,0 +1,83 @@
# Perl 6 is perfectly fine with NUL *characters* in strings:
my Str $s = 'nema' ~ 0.chr ~ 'problema!';
say $s;
# However, Perl 6 makes a clear distinction between strings
# (i.e. sequences of characters), like your name, or …
my Str $str = "My God, it's full of chars!";
# … and sequences of bytes (called Bufs), for example a PNG image, or …
my Buf $buf = Buf.new(255, 0, 1, 2, 3);
say $buf;
# Strs can be encoded into Bufs …
my Buf $this = 'foo'.encode('ascii');
# … and Bufs can be decoded into Strs …
my Str $that = $this.decode('ascii');
# So it's all there. Nevertheless, let's solve this task explicitly
# in order to see some nice language features:
# We define a class …
class ByteStr {
# … that keeps an array of bytes, and we delegate some
# straight-forward stuff directly to this attribute:
# (Note: "has byte @.bytes" would be nicer, but that is
# not yet implemented in rakudo or niecza.)
has Int @.bytes handles(< Bool elems gist push >);
# A handful of methods …
method clone() {
self.new(:@.bytes);
}
method substr(Int $pos, Int $length) {
self.new(:bytes(@.bytes[$pos .. $pos + $length - 1]));
}
method replace(*@substitutions) {
my %h = @substitutions;
@.bytes.=map: { %h{$_} // $_ }
}
}
# A couple of operators for our new type:
multi infix:<cmp>(ByteStr $x, ByteStr $y) { $x.bytes cmp $y.bytes }
multi infix:<~> (ByteStr $x, ByteStr $y) { ByteStr.new(:bytes($x.bytes, $y.bytes)) }
# create some byte strings (destruction not needed due to garbage collection)
my ByteStr $b0 = ByteStr.new;
my ByteStr $b1 = ByteStr.new(:bytes( 'foo'.ords, 0, 10, 'bar'.ords ));
# assignment ($b1 and $b2 contain the same ByteStr object afterwards):
my ByteStr $b2 = $b1;
# comparing:
say 'b0 cmp b1 = ', $b0 cmp $b1;
say 'b1 cmp b2 = ', $b1 cmp $b2;
# cloning:
my $clone = $b1.clone;
$b1.replace('o'.ord => 0);
say 'b1 = ', $b1;
say 'b2 = ', $b2;
say 'clone = ', $clone;
# to check for (non-)emptiness we evaluate the ByteStr in boolean context:
say 'b0 is ', $b0 ?? 'not empty' !! 'empty';
say 'b1 is ', $b1 ?? 'not empty' !! 'empty';
# appending a byte:
$b1.push: 123;
# extracting a substring:
my $sub = $b1.substr(2, 4);
say 'sub = ', $sub;
# replacing a byte:
$b2.replace(102 => 103);
say $b2;
# joining:
my ByteStr $b3 = $b1 ~ $sub;
say 'joined = ', $b3;

View file

@ -0,0 +1,26 @@
;string creation
x$ = "hello world"
;string destruction
x$ = ""
;string comparison
If x$ = "hello world" : PrintN("String is equal") : EndIf
;string copying;
y$ = x$
; check If empty
If x$ = "" : PrintN("String is empty") : EndIf
; append a byte
x$ = x$ + Chr(41)
; extract a substring
x$ = Mid(x$, 1, 5)
; replace bytes
x$ = ReplaceString(x$, "world", "earth")
; join strings
x$ = "hel" + "lo w" + "orld"

View file

@ -0,0 +1,33 @@
' Create string
s$ = "Hello, world"
' String destruction
s$ = ""
' String comparison
If s$ = "Hello, world" then print "Equal String"
' Copying string
a$ = s$
' Check If empty
If s$ = "" then print "String is MT"
' Append a byte
s$ = s$ + Chr$(65)
' Extract a substring
a$ = Mid$(s$, 1, 5) ' bytes 1 -> 5
'substitute string "world" with "universe"
a$ = "Hello, world"
for i = 1 to len(a$)
if mid$(a$,i,5)="world" then
a$=left$(a$,i-1)+"universe"+mid$(a$,i+5)
end if
next
print a$
'join strings
s$ = "See " + "you " + "later."
print s$