Linguistic Diversity -------------------- Each of the programs below does the same thing when run with an appropriate interpreter or compiler: it displays "Hello, who are you?", reads a text string, and then displays each "prefix string" of the string entered by the user, from smallest to largest. E.g. Computer: Hello, who are you? User: Somebody Computer: S Computer: So Computer: Som Computer: Some Computer: Someb Computer: Somebo Computer: Somebod Computer: Somebody The appearance of the source code versions is quite varied. In general, the better I knew a language, the more concise and readable my version of the program will be in that language. (This is true of expression in natural languages as well.) Some of the languages do lend themselves to shorter implementations of this particular program. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #!/usr/bin/awk # This program is in awk, a text-processing language popular among system # administrators. BEGIN { print "Hello, who are you?" } { for (i=1; i<=length($0); i++) print substr($0, 1, i); exit(0); } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - REM This version is written for an older dialect of BASIC, like Microsoft REM GW-BASIC or AppleSoft BASIC. (This version worked in BWBasic.) 10 PRINT "Hello, who are you?" 20 INPUT "", PERSON$ 30 FOR I = 1 TO LEN(PERSON$) 40 PRINT LEFT$(PERSON$, I) 50 NEXT I - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' This version is written for a more modern dialect of BASIC, like Microsoft ' QuickBASIC. The only significant difference is that line numbers are no ' longer required. Some versions will also allow us to drop the "$" after ' string variables and functions. (This version also worked in BWBasic.) Print "Hello, who are you?" Input "", Person$ For I = 1 To Len(Person$) Print Left$(Person$, I) Next - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /* This program is in C, a low-level system programming language. */ #include #include #define MAX 100 int main(int argc, char *argv[]){ int i; char person[MAX], temp[MAX]; printf("Hello there, who are you?\n"); fgets(person, MAX, stdin); bzero(temp, MAX); /* This should be "i<=strlen(person)", but fgets annoyingly includes the newline character in the string. */ for (i=1; i; chop($person); for ($i=1; $i<=length($person); $i++){ print substr($person, 0, $i) . "\n"; } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (* This program is written in the high-level language Pascal, which has been popular as a teaching language, as well as a system programming language for the Macintosh and sometimes on DOS. Pascal survives today in Borland Delphi, which is akin to Microsoft's Visual Basic. *) program hello(input, output); var person: string; i: integer; begin writeln('Hello, who are you?'); readln(person); i := 1; while (i <= length(person)) do begin writeln(copy(person, 1, i)); i := i + 1; end; end. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #!/usr/bin/python # This program is in Python, a very high level scripting language. print "Hello, who are you?" person = raw_input() for i in range(len(person)): print person[0:i+1] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #!/usr/bin/ruby # This program is written in Ruby, a very high level object-oriented # scripting language which is popular in Japan. puts "Hello, who are you?" person = readline.chop 1.upto(person.length) { |i| puts person[0..i-1]; } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ; This program is in Scheme, a dialect of the very high level functional ; programming language LISP. (define (print-rest-of-string s n) (if (<= n (string-length s)) (begin (display (substring s 0 n)) (newline) (print-rest-of-string s (+ n 1))) nil)) (display "Hello, who are you?\n") (define person (read-line)) (print-rest-of-string person 1) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #!/bin/bash # This program is a shell script (written in the Bourne shell, the standard # user interface for the Unix operating system). echo "Hello, who are you?" read PERSON LENGTH=$(echo -n "$PERSON" | wc -c) for i in $(seq "$LENGTH") do echo "$PERSON" | cut -c1-$i done