Functional programming 编写三个Scheme程序来模拟这三个门:AND、OR和XOR

Functional programming 编写三个Scheme程序来模拟这三个门:AND、OR和XOR,functional-programming,scheme,racket,Functional Programming,Scheme,Racket,到目前为止,我认为最后两项应该是: (define or-gate (lambda (a b) (if (= a 1) 1 (if (= b 1) 1 0)))) (define xor-gate (lambda (a b) (if (= a b) 0 1))) …但这一点令人费解。如何实现它?如果我们记住=可以接受两个以上的参数,那么这很简单: (defi

到目前为止,我认为最后两项应该是:

(define or-gate
  (lambda (a b)
    (if (= a 1)
        1
        (if (= b 1)
            1
            0))))

(define xor-gate
  (lambda (a b)
    (if (= a b)
        0
        1)))

…但这一点令人费解。如何实现它?

如果我们记住
=
可以接受两个以上的参数,那么这很简单:

(define and-gate
  (lambda (a b)
    (if (= a b 1)
        1
        0)))

换句话说:
逻辑连接器是
真的
当且仅当它的两个参数都是
真的
,对于所有其他参数它是
假的

什么是
的真值表?实际上,如果您有一个
异或
的真值表,那么算法是相同的

让我们创建一个接受真值表的函数,并返回一个计算门逻辑的函数

(define (gate-logic-for-truth-table table)
  (lambda (a b)
    (vector-ref (vector-ref table b) a)))
现在,使用
的真值表,我们生成
和门
函数:

(define and-gate (gate-logic-for-truth-table
                  '#(#(0 0)
                     #(0 1))))
还有一个小测试:

> (and-gate 0 0)
0
> (and-gate 0 1)
0
> (and-gate 1 0)
0
> (and-gate 1 1)
1

假设参数为
1
0

对于
,如果
a
1
,则无需查看
b
——结果是
1
。否则结果为
b

对于
,如果
a
0
,则无需查看
b
——结果为
0
。否则,结果为
b

如果要使其尽可能类似于
或gate
,可以将外部条件中的
1
s替换为
0
s:

(define and-gate
    (lambda (a b)
        (if (= a 0)
            0
            (if (= b 1)
                1
                0))))
或者,如果要坚持与
1
进行比较,可以重新排列分支:

(define and-gate
    (lambda (a b)
        (if (= a 1)
            (if (= b 1)
                1
                0)
             0)))
可以缩短代码:

(define and-gate
    (lambda (a b)
        (if (= a 1)
            b
            0)))


但这是否更具可读性是一个相当独立的问题。

这里有一个简单的定义,其中真值表是明确的:

(define (and-gate a b)
  (cond
    [(and (= a 0) (= b 0))  0]
    [(and (= a 0) (= b 1))  0]
    [(and (= a 1) (= b 0))  0]
    [(and (= a 1) (= b 1))  1]))
如果我们注意到只有一个子句的结果为1,而所有其他子句的结果为0, 我们可以写:

(define (and-gate a b)
  (cond
    [(and (= a 1) (= b 1))  1]
    [else                   0]))

如果我们要使用盖茨,我们可能应该首先定义一种构建它们的方法。我的意思是我们必须建造三个大门

#lang racket

;; Gate Constructor
(define (make-gate predicate)
  (lambda (A B)
    (predicate A B)))
然后,我们可以用一厢情愿的想法在高层定义闸门:

(define and-gate
  (make-gate and-predicate))

(define or-gate
  (make-gate or-predicate))

(define xor-gate
  (make-gate xor-predicate))
然后我们可以任意定义内部门逻辑,不管我们怎么想:

(define (and-predicate A B)
  (let ([a (if A 1 0)]
        [b (if B 1 0)])
    (= 2 (+ a b))))

(define (or-predicate A B)
  (let ([a (if A 1 0)]
        [b (if B 1 0)])
    (< 0 (+ a b))))

(define (xor-predicate A B)
  (let ([a (if A 1 0)]
        [b (if B 1 0)])
    (= 1 (+ a b))))
运行测试 既然所有的考试都通过了,我们现在就可以交作业了[一如既往地遵守学术政策]

笔记
使用真值
#f
#t
可以为球拍的测试工具提供更干净的挂钩。它还允许直接编写谓词,而不是序列化和反序列化
1
0

我想告诉您如何在有序列表中输入节点,但您删除了qestiom。如果您仍然需要解决方案,您可以在论坛的第一部分(针对初学者)的我的个人论坛上用英语提问,我会回答您的问题。:)
(define (and-predicate A B)
  (let ([a (if A 1 0)]
        [b (if B 1 0)])
    (= 2 (+ a b))))

(define (or-predicate A B)
  (let ([a (if A 1 0)]
        [b (if B 1 0)])
    (< 0 (+ a b))))

(define (xor-predicate A B)
  (let ([a (if A 1 0)]
        [b (if B 1 0)])
    (= 1 (+ a b))))
(module+ test
  (require rackunit
       rackunit/text-ui)

  (define (make-test-harness test-function)
    (define (test-harness ins outs)
      (if (or (null? ins)
              (null? outs))
      'test-complete
      (begin
        (test-function (first ins)
                       (first outs))
        (test-harness (rest ins)
                      (rest outs)))))
    test-harness))

  (define gate-inputs
    '((#f #f)
      (#t #f)
      (#f #t)
      (#t #t)))

  (define and-truth-table
    '(#f #f #f #t))

  (define or-truth-table
    '(#f #t #t #t))

  (define xor-truth-table
    '(#f #t #t #f))


  (define (make-gate-test gate name)
    (lambda (input correct)
      (define A (first input))
      (define B (second input))
      (test-equal? name
                   (gate A B)
                   correct)))

  (define and-gate-test
    (make-gate-test and-gate "AND Gate Test"))
  (define or-gate-test
    (make-gate-test or-gate "OR Gate Test"))
  (define xor-gate-test
    (make-gate-test xor-gate "XOR Gate Test"))

  (define (and-tests)
    (define tests
      (make-test-harness and-gate-test))
    (tests gate-inputs and-truth-table))

  (define (or-tests)
    (define tests
      (make-test-harness or-gate-test))
    (tests gate-inputs or-truth-table))

  (define (xor-tests)
    (define tests
      (make-test-harness xor-gate-test))
    (tests gate-inputs xor-truth-table))

  (define-test-suite
    all-gate-tests
    (and-tests)
    (or-tests)
    (xor-tests))

  (run-tests all-gate-tests))
racket@29761897.rkt> ,enter "/home/ben/StackOverflow/29761897.rkt"
12 success(es) 0 failure(s) 0 error(s) 12 test(s) run
0