[PYTHON] Implement the REPL

If you're exposed to programming languages, you'll want to implement a REPL.

Abbreviation for Read-eval-print loop Read, evaluate, display and repeat like letters. Refers to an interactive evaluation environment.

What is REPL?-Hatena Keyword

I see.

(loop (print (eval (read))))

Read in order from the right, "Read-Eval-Print Loop" is certainly ** REPL **.

If you want to execute the contents of this article and stop in the middle, use Ctrl + D </ kbd> or Ctrl + C </ kbd>.

Let's do it with Ruby.

% ruby -e'loop{p eval gets}'
1 + 2 * 3
7
"Pixiv".downcase
"pixiv"

Yeah, it's perfect. "Gets-Eval-P Loop" is ** GEPL **.

PHP is a little longer.

% php -r'while(1){ $code = sprintf("\$result = %s;", fgets(STDIN)); eval($code); var_dump($result); }'
1 + 2 * 3
int(7)
strtolower("Pixiv")
string(5) "pixiv"

“Vardump-Eval-Fgets While” is ** VEFW **. What is it?

How about Python (2.7)? It's very hard to write with one liner.

% python -c "while True:
    print(input())
"
1 + 2 * 3
7
"Pixiv".lower()
pixiv

I don't know why, but Python 2 series ʻinput () is the same as ʻeval (raw_input (prompt)). Everyone wondered who would get it ... Python 3000's ʻinput ()is the same as the 2nd seriesraw_input ()`.

"Input-Print While" is ** IPW **. Or "Rawinput-Eval-Print-While" ** REPW **!

Finally

I wrote something like ~ in Lisp, but I haven't written Common Lisp properly, so I tried it.

% sbcl
This is SBCL 1.2.16, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (loop (print (eval (read))))
(+ 1 (* 2 3))

7
debugger invoked on a END-OF-FILE in thread
#<THREAD "main thread" RUNNING {1002C74753}>:
  end of file on #<SB-SYS:FD-STREAM for "standard input" {100632C013}>

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(SB-IMPL::INPUT-CHAR/UTF-8 #<SB-SYS:FD-STREAM for "standard input" {100632C013}> T 0)
0](string-downcase "Pixiv")

"pixiv"
0] ^D
*

I'm not sure, but I didn't get the default prompt *, so I felt like it was working. (Maybe it's not working)

I also thought about studying Perl, but I didn't understand the data type so I gave up.

Recommended Posts

Implement the REPL
Implement the Singleton pattern in Python
Implement the multi-layer perceptron very neatly
We will implement the optimization algorithm (overview)
Implement RSA
Implement the shortest path search for the maze
[Python] Fluid simulation: Implement the advection equation
Implement part of the process in C ++
We will implement the optimization algorithm (firefly algorithm)
[Python] Fluid simulation: Implement the diffusion equation
We will implement the optimization algorithm (bat algorithm)
We will implement the optimization algorithm (Problem)
We will implement the optimization algorithm (Kujira-san algorithm)
[Keras] Implement noisy student and check the effect
Implement the autocomplete feature on Django's admin screen
We will implement the optimization algorithm (cuckoo search)
I tried to implement the traveling salesman problem
We will implement the optimization algorithm (harmony search)
Access the variables defined in the script from the REPL