Case-sensitivity of identifiers: Difference between revisions
(Added PicoLisp) |
(→{{header|Tcl}}: Add Ruby.) |
||
Line 55: | Line 55: | ||
The three dogs are named Benjamin , Samba , and Bernie |
The three dogs are named Benjamin , Samba , and Bernie |
||
>>> </lang> |
>>> </lang> |
||
=={{header|Ruby}}== |
|||
Ruby gives a special meaning to the first letter of a name. A lowercase letter starts a local variable. An uppercase letter starts a constant. So <tt>dog</tt> is a local variable, but <tt>Dog</tt> and <tt>DOG</tt> are constants. To adapt this task to Ruby, I added <tt>dOg</tt> and <tt>doG</tt> so that I have more than one local variable. |
|||
<lang ruby>module FiveDogs |
|||
dog = "Benjamin" |
|||
dOg = "Dogley" |
|||
doG = "Fido" |
|||
Dog = "Samba" # this constant is FiveDogs::Dog |
|||
DOG = "Bernie" # this constant is FiveDogs::DOG |
|||
names = [dog, dOg, doG, Dog, DOG] |
|||
names.uniq! |
|||
puts "There are %d dogs named %s." % [names.length, names.join(", ")] |
|||
puts |
|||
puts "The local variables are %s." % local_variables.join(", ") |
|||
puts "The constants are %s." % constants.join(", ") |
|||
end</lang> |
|||
Output: <pre>There are 5 dogs named Benjamin, Dogley, Fido, Samba, Bernie. |
|||
The local variables are dog, dOg, doG, names. |
|||
The constants are Dog, DOG.</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
Revision as of 19:30, 23 February 2011
Three dogs (Are there three dogs or one dog?) is a code snipped used to illustrate the lettercase sensitivity of the programming language. For a case sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language that is lettercase insensitive, we get the following output:
There is just one dog named Bernie.
Euphoria
<lang Euphoria>-- These variables are all different sequence dog = "Benjamin" sequence Dog = "Samba" sequence DOG = "Bernie" printf( 1, "The three dogs are named %s, %s and %s\n", {dog, Dog, DOG} )</lang>
J
<lang j> NB. These variables are all different
dog=: 'Benjamin' Dog=: 'Samba' DOG=: 'Bernie' 'The three dogs are named ',dog,', ',Dog,', and ',DOG
The three dogs are named Benjamin, Samba, and Bernie </lang>
Java
<lang java>String dog = "Benjamin"; String Dog = "Samba"; //in general, identifiers that start with capital letters are class names String DOG = "Bernie"; //in general, identifiers in all caps are constants //the conventions listed in comments here are not enforced by the language System.out.println("There are three dogs named " + dog + ", " + Dog + ", and " + DOG + "'");</lang>
Perl
<lang perl># These variables are all different $dog='Benjamin'; $Dog='Samba'; $DOG='Bernie'; print "The three dogs are named $dog, $Dog, and $DOG \n"</lang>
PicoLisp
<lang PicoLisp>(let (dog "Benjamin" Dog "Samba" DOG "Bernie")
(prinl "The three dogs are named " dog ", " Dog " and " DOG) )</lang>
Output:
The three dogs are named Benjamin, Samba and Bernie
PureBasic
<lang PureBasic>dog$="Benjamin" Dog$="Samba" DOG$="Bernie" Debug "There is just one dog named "+dog$</lang>
Python
Python names are case sensitive: <lang python>>>> dog = 'Benjamin'; Dog = 'Samba'; DOG = 'Bernie' >>> print ('The three dogs are named ',dog,', ',Dog,', and ',DOG) The three dogs are named Benjamin , Samba , and Bernie >>> </lang>
Ruby
Ruby gives a special meaning to the first letter of a name. A lowercase letter starts a local variable. An uppercase letter starts a constant. So dog is a local variable, but Dog and DOG are constants. To adapt this task to Ruby, I added dOg and doG so that I have more than one local variable.
<lang ruby>module FiveDogs
dog = "Benjamin" dOg = "Dogley" doG = "Fido" Dog = "Samba" # this constant is FiveDogs::Dog DOG = "Bernie" # this constant is FiveDogs::DOG
names = [dog, dOg, doG, Dog, DOG] names.uniq! puts "There are %d dogs named %s." % [names.length, names.join(", ")] puts puts "The local variables are %s." % local_variables.join(", ") puts "The constants are %s." % constants.join(", ")
end</lang>
Output:
There are 5 dogs named Benjamin, Dogley, Fido, Samba, Bernie. The local variables are dog, dOg, doG, names. The constants are Dog, DOG.
Tcl
Tcl variable names are case sensitive: <lang tcl>set dog "Benjamin" set Dog "Samba" set DOG "Bernie" puts "The three dogs are named $dog, $Dog and $DOG"</lang> Which prints...
The three dogs are named Benjamin, Samba and Bernie