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

From “The Seasoned Schemer“.

“The Seasoned Schemer,” written by Daniel P. Friedman and Matthias Felleisen, will be a book for intermediate Scheme programming students.

The book offers short puzzles and exercises to deepen your knowledge of Scheme. It also explains important programming concepts and functional programming techniques in Scheme.

“The Seasoned Schemer” is characterized by its simple, easy-to-understand problem set. Each problem does not have a correct answer, but often has more than one answer, requiring the reader to find his or her own solution.

This book, like “The Little Schemer” assumes that S-expressions are used to write Scheme programs. The book also provides detailed explanations of functional programming techniques, making it a good book for beginners in functional programming.

This book is also a follow-up to “The Reasoned Schemer” an explanatory book on miniKanren, a DSL for logic programming, previously described in “Clojure core.logic and miniKanren.

「The notion that “thinking about computers is one of the most exciting things the human mind can do” is what led The Little Schemer (formerly known as The Little LISPer) and its new sister volume, The Seasoned Schemer, to be called “The Reasoned 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 contents are as follows.

11. Welcome Back to the Show 
12. Take Cover
13. Hop, Skip, and1 Jump
14. Let There Be Names
15. The Difference Between Men and Boys... 
16. Ready, Set, Bang!
17. We Change, Therefore We Are!
18. We Change, Therefore We Are the Same!
19. Absconding with the Jewels
20. What's in Store?
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 Seasoned Schemer” for further implementation details.

コメント

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