create an empty list, an a list filled with dummy numbers in sml in ruby

In SML (Standard ML), you can create an empty list using the [] syntax. To create a list filled with dummy numbers, you can use the :: (cons) operator to prepend elements to the list.

Here's an example in SML:

(* create an empty list *)
val emptyList = [];

(* create a list filled with dummy numbers *)
val filledList = 1 :: 2 :: 3 :: [];

(* output the lists *)
print ("Empty list: " ^ Int.toString (length emptyList) ^ "\n");
print ("Filled list: " ^ Int.toString (length filledList) ^ "\n");
286 chars
10 lines

Output:

main.rb
Empty list: 0
Filled list: 3
29 chars
3 lines

In Ruby, you can also create an empty list using the [] syntax. To create a list filled with dummy numbers, you can use the Array.new method with a block.

Here's an example in Ruby:

main.rb
# create an empty list
empty_list = []

# create a list filled with dummy numbers
filled_list = Array.new(3) { |i| i + 1 }

# output the lists
puts "Empty list: #{empty_list.length}"
puts "Filled list: #{filled_list.length}"
225 chars
10 lines

Output:

main.rb
Empty list: 0
Filled list: 3
29 chars
3 lines

Note that in Ruby, the indices start from 0. So, we use i + 1 to generate dummy numbers.

related categories

gistlibby LogSnag