예제 #1
0
        /// <summary>
        /// Truth tables for logical expressions.
        /// Write a predicate table/3 which prints the truth table of a given logical expression in two variables.
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public static List <(bool, bool, bool)> _3_01(Func <bool, bool, bool> func)
        {
            var booleans = new List <bool> {
                true, false
            };

            return(booleans.SelectMany(a => Lists._1_05(booleans).Select(b => (a, b, func(a, b)))).ToList());
        }
예제 #2
0
        /// <summary>
        /// Gray code.
        /// An n-bit Gray code is a sequence of n-bit strings constructed according to certain rules. For example,
        /// n = 1: C(1) = ['0','1'].
        /// n = 2: C(2) = ['00','01','11','10'].
        /// n = 3: C(3) = ['000','001','011','010','110','111','101','100'].
        /// Find out the construction rules and write a predicate with the following specification:
        /// % gray(N,C) :- C is the N-bit Gray code
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static List <string> _3_04(int number)
        {
            if (number == 1)
            {
                return(new List <string> {
                    "0", "1"
                });
            }

            var left  = _3_04(number - 1);
            var right = Lists._1_05(left);

            return(left.Select(i => "0" + i).Concat(right.Select(i => "1" + i)).ToList());
        }