牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

2.3 Symbolic Data の 2.3.1 Quotation を Python でやってみた

SICP2.3 Symbolic Data の 2.3.1 Quotation あたりを Python でやってみた

scheme
;;;2.3  Symbolic Data
;;2.3.1  Quotation

(define a 1)
(define b 2)
(list a b)
(list 'a 'b)
(list 'a b)
(car '(a b c))
(cdr '(a b c))
;(1 2)
;(a b)
;(a 2)
;a
;(b c)

(define (memq item x)
  (cond ((null? x) false)
        ((eq? item (car x)) x)
        (else (memq item (cdr x)))))
(memq 'apple '(pear banana prune))
;#f
(memq 'apple '(x (apple sauce) y apple pear))
;(apple pear).
python
#2.3  Symbolic Data
#2.3.1  Quotation

#A
def a():
    return 1
def b():
    return 2
print [a(), b()]
print ['a()', 'b()']
print ['a()', b()]
print ['a()', 'b()', 'c()'][0]
print ['a()', 'b()', 'c()'][1:]
# [1, 2]
# ['a()', 'b()']
# ['a()', 2]
# a()
# ['b()', 'c()']

#B
a = 1
b = 2
print [a, b]
print ['a', 'b']
print ['a', b]
print ['a', 'b', 'c'][0]
print ['a', 'b', 'c'][1:]
# [1, 2]
# ['a', 'b']
# ['a', 2]
# a
# ['b', 'c']

def memq(item, x):
    if not x:
        return False
    if item == x[0]:
        return x
    return memq(item, x[1:])
print memq('apple', ['pear', 'banana', 'prune'])
#False
print memq('apple', ['x', 'apple sauce', 'y', 'apple', 'epar'])
#['apple', 'epar']

こんな感じ?