This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -0,0 +1 @@
list l;

View file

@ -0,0 +1,3 @@
l_p_integer(l, 0, 7);
l_push(l, "a string");
l_append(l, 2.5);

View file

@ -0,0 +1,3 @@
l_query(l, 2);
l_head(l);
l_q_text(l, 1);

View file

@ -0,0 +1 @@
record r;

View file

@ -0,0 +1,2 @@
r_p_integer(r, "key1", 7);
r_put(r, "key2", "a string");

View file

@ -0,0 +1,2 @@
r_query(r, "key1");
r_tail(r);

View file

@ -0,0 +1,8 @@
#lang racket
;; create a list
(list 1 2 3 4)
;; create a list of size N
(make-list 100 0)
;; add an element to the front of a list (non-destructively)
(cons 1 (list 2 3 4))

View file

@ -1,8 +1,12 @@
# creating an empty array and adding values
a = [] # => []
a[0] = 1 # => [1]
a[3] = 2 # => [1, nil, nil, 2]
a = [] #=> []
a[0] = 1 #=> [1]
a[3] = "abc" #=> [1, nil, nil, "abc"]
a << 3.14 #=> [1, nil, nil, "abc", 3.14]
# creating an array with the constructor
a = Array.new # => []
a = Array.new #=> []
a = Array.new(3) #=> [nil, nil, nil]
a = Array.new(3, 0) #=> [0, 0, 0]
a = Array.new(3){|i| i*2} #=> [0, 2, 4]

View file

@ -1,9 +1,23 @@
# creating an empty hash
h = {} # => {}
h["a"] = 1 # => {"a" => 1}
h["test"] = 2.4 # => {"a" => 1, "test" => 2.4}
h[3] = "Hello" # => {"a" => 1, "test" => 2.4, 3 => "Hello"}
h = {} #=> {}
h["a"] = 1 #=> {"a"=>1}
h["test"] = 2.4 #=> {"a"=>1, "test"=>2.4}
h[3] = "Hello" #=> {"a"=>1, "test"=>2.4, 3=>"Hello"}
h = {a:1, test:2.4, World!:"Hello"}
#=> {:a=>1, :test=>2.4, :World!=>"Hello"}
# creating a hash with the constructor
h = Hash.new # => {}
h = Hash.new #=> {} (default value : nil)
p h[1] #=> nil
h = Hash.new(0) #=> {} (default value : 0)
p h[1] #=> 0
p h #=> {}
h = Hash.new{|hash, key| key.to_s}
#=> {}
p h[123] #=> "123"
p h #=> {}
h = Hash.new{|hash, key| hash[key] = "foo#{key}"}
#=> {}
p h[1] #=> "foo1"
p h #=> {1=>"foo1"}

View file

@ -0,0 +1,22 @@
# creating a struct
Person = Struct.new(:name, :age, :sex)
a = Person.new("Peter", 15, :Man)
p a[0] #=> "Peter"
p a[:age] #=> 15
p a.sex #=> :Man
p a.to_a #=> ["Peter", 15, :Man]
p a.to_h #=> {:name=>"Peter", :age=>15, :sex=>:Man}
b = Person.new
p b #=> #<struct Person name=nil, age=nil, sex=nil>
b.name = "Margaret"
b["age"] = 18
b[-1] = :Woman
p b.values #=> ["Margaret", 18, :Woman]
p b.members #=> [:name, :age, :sex]
p b.size #=> 3
c = Person["Daniel", 22, :Man]
p c.to_h #=> {:name=>"Daniel", :age=>22, :sex=>:Man}