zzz6519003 / blog

My blog about coding
4 stars 1 forks source link

Question collection #151

Open zzz6519003 opened 2 years ago

zzz6519003 commented 2 years ago
            Array.prototype._map = (f) => {
                const res = []
                    const len = this.length;

                for (let i = 0; i < len; i++) {
                    console.log(i)
                    res.push(f(this[i]));
                }
                return res;
            }
const a = [1,2,3];
            const newa = a._map(i => i * 3);
            console.log(newa)
            Array.prototype._map = function (f) => {
                const res = []
                    const len = this.length;

                for (let i = 0; i < len; i++) {
                    console.log(i)
                    res.push(f(this[i]));
                }
                return res;
            }
const a = [1,2,3];
            const newa = a._map(i => i * 3);
            console.log(newa)

solved, arrow don't have this...

zzz6519003 commented 2 years ago

14.4.1 Kalotan谜题

Kalotan是一个奇特的部落。这个部落里所有男人都总是讲真话。所有的女人从来不会连续2句讲真话,也不会连续2句都讲假话。

一个哲学家(Worf)开始研究这些人。Worf不懂Kalotan的语言。一天他碰到一对Kalotan夫妻和他们的孩子Kibi。Worf问Kibi:“你是男孩吗?”Kibi用Kalotan语回答,Worf没听懂。

Wrof又问孩子的父母(他们都会说英语),其中一个人说:“Kibi说:‘我是个男孩。’”,另外一个人说:“Kibi是个女孩,Kibi撒谎了”。

请问这三个Kalotan人的性别。

解决的方法包括引进一堆变量,给它们赋上各种可能的值,把所有情况列举为一系列assert表达式。

变量:parent1,parent2,kibi分别是父母(按照说话的顺序)和Kibi的性别。kibi-self-desc是Kibi用Kalotan语说的自己的性别。kibi-lied?表示Kibi是否说谎。

(define solve-kalotan-puzzle
  (lambda ()
    (let ((parent1 (amb 'm 'f))
          (parent2 (amb 'm 'f))
          (kibi (amb 'm 'f))
          (kibi-self-desc (amb 'm 'f))
          (kibi-lied? (amb #t #f)))
      (assert
       (distinct? (list parent1 parent2)))
      (assert
       (if (eqv? kibi 'm)
           (not kibi-lied?)))
      (assert
       (if kibi-lied?
           (xor
            (and (eqv? kibi-self-desc 'm)
                 (eqv? kibi 'f))
            (and (eqv? kibi-self-desc 'f)
                 (eqv? kibi 'm)))))
      (assert
       (if (not kibi-lied?)
           (xor
            (and (eqv? kibi-self-desc 'm)
                 (eqv? kibi 'm))
            (and (eqv? kibi-self-desc 'f)
                 (eqv? kibi 'f)))))
      (assert
       (if (eqv? parent1 'm)
           (and
            (eqv? kibi-self-desc 'm)
            (xor
             (and (eqv? kibi 'f)
                  (eqv? kibi-lied? #f))
             (and (eqv? kibi 'm)
                  (eqv? kibi-lied? #t))))))
      (assert
       (if (eqv? parent1 'f)
           (and
            (eqv? kibi 'f)
            (eqv? kibi-lied? #t))))
      (list parent1 parent2 kibi))))