開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Scheme (プログラミング言語)
- kscm(コンパイラ(ksc)・インタプリタ(ksi)、実装)
- 計算機プログラムの構造と解釈(参考書籍)
Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 1(Starting with the basics)、Chapter 5(Fruitful subroutines)の Exercise 5-3.を Scheme で取り組んでみる。
Exercise 5-3.
コード(Emacs)
(begin (define (first-l word) (if (string=? word "") #false (string-ref word 0))) (define (last-l word) (if (string=? word "") #false (string-ref word (- (string-length word) 1)))) (define (middle-l word) (if (<= (string-length word) 1) #false (string-copy word 1 (- (string-length word) 1)))) (define (p obj) (display obj) (newline)) (p "1.") (p (middle-l "ab")) (p (middle-l "")) (p (middle-l "\"")) (p "2.") (define (is-palindrome word) (if (<= (string-length word) 1) #true (and (char=? (first-l word) (last-l word)) (is-palindrome (middle-l word))))) (define (p2 word bln1 bln2) (display word) (display " ") (display bln1) (display " ") (display bln2) (newline)) (for-each (lambda (obj) (p2 (car obj) (is-palindrome (car obj)) (cdr obj))) (list (cons "" #t) (cons "a" #t) (cons "aa" #t) (cons "ab" #f) (cons "abc" #f) (cons "aba" #t) (cons "abcdedcba" #t) (cons "abcdeedcba" #t))) 'done )
入出力結果(Terminal, REPL)
$ ./ksi < palindrome.scm > 1. #false #false 2. #true #true a #true #true aa #true #true ab #false #false abc #false #false aba #true #true abcdedcba #true #true abcdeedcba #true #true => done > $
0 コメント:
コメントを投稿