The Little Schemer

Web Technology Digital Transformation Artificial Intelligence Natural Language Processing Semantic Web Chatbot and Q&A User Interface Knowledge Information Processing Machine Learning Programming LISP and Derived Languages  Navigation of this blog

Scheme Programming

Scheme, a functional programming language, is one of the dialects of Lisp and is an old language that has existed since the 1950s.

The features of Scheme programming are as follows

  • Simple and consistent syntax: Scheme has a simple syntax that allows writing compact and readable programs, characterized by the grouping of expressions in parentheses common to the Lisp family of languages.
  • Powerful procedural programming: Scheme can treat procedures (functions) as first-class objects. This allows for flexible programming styles such as higher-order functions (functions that take a function as an argument and return it as a return value) and closures (functions bound to an environment).
  • Manipulation of recursive data structures: Scheme is well suited for manipulating recursive data structures (e.g., lists and trees), and recursive functions can be used to traverse data structures and implement efficient and flexible algorithms.
  • Dynamically typed: Scheme is a dynamically typed language, as described in “Difference between statically and dynamically typed languages in programming“, and does not require declarations of variable types. This allows for greater flexibility and simpler program writing. However, it is important to note that there is a possibility of errors due to dynamic type checking.
  • Powerful macro system: Scheme has a powerful macro system that allows you to extend program syntax and create your own DSL (Domain-Specific Language). This allows for a higher level of abstraction and automation.
  • Stable specification and implementation: Scheme has a standard specification called “RnRS (Revised^n Report on the Algorithmic Language Scheme)” and various implementations exist. This allows for high compatibility among different implementations and stable program development.

Typical Scheme implementations include MIT Scheme, GNU Guile, Racket, etc. The language is not only widely used in education and research, but is also suitable for developing practical programs.

Implementation example

The implementation of Scheme programming is as follows

Hello World:

(display "Hello, World!")
(newline)

Definition of the factorial function:

(define (factorial n)
  (if (= n 0)
      1
      (* n (factorial (- n 1)))))

(display (factorial 5))
(newline)

Definition of a procedure to sum the elements of a list:

(define (sum-list lst)
  (if (null? lst)
      0
      (+ (car lst) (sum-list (cdr lst)))))

(display (sum-list '(1 2 3 4 5)))
(newline)

As an example of the use of higher-order functions, define a procedure that doubles each element of a list:

(define (double-each lst)
  (map (lambda (x) (* x 2)) lst))

(display (double-each '(1 2 3 4 5)))
(newline)

Definition of procedures to realize counters using closures:

(define (make-counter)
  (let ((count 0))
    (lambda ()
      (set! count (+ count 1))
      count)))

(define counter (make-counter))
(display (counter)) ; 1
(newline)
(display (counter)) ; 2
(newline)
Areas where Scheme Programming is utilized

Scheme programming is utilized in the following areas

Education: Scheme’s simple and consistent syntax makes it suitable for learning the fundamentals of functional programming; Scheme is widely used in academic computer science education and introductory programming classes.

Research: Scheme can be used as a powerful means of abstraction and automation; Scheme’s macro system can be used to experiment with the properties and features of new programming languages; Scheme is also suitable for studying functional programming concepts and algorithms Scheme is also suitable for the study of functional programming concepts and algorithms.

Program prototyping: Scheme’s simple syntax and powerful procedural programming properties make it suitable for testing ideas and prototyping programs. You can quickly write experimental code and see how it works.

Use as a scripting language: Scheme is sometimes used as a command line tool or script. Scheme can be used to create scripts for simple tasks or automation.

Small embedded systems: Scheme’s compactness makes it suitable for use in embedded and embedded systems with limited resources. In particular, there is a small implementation based on R7RS (Revised^7 Report on the Algorithmic Language Scheme).

However, Scheme is relatively rarely chosen as the primary programming language for general application development or large commercial projects, and more popular languages (Java, Python, C++, etc.) are more widely used for more general applications.

The Little Schemer

From “The Little Schemer“.

The Little Schemer” will be an educational book on the Scheme programming language written by Daniel P. Friedman and Matthias Felleisen. The book teaches basic computer science concepts such as list structures and recursive functions in a unique question-and-answer format.

The book provides small steps for readers to solve simple problems before writing Scheme code. Then, through the solutions, the reader learns the concepts necessary to program in Scheme.

This book is worth reading even for those who are familiar with programming languages before learning a new programming language. It is also a good introduction for beginners in programming to learn the basic concepts of computer science.

A sequel to this book is “The Seasoned Schemer. It is further followed by “The Reasoned Schemer” an explanatory book on miniKanren, a DSL for logic programming, previously described in “Clojure core.logic and miniKanren.

The contents include recursion and anonymous functions, lambda functions, Y-combinators, and a simple interpreter.

「The notion that “thinking about computers is one of the most exciting things the human mind can do” is the inspiration for The Little Schemer (formerly known as The Little LISPer) and its new sister volume, The Seasoned Schemer. set them apart from other books on LISP. The author’s enthusiasm for the subject is evident as he presents abstract concepts in a humorous and understandable way. This book will open new doors of thinking for anyone who wants to know what a computer is. “The Little Schemer” introduces computers as an extension of the arithmetic and algebra taught in elementary and high school. It introduces programs as recursive functions and briefly touches on the limits of what a computer can do. The author uses the programming language Scheme and interesting food to illustrate these abstract ideas. The Seasoned Schemer introduces readers to further aspects of computing, such as functions as values, state changes, and exceptional cases. Little LISPer” has long been a popular introduction to LISP. The Little Schemer and The Seasoned Schemer are worthy successors and are equally popular as textbooks for Scheme courses and as companion texts for complete introductory courses in Computer Science. will be equally popular.”」

The table of contents is shown below.

1. Toys
2. Do it, Do it Again, and Again, and Again
3. Cons the Magnificent
4. Number Games
5. *Oh My Gawd: It7s Full of Stars
6. Shadows
7. Friend and Relations
8. Lambda the Ultimate
9. and Again, and Again,...
10. What is the value of All of This?
Try Schema

To use Scheme on a Mac, the Scheme processor must be installed. This can be installed using Homebrew. Open a terminal and execute the following command

>brew install mit-scheme

This will install MIT/GNU Scheme. Next, write your code using a text editor. in IT Infrastructure Technology” for an introduction to the various text editors.

Next, start the REPL, which is an interactive execution environment for Scheme. This will invoke the Scheme processor and start the REPL; in the REPL, you can enter Scheme expressions and see the results immediately.

Execution of the code file created in the text editor is done by executing the scheme < filename command in the terminal.

The following are simple Hello World codes for reference. These are for confirmation with the REPL.

First, for those using the display procedure, execute the following code.

> (display "Hello World!")
Hello World!

The next case in which the PRINT procedure is used is as follows.

> (print "Hello World!")
"Hello World!"

See “The Little Schemer” for further implementation details.

コメント

タイトルとURLをコピーしました