Drkcore

13 08 2011 Scheme javascript Tweet

Scheme修行 11章

JGPによるとjavascriptはCの皮を被ったLispだからLISPの作法にも慣れておくと良いと思うのです。というよりこのシリーズは面白いので読むといいと思います。

ProductName Scheme修行
Daniel P. Friedman and Matthias Felleisen
オーム社 / 2940円 ( 2011-06-15 )


two-in-a-rowっていうリストの中に同じ値が隣あっているかどうかを調べる関数を定義する。

まずは相互再帰

(define two-in-a-row?
  (lambda (lat)
    (cond
     ((null? lat) #f)
     (else
      (is-first-b? (car lat) (cdr lat))))))

(define is-first-b?
  (lambda (a lat)
    (cond
     ((null? lat) #f)
     (else (or (eq? (car lat) a)
           (two-in-a-row? lat))))))

これを直接再帰に書き直す。

(define two-in-a-row-b?
  (lambda (preceeding lat)
    (cond 
     ((null? lat) #f)
     (else (or (eq? (car lat) preceeding)
           (two-in-a-row-b? (car lat)
                (cdr lat)))))))

(define two-in-a-row-2?
  (lambda (lat)
    (cond
     ((null? lat) #f)
     (else (two-in-a-row-b? (car lat)
                (cdr lat))))))

8/20にJGPの読書会@静岡があります。

ProductName JavaScript: The Good Parts ―「良いパーツ」によるベストプラクティス
Douglas Crockford
オライリージャパン / 1890円 ( 2008-12-22 )


About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021