import { anyToString } from '#src/utils/printers.js'; import { head, tail, nil, cons, isEmpty } from '#src/utils/list.functional.api.js'; let aList = cons(1, cons(2, nil)); console.log(anyToString(aList)); // -> (|1, 2|) console.logA(aList); // -> (|1, 2|) let anotherList = cons(3, tail(aList)); console.logA(anotherList) // -> (|3, 2|) import { node, val, children } from '#src/utils/tree.functional.api.js'; let aTree = node(1, nil); // Simple tree with one node console.logA(aTree); // More complex tree with three nodes let anotherTree = node('A', cons(node('B', nil), cons(node('C', nil), nil))); console.logA(anotherTree); ////////// Exercice 1 ////////// import { hasKey } from '#src/utils/object.js'; hasKey({ name: "Alf" }, "name"); // -> true hasKey({ name: "Alf" }, "cat"); // -> false ////////// Exercice 2 ////////// let aList1 = cons(1, cons(listIota(5,9), cons(4, nil))); // -> (|(|1|), (|5, 6, 7, 8|), 4|) let aList2 = cons(cons(1, nil), cons(cons(2, cons(3, cons(cons(4, nil), nil))), nil)); // -> (|(|1|), (|2, 3, (|4|)|)|) console.log(`Disp aList1 : ${anyToString(aList1)}`); console.log(`Disp aList2 : ${anyToString(aList2)}`); listFlatten(aList1); // -> (|1, 5, 6, 7, 8, 4|) listFlatten(aList2); // -> (|1, 2, 3, 4|) let aList = cons('a', cons('b', cons(cons('c', cons('d', nil)), cons('e', cons('f', nil))))); // -> (|a, b, (|c, d|), e, f|) listReverse(aList); // -> (|f, e, (|d, c|), b, a|) let aList = cons('a', cons(cons('b', cons('a', cons(cons('c', cons('a', nil)), nil))), cons('d', cons('a', nil)))); // -> (|a, (|b, a, (|c, a|), d, a|) countList('a', aList); // -> 4 ////////// Exercice 3 ////////// // A special tree representing the empty binary tree const tnil = node(null, nil); // A function to test if a tree is the empty binary tree function binaryTreeIsEmpty(t) { return t === tnil; } // Returns a string displaying the contents of the binary tree `t` function binaryTreeDisp(t) { function btDisp(t, depth) { const prefix = (depth >= 1) ? (" ".repeat(depth-1) + "⌞") : "" if (binaryTreeIsEmpty(t)) { return `${prefix}{}\n`; } else if (binaryTreeIsEmpty(leftChild(t)) && binaryTreeIsEmpty(rightChild(t))) { return `${prefix}${val(t)}\n`; } else { return `${prefix}${val(t)}\n${btListDisp(children(t), depth+1)}`; } } function btListDisp(tl, depth) { if (isEmpty(tl)) return ""; else if (isEmpty(tail(tl))) return `${btDisp(head(tl), depth)}`; else return `${btDisp(head(tl), depth)}${btListDisp(tail(tl), depth)}`; } return btDisp(t, 0); } ////////// Exercice 4 //////////