The Reasoned 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 Reasoned Schemer

From “The Reasoned Schemer“.

“The Reasoned Schemer,” written by Daniel P. Friedman, William E. Byrd, and Oleg Kiselyov, will be a book on problem solving using miniKanren, a DSL logic programming language described in “Clojure core.logic and miniKanren The book will be about problem solving using miniKanren, a programming language.

miniKanren is a type of constraint logic programming, influenced by Prolog. miniKanren allows problems to be expressed as constraints and automatically solved.

“The Reasoned Schemer” explains how to use miniKanren to solve problems using constraint logic programming. It also explains important programming concepts and functional programming techniques in Scheme.

The book assumes that Scheme programs are written using S-expression, as in “The Little Shemer” and “The Seasoned Schemer“. Also, knowledge of constraint logic programming using miniKanren is required.

“The Reasoned Schemer” is recommended reading for those who are interested in problem solving techniques using miniKanren, or for those who are familiar with Scheme and functional programming.

“The purpose of The Reasoned Schemer is to help functional programmers think logically, and logic programmers think functionally. the author of The Reasoned Schemer believes that logic programming is a natural extension of functional programming, and that logic programming is a natural extension of functional programming. By extending Scheme, a functional language, with logical constructs, they show that it combines the advantages of both styles. This extension encompasses most of the ideas of the logic programming language Prolog. the teaching method of The Reasoned Schemer is a series of questions and answers with the characteristic humor found in The Little Schemer and The Seasoned Schemer. The teaching method of The Reasoned Schemer is a series of questions and answers with the characteristic humor found in The Little Schemer and The Seasoned Schemer. It is assumed that you are familiar with functional languages and the first eight chapters of The Little Schemer. To add logic functions, you will need to introduce new forms. The author’s goal is to show how writing logic programs is the same as writing functional programs using these forms. In this way, readers of The Reasoned Schemer will come to understand how simple logic programming is, and how easy it is to define functions that behave like relations.”

The content of the book is as follows.

1. Playthings 
2. Teaching Old Toys New Tricks
3. Seeing Old Friends in New Ways
4. Double Your Fun
5. Members Only
6. The Fun Never Ends..
7. A Bit Too Mutch
8. Just a Bit More
9. Thin Ice
10. Under The Hood
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 Reasoned Schemer” for further implementation details.

コメント

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