Interactive programming (repl)
You are encouraged to solve this task according to the task description, using any language you may know.
Many language implementations come with a command line interpreter (a.k.a. shell or REPL).
Show how to start the interpreter, then, as a small example of its use, interactively create a function of two strings and a separator that returns the strings separated by two concatenated instances of the separator.
For example, f('Rosetta', 'Code', ':') should return 'Rosetta::Code'
Note: this task is not about creating your own interpreter.
Common Lisp
The details of interactive use vary widely between implementations; this example is from SBCL. *
is the prompt. By default, SBCL compiles (not interprets) all code, unless sb-ext:*evaluator-mode*
is changed.
$ rlwrap sbcl This is SBCL 1.0.25, an implementation of ANSI Common Lisp. More information about SBCL is available at <http://www.sbcl.org/>. ... * (defun f (string-1 string-2 separator) (concatenate 'string string-1 separator separator string-2)) F * (f "Rosetta" "Code" ":") "Rosetta::Code" *
E
<lang sh>$ rune # from an OS shell. On Windows there is also a desktop shortcut.</lang>
"?
" and ">
" are prompts for input; "#
" marks output.
<lang e>? def f(string1 :String, string2 :String, separator :String) { > return separator.rjoin(string1, "", string2) > }
- value: <f>
? f("Rosetta", "Code", ":")
- value: "Rosetta::Code"
</lang>
If you type a definitely incomplete expression, such as "def f() {
", then it gives an ">
" prompt and takes additional lines. If the expression is not necessarily incomplete, you can continue anyway by ending a line with "\
".
Forth
All Forth systems come with an interpreter. On embedded systems, the interpreter functions as a monitor or lightweight operating system. (User input is shown here in italics.)
$ gforth Gforth 0.7.0, Copyright (C) 1995-2008 Free Software Foundation, Inc. Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license' Type `bye' to exit : f ( separator suffix prefix -- ) compiled pad place 2swap 2dup pad +place pad +place pad +place compiled pad count ; ok s" :" s" Code" s" Rosetta" f cr type Rosetta::Code ok
Haskell
The details of interactive use vary widely between implementations. This example is from GHCi.
$ ghci ___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 6.4.2, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Loading package base-1.0 ... linking ... done. Prelude> let f as bs sep = as ++ sep ++ sep ++ bs Prelude> f "Rosetta" "Code" ":" "Rosetta::Code"
Logo
$ logo Welcome to Berkeley Logo version 5.6 ? to f :prefix :suffix :separator > output (word :prefix :separator :separator :suffix) > end f defined ? show f "Rosetta "Code ": Rosetta::Code ?
OCaml
Because you can enter expressions that span multiple lines, you have to type the double semicolon (";;") at the end so that it knows you are done.
$ ocaml Objective Caml version 3.10.2 # let f s1 s2 sep = String.concat sep [s1; ""; s2];; val f : string -> string -> string -> string = <fun> # f "Rosetta" "Code" ":";; - : string = "Rosetta::Code" #
Perl
Perl doesn't have an interpreter, but there is an interactive debugger:
$ perl -de1 Loading DB routines from perl5db.pl version 1.3 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 1 DB<1> sub f {my ($s1, $s2, $sep) = @_; $s1 . $sep . $sep . $s2} DB<2> p f('Rosetta', 'Code', ':') Rosetta::Code DB<3> q
Python
Start the interpreter by typing python at the command line (or select it from a menu). You get a response showing the version of the interpreter being run before giving an input prompt of three greater-than characters and a space:
<lang python>python Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> def f(string1, string2, separator): return separator.join([string1, , string2])
>>> f('Rosetta', 'Code', ':') 'Rosetta::Code' >>> </lang>
Ruby
Start the interpreter by typing irb at the command line. You will see an input prompt, which by default is name of this program(name of main object):line number:indent level>
:
<lang ruby>$ irb irb(main):001:0> def f(string1, string2, separator) irb(main):002:1> [string1, , string2].join(separator) irb(main):003:1> end => nil irb(main):004:0> f('Rosetta', 'Code', ':') => "Rosetta::Code" irb(main):005:0> </lang>
Standard ML
Because you can enter expressions that span multiple lines, you have to type the semicolon (";") at the end so that it knows you are done.
$ sml Standard ML of New Jersey v110.67 [built: Fri Jul 4 09:00:58 2008] - fun f (s1, s2, sep) = String.concatWith sep [s1, "", s2]; [autoloading] [library $SMLNJ-BASIS/basis.cm is stable] [autoloading done] val f = fn : string * string * string -> string - f ("Rosetta", "Code", ":"); val it = "Rosetta::Code" : string -
Tcl
$ tclsh % proc f {s1 s2 sep} { append result $s1 $sep $sep $s2 } % f Rosetta Code : Rosetta::Code % exit