Case-sensitivity of identifiers: Difference between revisions
This is a real task. The only thing in dispute is the name which can easily change. |
→{{header|Common Lisp}}: The style warnings come from SBCL. |
||
Line 63: | Line 63: | ||
; caught 2 STYLE-WARNING conditions |
; caught 2 STYLE-WARNING conditions |
||
"There is just one dog named Bernie."</lang> |
"There is just one dog named Bernie."</lang> |
||
The style warnings come from [[SBCL]]. |
|||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio; |
<lang d>import std.stdio; |
Revision as of 23:31, 12 May 2011
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
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.
Ada
case insensitive <lang Ada>with Ada.Text_IO; procedure Dogs is
Dog : String := "Bernie";
begin
Ada.Text_IO.Put_Line ("There is just one dog named " & DOG);
end Dogs;</lang>
Output:
There is just one dog named Bernie
AWK
<lang awk>BEGIN { dog = "Benjamin" Dog = "Samba" DOG = "Bernie" printf "The three dogs are named %s, %s and %s.\n", dog, Dog, DOG }</lang>
The three dogs are named Benjamin, Samba and Bernie.
bc
The only variables are 'a' through 'z'. They can only hold numbers, not strings. Some implementations allow longer names like 'dog', but only with lowercase letters. A name like 'Dog' or 'DOG' is a syntax error.
<lang bc>obase = 16 ibase = 16
/*
* Store the hexadecimal number 'BE27A312' * in the variable 'd'. */
d = BE27A312 "There is just one dog named "; d quit</lang>
There is just one dog named BE27A312
Common Lisp
<lang lisp>CL-USER> (let* ((dog "Benjamin") (Dog "Samba") (DOG "Bernie")) (format nil "There is just one dog named ~a." dog))
- in
- LAMBDA NIL
- (LET* ((DOG "Benjamin") (DOG "Samba") (DOG "Bernie"))
- (FORMAT NIL "There is just one dog named ~a." DOG))
- caught STYLE-WARNING
- The variable DOG is defined but never used.
- caught STYLE-WARNING
- The variable DOG is defined but never used.
- compilation unit finished
- caught 2 STYLE-WARNING conditions
"There is just one dog named Bernie."</lang>
The style warnings come from SBCL.
D
<lang d>import std.stdio;
void main() {
string dog = "Benjamin"; string Dog = "Samba"; // identifiers that start with capital letters are type names string DOG = "Bernie"; writefln("There are three dogs named ", dog, ", ", Dog, ", and ", DOG, "'");
}</lang> Output:
There are three dogs named Benjamin, Samba, and Bernie'
dc
A register name has only one character, so this example uses 'd' and 'D'.
<lang dc>[Benjamin]sd [Samba]sD [The two dogs are named ]P ldP [ and ]P lDP [. ]P</lang>
The two dogs are named Benjamin and Samba.
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>
Fortran
Fortran is case insensitive <lang fortran>program Example
implicit none
character(8) :: dog, Dog, DOG
dog = "Benjamin" Dog = "Samba" DOG = "Bernie"
if (dog == DOG) then write(*,*) "There is just one dog named ", dog else write(*,*) "The three dogs are named ", dog, Dog, " and ", DOG end if
end program Example</lang> Output:
There is just one dog named Bernie
Go
Go is case sensitive. Further, visibility depends on case. See the Go entry under the Scope modifiers task. <lang go>package dogs
import "fmt"
var dog, Dog, DOG string
func init() {
dog = "Salt" Dog = "Pepper" DOG = "Mustard"
}
func PackageSees() () {
fmt.Println("Package sees:", dog, Dog, DOG)
}</lang> <lang go>package main
import (
. "dogs" "fmt"
)
// with the dogs package imported, there are three dogs, but only two are // visible from here.
func main() {
// The declaration of dog is required here. Without it, the Println // just below would not compile. The variable dog in the package is // not visible because it begins with a lower case letter. dog := "Benjamin" // four dogs now. three visible from here. PackageSees() fmt.Println("Main sees: ", dog, Dog, DOG, "\n")
// Not a declaration, just an assigment. This assigns a new value to // the variable Dog declared in the package. Dog is visible because // it begins with an upper case letter. Dog = "Samba" // same four dogs, same three visible, one just has a new name. PackageSees() fmt.Println("Main sees: ", dog, Dog, DOG, "\n")
// Of course you can still declare a variable if you want to. This // declares a new variable, shadowing DOG in the package and rendering // it inaccessable even though it begins with an upper case letter. var DOG = "Bernie" // five dogs now. three visible from here. PackageSees() fmt.Println("Main sees: ", dog, Dog, DOG, "\n")
fmt.Println("There are five dogs.")
}</lang> Output:
Package sees: Salt Pepper Mustard Main sees: Benjamin Pepper Mustard Package sees: Salt Samba Mustard Main sees: Benjamin Samba Mustard Package sees: Salt Samba Mustard Main sees: Benjamin Samba Bernie There are five dogs.
Groovy
Solution: <lang groovy>def dog = "Benjamin", Dog = "Samba", DOG = "Bernie" println (dog == DOG ? "There is one dog named ${dog}" : "There are three dogs named ${dog}, ${Dog} and ${DOG}.")</lang>
Output:
There are three dogs named Benjamin, Samba and Bernie.
Icon and Unicon
The program below demonstrates the three dog task. All variants of Icon/Unicon have case sensitive variable names. But if one wasn't this would find it. <lang Icon>procedure main()
dog := "Benjamin" Dog := "Samba" DOG := "Bernie"
if dog == DOG then write("There is just one dog named ", dog,".") else write("The three dogs are named ", dog, ", ", Dog, " and ", DOG, ".")
end</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>
Lua
<lang lua>dog = "Benjamin" Dog = "Samba" DOG = "Bernie"
print( "There are three dogs named "..dog..", "..Dog.." and "..DOG.."." )</lang>
There are three dogs named Benjamin, Samba and Bernie.
PARI/GP
<lang parigp>dog="Benjamin"; Dog="Samba"; DOG="Bernie"; printf("The three dogs are named %s, %s, and %s.", dog, Dog, 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
Prolog
In Prolog, the initial of a variable must be a uppercase letter. So the task can't be completed but we can write this code : <lang Prolog>three_dogs :- DoG = 'Benjamin', Dog = 'Samba', DOG = 'Bernie', format('The three dogs are named ~w, ~w and ~w.~n', [DoG, Dog, DOG]). </lang> The output is :
?- three_dogs. The three dogs are named Benjamin, Samba and Bernie. true.
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
UNIX Shell
<lang bash>dog="Benjamin" Dog="Samba" DOG="Bernie" echo "The three dogs are named $dog, $Dog and $DOG."</lang>
The three dogs are named Benjamin, Samba and Bernie.