CINXE.COM

Exercises

<!DOCTYPE html> <html lang="en"> <head id="htmx-head"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content="A list of exercises to work on your OCaml skills."> <meta name="twitter:title" content="Exercises"> <meta name="twitter:description" content="A list of exercises to work on your OCaml skills."> <meta name="twitter:image" content="https://ocaml.org/_/MWIyY2ZmMWM5YzdkYWNmYWI4NGQ0MDBjOGFiZTYxOTg/ocaml_org_social_media.png"> <meta property="og:site_name" content="OCaml"> <meta property="og:type" content="object"> <meta property="og:title" content="Exercises"> <meta property="og:description" content="A list of exercises to work on your OCaml skills."> <meta name="og:image" content="https://ocaml.org/_/MWIyY2ZmMWM5YzdkYWNmYWI4NGQ0MDBjOGFiZTYxOTg/ocaml_org_social_media.png"> <meta name="theme-color" content="#fff" > <meta name="color-scheme" content="white"> <meta name="ahrefs-site-verification" content="6ff715b377cdcd566334b44ae8888791189ce24640c8a403eacdc3bcbaa9449b"> <link rel="canonical" href="https://ocaml.org/exercises"> <link rel="icon" type="image/x-icon" href="/_/ZDJmMjgzN2NkZmJlMzgxNGQxMTMxNGVlMzk1NzZkN2I/favicon.ico"> <link rel="manifest" href="/manifest.json"> <link rel="stylesheet" href="/_/M2QzNTJkMGFlMzUxZDFkZDE4MDdjYzljNThkN2U1YjQ/css/main.css"> <link rel="stylesheet" href="/_/NmFjYjY5ZmIxYWRhMWQxMTQxODM4ZDEwYTgwMDcwNmI/vendors/font-files/inter.css"> <link rel="stylesheet" href="/_/YjM3OGY4YzIwN2E2ZmUxZTczOWVkNDQwZWRhMDU0MGM/vendors/font-files/roboto-mono.css"> <script defer src="/_/OWYyZDQyZjg4MTFlNWEzZjE4ZGY0YWM0OTU1NGEzYTI/vendors/alpine-clipboard.js"></script> <script defer src="/_/NGI1NjQzZjdkODY0MTY2MmViNThhM2VlZTYwMDk3NTI/vendors/alpine.min.js"></script> <script defer src="/_/YTNhODI2Yzg0Yzk4M2ZiNDcxYmRiYzdhY2FiN2Y0OWQ/vendors/htmx.min.js"></script> <script defer data-domain="ocaml.org" src="https://plausible.ci.dev/js/script.js"></script> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="OCaml"> <title>Exercises</title> </head> <body> <script> if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches)) { document.body.classList.add("dark"); } else { document.body.classList.remove("dark"); } </script> <header class="h-20 flex items-center bg-sand dark:bg-dark-background_navigation" x-data="{ open: false }"> <nav class="container-fluid wide header flex justify-between items-center gap-5 xl:gap-8"> <ul class="order-0 space space-x-5 xl:space-x-8 items-center flex text-content font-medium dark:text-title dark:text-opacity-60 dark:font-semibold"> <li style="width:132px"> <a href="/" class="block pb-2"> <img src="/_/MDBjN2M4YjIxY2Y1YWExZWE3Njk4M2FiZTg1NWZjNTM/logo-with-name.svg" width="132" alt="OCaml logo" class="dark:hidden"> <img src="/_/MTE3YWE5YzJlZmExNmYyMmI3ZmEwYjliMDEwNDBkMDY/logo-with-name-white.svg" width="132" alt="OCaml logo" class="hidden dark:inline"> </a> </li> </ul> <ul class="order-2 hidden lg:flex items-center"> <li> <form x-data="{ row: null, col: 0, max: 0, total: 0 }" @submit="if (row !== null) { window.location = document.getElementById('package-autocomplete-'+row+'-'+col).getAttribute('href'); $event.stopPropagation(); $event.preventDefault(); return false }" action="/packages/search" method="GET"> <div class="dropdown-container flex items-center justify-center h-10 rounded-md focus-within:outline-primary_25 dark:focus-within:outline-dark-primary_40 focus-within:outline focus-within:outline-2 lg:w-56 xl:w-80" tabindex="0"> <label for="q" class="sr-only">Search OCaml packages</label> <input type="search" name="q" placeholder="Search OCaml packages" @keydown.escape="$event.target.blur()" class="bg-white dark:bg-dark-card text-title dark:text-dark-title h-full w-full font-normal focus:border-primary dark:focus:border-dark-primary focus:ring-0 border-primary dark:border-dark-primary rounded-md rounded-r-none px-3 py-1 placeholder-text-content dark:placeholder:text-dark-content appearance-none focus:outline-none" @keydown.stop @keyup.down="if (row === null) { row = 0; col = 0; } else { row +=1; if (row > max) { row = max } }" @keyup.up="if (row !== null) { row -=1; if (row < 0) { row = null } }" @keyup.right="if (col < 1) col++" @keyup.left="if (col >= 1) col--" :aria-activedescendant="row !== null ? 'package-autocomplete-'+row+'-'+col : null" hx-get="/packages/autocomplete" hx-params="q" hx-trigger="keyup changed, search" hx-target="#header-search-results" hx-indicator="#header-search-indicator" autocomplete="off" > <button aria-label="search" class="h-full flex items-center justify-center rounded-r-md bg-primary dark:bg-dark-primary text-white dark:text-dark-white px-4" type="submit" ><svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /> </svg> </button> <div class="dropdown w-full lg:w-[32rem] xl:w-[32rem] z-10 absolute rounded-md mt-12 shadow-2xl top-1 p-2 bg-background dark:bg-dark-background shadow-3xl dark:outline dark:outline-1 dark:outline-primary"> <div id="header-search-results" aria-live="polite"></div> <a class="flex py-2 px-2 mx-2 gap-4 hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 font-normal rounded-md text-primary dark:text-dark-primary" href="/api"> Standard Library API <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"></path> </svg> </a> </div> </div> </form> </li> </ul> <ul class="order-1 mr-auto items-center hidden lg:flex font-medium dark:text-white dark:text-opacity-60 dark:font-semibold"> <li><a href="/docs" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primary text-primary dark:text-dark-primary underline">Learn</a> </li> <li><a href="/tools" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primary text-title dark:text-dark-title">Tools</a> </li> <li><a href="/packages" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primary text-title dark:text-dark-title">Packages</a> </li> <li><a href="/community" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primary text-title dark:text-dark-title">Community</a> </li> <li><a href="/changelog" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primary text-title dark:text-dark-title">News</a> </li> <li><a href="/play" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primary text-title dark:text-dark-title">Play</a> </li> </ul> <ul class="order-3 hidden lg:flex items-center"> <li><a href="/docs/get-started" class="border border-primary dark:border-dark-primary text-primary dark:text-dark-primary font-bold py-2.5 px-7 whitespace-nowrap rounded">Get Started</a></li> </ul> <ul class="order-1 lg:hidden flex items-center"> <li class="h-12 w-12 hover:bg-primary_25 dark:hover:bg-primary_20 flex items-center justify-center rounded-full text-content dark:text-dark-title"> <button aria-label="open menu" @click="open = ! open"> <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 10h16M4 14h16M4 18h16" /> </svg> </button> </li> </ul> </nav> <div class="bg-black fixed w-full h-full left-0 top-0 opacity-60 z-40" x-show='open' x-cloak></div> <nav class="z-50 h-full fixed right-0 top-0 max-w-full w-96 bg-background dark:bg-dark-background shadow-lg" x-show="open" x-cloak @click.away="open = false" x-transition:enter="transition duration-200 ease-out" x-transition:enter-start="translate-x-full" x-transition:leave="transition duration-100 ease-in" x-transition:leave-end="translate-x-full"> <ul class="text-content p-6 font-semibold"> <li class="flex justify-between items-center"> <a href="/"> <img src="/_/MDBjN2M4YjIxY2Y1YWExZWE3Njk4M2FiZTg1NWZjNTM/logo-with-name.svg" width="132" alt="OCaml logo" class="dark:hidden"> <img src="/_/MTE3YWE5YzJlZmExNmYyMmI3ZmEwYjliMDEwNDBkMDY/logo-with-name-white.svg" width="132" alt="OCaml logo" class="hidden dark:inline"> </a> <div class="" x-on:click="open = false"> <button aria-label="close" class="text-content dark:text-dark-title"> <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" /> </svg> </button> </div> </li> <li class="mt-6 mb-3"> <form action="/packages/search" method="GET"> <div class="dropdown-container flex items-center justify-center h-10 rounded-md focus-within:outline-primary_25 dark:focus-within:outline-dark-primary_40 focus-within:outline focus-within:outline-2 " tabindex="0"> <label for="q" class="sr-only">Search OCaml packages</label> <input type="search" name="q" placeholder="Search OCaml packages" @keydown.escape="$event.target.blur()" class="bg-white dark:bg-dark-card text-title dark:text-dark-title h-full w-full font-normal focus:border-primary dark:focus:border-dark-primary focus:ring-0 border-primary dark:border-dark-primary rounded-md rounded-r-none px-3 py-1 placeholder-text-content dark:placeholder:text-dark-content appearance-none focus:outline-none" > <button aria-label="search" class="h-full flex items-center justify-center rounded-r-md bg-primary dark:bg-dark-primary text-white dark:text-dark-white px-4" type="submit" ><svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /> </svg> </button> </div> </form> </li> <li><a href="/docs" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primaryblock text-primary dark:text-dark-primary underline">Learn</a> </li> <li><a href="/tools" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primaryblock text-title dark:text-dark-title">Tools</a> </li> <li><a href="/packages" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primaryblock text-title dark:text-dark-title">Packages</a> </li> <li><a href="/community" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primaryblock text-title dark:text-dark-title">Community</a> </li> <li><a href="/changelog" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primaryblock text-title dark:text-dark-title">News</a> </li> <li><a href="/play" class="block font-normal py-3 mg:py-4 px-1 lg:px-3 hover:text-primary dark:hover:text-dark-primaryblock text-title dark:text-dark-title">Play</a> </li> <li> <a href="/api" class="flex py-3 px-1 gap-4 font-semibold text-primary dark:text-dark-primary">Standard Library API<svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"></path> </svg> </a> </li> <li class="mt-3 mb-6"> <a href="/docs/get-started" class="w-full rounded font-normal py-3 px-7 flex items-center justify-center bg-primary dark:bg-dark-primary text-white dark:text-dark-title">Get started</a> </li> <li> <div class="space-x-6 text-2xl flex items-center"> <a aria-label="OCaml's Discord" href="https://discord.gg/cCYQbqN" class="opacity-60 hover:opacity-100 text-content dark:text-dark-title hover:text-primary dark:hover:text-dark-primary"> <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"> <path fill-rule="evenodd" d="M18.9419 5.29661C17.6473 4.69088 16.263 4.25066 14.8157 4C14.638 4.32134 14.4304 4.75355 14.2872 5.09738C12.7487 4.86601 11.2245 4.86601 9.7143 5.09738C9.57116 4.75355 9.3588 4.32134 9.17947 4C7.73067 4.25066 6.3448 4.6925 5.05016 5.29982C2.43887 9.24582 1.73099 13.0938 2.08493 16.8872C3.81688 18.1805 5.49534 18.9662 7.14548 19.4804C7.55291 18.9196 7.91628 18.3235 8.22931 17.6953C7.63313 17.4688 7.06211 17.1892 6.52256 16.8647C6.6657 16.7586 6.80571 16.6478 6.94098 16.5337C10.2318 18.0729 13.8074 18.0729 17.0589 16.5337C17.1958 16.6478 17.3358 16.7586 17.4774 16.8647C16.9362 17.1908 16.3637 17.4704 15.7675 17.697C16.0805 18.3235 16.4423 18.9212 16.8513 19.4819C18.503 18.9678 20.183 18.1822 21.915 16.8872C22.3303 12.4897 21.2056 8.67705 18.9419 5.29661ZM8.67765 14.5543C7.68977 14.5543 6.87963 13.632 6.87963 12.509C6.87963 11.3859 7.67247 10.4621 8.67765 10.4621C9.68285 10.4621 10.493 11.3843 10.4757 12.509C10.4772 13.632 9.68285 14.5543 8.67765 14.5543ZM15.3223 14.5543C14.3344 14.5543 13.5243 13.632 13.5243 12.509C13.5243 11.3859 14.3171 10.4621 15.3223 10.4621C16.3275 10.4621 17.1376 11.3843 17.1203 12.509C17.1203 13.632 16.3275 14.5543 15.3223 14.5543Z" clip-rule="evenodd" /> </svg> </a> <a aria-label="The OCaml Compiler on GitHub" href="https://github.com/ocaml/ocaml" class="opacity-60 hover:opacity-100 text-content dark:text-dark-title hover:text-primary dark:hover:text-dark-primary"> <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"> <path fill-rule="evenodd" d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z" clip-rule="evenodd" /> </svg> </a> <a aria-label="The OCaml Language Twitter Account" href="https://twitter.com/ocaml_org" class="opacity-60 hover:opacity-100 text-content dark:text-dark-title hover:text-primary dark:hover:text-dark-primary"> <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"> <path d="M8.29 20.251c7.547 0 11.675-6.253 11.675-11.675 0-.178 0-.355-.012-.53A8.348 8.348 0 0022 5.92a8.19 8.19 0 01-2.357.646 4.118 4.118 0 001.804-2.27 8.224 8.224 0 01-2.605.996 4.107 4.107 0 00-6.993 3.743 11.65 11.65 0 01-8.457-4.287 4.106 4.106 0 001.27 5.477A4.072 4.072 0 012.8 9.713v.052a4.105 4.105 0 003.292 4.022 4.095 4.095 0 01-1.853.07 4.108 4.108 0 003.834 2.85A8.233 8.233 0 012 18.407a11.616 11.616 0 006.29 1.84" /> </svg> </a> </div> </li> </ul> </nav> </header> <script> window.addEventListener('keydown', function(event) { if (event.key === "/") { event.preventDefault(); document.querySelector("input[type='search']").focus() } }) </script> <main class="bg-background dark:bg-dark-background"><nav aria-label="breadcrumbs" class="px-4 flex bg-title dark:bg-[#111827] text-white dark:text-dark-title md:hidden"> <ul> <li class="inline-block"> <a href="/docs" class="flex items-center px-2 py-2 border-transparent border-2 border-b-4"> Learn <span> <svg xmlns="http://www.w3.org/2000/svg" class="w-3 h-3 ml-2" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" /> </svg> </span> </a> </li> <li class="inline-block"> <select onchange="location = this.value;" class="appearance-none border-2 border-b-4 bg-transparent bg-none font-bold border-none w-auto p-0 m-0 cursor-pointer focus:outline-none focus:ring-0"> <option value="/docs" > Overview </option> <option value="/docs/get-started" > Get Started </option> <option value="/docs/language" > Language </option> <option value="/docs/tools" > Platform </option> <option value="/docs/guides" > Guides </option> <option value="/cookbook" > Cookbook </option> <option value="/exercises" selected> Exercises </option> <option value="/books" > Books </option> </select> <span class="text-primary dark:text-dark-primary cursor-pointer">&#x25BE;</span> </li> </ul> </nav> <div class="bg-title dark:bg-[#111827] hidden md:flex"> <nav class="container-fluid wide flex flex-wrap"> <a href="/docs" class="justify-start px-4 py-2 text-white dark:text-dark-title items-center font-normal border-2 border-b-4 border-transparent rounded opacity-80 hover:text-primary dark:hover:text-dark-primary"> Overview </a> <a href="/docs/get-started" class="justify-start px-4 py-2 text-white dark:text-dark-title items-center font-normal border-2 border-b-4 border-transparent rounded opacity-80 hover:text-primary dark:hover:text-dark-primary"> Get Started </a> <a href="/docs/language" class="justify-start px-4 py-2 text-white dark:text-dark-title items-center font-normal border-2 border-b-4 border-transparent rounded opacity-80 hover:text-primary dark:hover:text-dark-primary"> Language </a> <a href="/docs/tools" class="justify-start px-4 py-2 text-white dark:text-dark-title items-center font-normal border-2 border-b-4 border-transparent rounded opacity-80 hover:text-primary dark:hover:text-dark-primary"> Platform </a> <a href="/docs/guides" class="justify-start px-4 py-2 text-white dark:text-dark-title items-center font-normal border-2 border-b-4 border-transparent rounded opacity-80 hover:text-primary dark:hover:text-dark-primary"> Guides </a> <a href="/cookbook" class="justify-start px-4 py-2 text-white dark:text-dark-title items-center font-normal border-2 border-b-4 border-transparent rounded opacity-80 hover:text-primary dark:hover:text-dark-primary"> Cookbook </a> <a href="/exercises" class="justify-start px-4 py-2 text-white dark:text-dark-title items-center font-normal border-2 border-b-4 border-transparent rounded border-b-primary dark:border-b-dark-primary"> Exercises </a> <a href="/books" class="justify-start px-4 py-2 text-white dark:text-dark-title items-center font-normal border-2 border-b-4 border-transparent rounded opacity-80 hover:text-primary dark:hover:text-dark-primary"> Books </a> </nav> </div> <div class="bg-background dark:bg-dark-background" x-data="{ sidebar: window.matchMedia('(min-width: 64em)').matches, showOnMobile: false}" @resize.window="sidebar = window.matchMedia('(min-width: 64em)').matches" x-on:close-sidebar="sidebar=window.matchMedia('(min-width: 64em)').matches"> <button :title='(sidebar? "close" : "open")+" sidebar"' class="flex items-center bg-[#D54000] p-4 z-30 rounded-full text-white shadow-custom bottom-24 md:bottom-36 fixed lg:hidden right-10" x-on:click="sidebar = ! sidebar"> <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="currentColor" viewBox="0 0 24 24" > <path d="M14.25 14.375H19.875C20.4962 14.375 21 13.8712 21 13.25V9.875C21 9.25377 20.4962 8.75 19.875 8.75H14.25C13.6288 8.75 13.125 9.25377 13.125 9.875V11H7.5V7.625H9.75C10.3712 7.625 10.875 7.12122 10.875 6.5V3.125C10.875 2.50378 10.3712 2 9.75 2H4.125C3.50378 2 3 2.50378 3 3.125V6.5C3 7.12122 3.50378 7.625 4.125 7.625H6.375V19.4375C6.375 19.7482 6.62677 20 6.9375 20H13.125V21.125C13.125 21.7462 13.6288 22.25 14.25 22.25H19.875C20.4962 22.25 21 21.7462 21 21.125V17.75C21 17.1288 20.4962 16.625 19.875 16.625H14.25C13.6288 16.625 13.125 17.1288 13.125 17.75V18.875H7.5V12.125H13.125V13.25C13.125 13.8712 13.6288 14.375 14.25 14.375ZM14.25 9.875H19.875V13.25H14.25V9.875ZM4.125 6.5V3.125H9.75V6.5H4.125ZM14.25 17.75H19.875V21.125H14.25V17.75Z"/> </svg> </button> <div class="fixed z-10 inset-0 bg-tertiary_25 backdrop-blur-sm lg:hidden" :class='sidebar ? "" : "hidden"' aria-hidden="true" x-on:click="sidebar = ! sidebar"></div> <div class="container-fluid wide pt-10"> <div class="flex flex-col lg:flex-row lg:gap-6 xl:gap-12"> <div class="z-20 bg-background dark:bg-dark-background flex-col fixed h-screen overflow-auto lg:flex left-0 top-0 lg:sticky p-6 lg:px-0 w-80 lg:w-72 lg:px-0 lg:pt-6 lg:pb-72" x-show="sidebar" x-transition:enter="transition duration-200 ease-out" x-transition:enter-start="-translate-x-full" x-transition:leave="transition duration-100 ease-in" x-transition:leave-end="-translate-x-full"> <div x-data='exercises' @scroll.window="scrollY = window.scrollY" @resize.window="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)" x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)" class="pr-5" > <p class="font-normal uppercase mb-4 text-title dark:text-dark-title">Exercise Categories</p> <hr class="border-separator_20 dark:border-dark-title"> <details class="flex flex-col"> <summary class="flex gap-4 justify-between text-sm text-title dark:text-dark-title font-normal py-4 focus:text-primary dark:focus:text-dark-primary active:text-primary dark:active:text-dark-primary hover:text-primary dark:hover:text-dark-primary"> Lists <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" /> </svg> </summary> <span class="border-l border-separator_20 dark:border-dark-card"><a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#1", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#1"> Tail of a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#2", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#2"> Last Two Elements of a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#3", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#3"> N&#x27;th Element of a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#4", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#4"> Length of a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#5", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#5"> Reverse a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#6", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#6"> Palindrome </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#7", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#7"> Flatten a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#8", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#8"> Eliminate Duplicates </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#9", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#9"> Pack Consecutive Duplicates </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#10", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#10"> Run-Length Encoding </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#11", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#11"> Modified Run-Length Encoding </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#12", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#12"> Decode a Run-Length Encoded List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#13", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#13"> Run-Length Encoding of a List (Direct Solution) </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#14", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#14"> Duplicate the Elements of a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#15", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#15"> Replicate the Elements of a List a Given Number of Times </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#16", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#16"> Drop Every N&#x27;th Element From a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#17", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#17"> Split a List Into Two Parts; The Length of the First Part Is Given </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#18", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#18"> Extract a Slice From a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#19", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#19"> Rotate a List N Places to the Left </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#20", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#20"> Remove the K&#x27;th Element From a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#21", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#21"> Insert an Element at a Given Position Into a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#22", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#22"> Create a List Containing All Integers Within a Given Range </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#23", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#23"> Extract a Given Number of Randomly Selected Elements From a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#24", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#24"> Lotto: Draw N Different Random Numbers From the Set 1..M </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#25", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#25"> Generate a Random Permutation of the Elements of a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#26", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#26"> Generate the Combinations of K Distinct Objects Chosen From the N Elements of a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#27", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#27"> Group the Elements of a Set Into Disjoint Subsets </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#28", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#28"> Sorting a List of Lists According to Length of Sublists </a> </span> </details> <hr class="border-separator_20 dark:border-dark-title"> <details class="flex flex-col"> <summary class="flex gap-4 justify-between text-sm text-title dark:text-dark-title font-normal py-4 focus:text-primary dark:focus:text-dark-primary active:text-primary dark:active:text-dark-primary hover:text-primary dark:hover:text-dark-primary"> Arithmetic <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" /> </svg> </summary> <span class="border-l border-separator_20 dark:border-dark-card"><a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#31", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#31"> Determine Whether a Given Integer Number Is Prime </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#32", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#32"> Determine the Greatest Common Divisor of Two Positive Integer Numbers </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#33", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#33"> Determine Whether Two Positive Integer Numbers Are Coprime </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#34", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#34"> Calculate Euler&#x27;s Totient Function Φ(m) </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#35", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#35"> Determine the Prime Factors of a Given Positive Integer </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#36", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#36"> Determine the Prime Factors of a Given Positive Integer (2) </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#37", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#37"> Calculate Euler&#x27;s Totient Function Φ(m) (Improved) </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#38", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#38"> Compare the Two Methods of Calculating Euler&#x27;s Totient Function </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#39", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#39"> A List of Prime Numbers </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#40", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#40"> Goldbach&#x27;s Conjecture </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#41", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#41"> A List of Goldbach Compositions </a> </span> </details> <hr class="border-separator_20 dark:border-dark-title"> <details class="flex flex-col"> <summary class="flex gap-4 justify-between text-sm text-title dark:text-dark-title font-normal py-4 focus:text-primary dark:focus:text-dark-primary active:text-primary dark:active:text-dark-primary hover:text-primary dark:hover:text-dark-primary"> Logic and Codes <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" /> </svg> </summary> <span class="border-l border-separator_20 dark:border-dark-card"><a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#46", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#46"> Truth Tables for Logical Expressions (2 Variables) </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#48", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#48"> Truth Tables for Logical Expressions </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#49", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#49"> Gray Code </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#50", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#50"> Huffman Code </a> </span> </details> <hr class="border-separator_20 dark:border-dark-title"> <details class="flex flex-col"> <summary class="flex gap-4 justify-between text-sm text-title dark:text-dark-title font-normal py-4 focus:text-primary dark:focus:text-dark-primary active:text-primary dark:active:text-dark-primary hover:text-primary dark:hover:text-dark-primary"> Binary Trees <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" /> </svg> </summary> <span class="border-l border-separator_20 dark:border-dark-card"><a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#55", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#55"> Construct Completely Balanced Binary Trees </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#56", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#56"> Symmetric Binary Trees </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#57", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#57"> Binary Search Trees (Dictionaries) </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#58", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#58"> Generate-and-Test Paradigm </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#59", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#59"> Construct Height-Balanced Binary Trees </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#60", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#60"> Construct Height-Balanced Binary Trees With a Given Number of Nodes </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#61A", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#61A"> Count the Leaves of a Binary Tree </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#61B", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#61B"> Collect the Leaves of a Binary Tree in a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#62A", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#62A"> Collect the Internal Nodes of a Binary Tree in a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#62B", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#62B"> Collect the Nodes at a Given Level in a List </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#63", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#63"> Construct a Complete Binary Tree </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#64", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#64"> Layout a Binary Tree (1) </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#65", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#65"> Layout a Binary Tree (2) </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#66", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#66"> Layout a Binary Tree (3) </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#67", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#67"> A String Representation of Binary Trees </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#68", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#68"> Preorder and Inorder Sequences of Binary Trees </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#69", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#69"> Dotstring Representation of Binary Trees </a> </span> </details> <hr class="border-separator_20 dark:border-dark-title"> <details class="flex flex-col"> <summary class="flex gap-4 justify-between text-sm text-title dark:text-dark-title font-normal py-4 focus:text-primary dark:focus:text-dark-primary active:text-primary dark:active:text-dark-primary hover:text-primary dark:hover:text-dark-primary"> Multiway Trees <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" /> </svg> </summary> <span class="border-l border-separator_20 dark:border-dark-card"><a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#70A", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#70A"> Tree Construction From a Node String </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#70B", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#70B"> Count the Nodes of a Multiway Tree </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#71", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#71"> Determine the Internal Path Length of a Tree </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#72", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#72"> Construct the Bottom-Up Order Sequence of the Tree Nodes </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#73", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#73"> Lisp-Like Tree Representation </a> </span> </details> <hr class="border-separator_20 dark:border-dark-title"> <details class="flex flex-col"> <summary class="flex gap-4 justify-between text-sm text-title dark:text-dark-title font-normal py-4 focus:text-primary dark:focus:text-dark-primary active:text-primary dark:active:text-dark-primary hover:text-primary dark:hover:text-dark-primary"> Graphs <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" /> </svg> </summary> <span class="border-l border-separator_20 dark:border-dark-card"><a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#80", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#80"> Conversions </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#81", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#81"> Path From One Node to Another One </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#82", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#82"> Cycle From a Given Node </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#83", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#83"> Construct All Spanning Trees </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#84", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#84"> Construct the Minimal Spanning Tree </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#85", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#85"> Graph Isomorphism </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#86", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#86"> Node Degree and Graph Coloration </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#87", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#87"> Depth-First Order Graph Traversal </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#88", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#88"> Connected Components </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#89", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#89"> Bipartite Graphs </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#90", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#90"> Generate K-Regular Simple Graphs With N Nodes </a> </span> </details> <hr class="border-separator_20 dark:border-dark-title"> <details class="flex flex-col"> <summary class="flex gap-4 justify-between text-sm text-title dark:text-dark-title font-normal py-4 focus:text-primary dark:focus:text-dark-primary active:text-primary dark:active:text-dark-primary hover:text-primary dark:hover:text-dark-primary"> Miscellaneous <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" /> </svg> </summary> <span class="border-l border-separator_20 dark:border-dark-card"><a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#91", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#91"> Eight Queens Problem </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#92", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#92"> Knight&#x27;s Tour </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#93", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#93"> Von Koch&#x27;s Conjecture </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#94", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#94"> An Arithmetic Puzzle </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#95", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#95"> English Number Words </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#96", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#96"> Syntax Checker </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#97", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#97"> Sudoku </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#98", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#98"> Nonograms </a> <a class="block text-title dark:text-dark-title border-l py-3 px-5 font-normal text-sm rounded-sm" :class='isWithin(scrollY, "#99", sectionYPositions) ? "bg-primary_nav_block_hover_10 dark:bg-dark-primary_nav_block_hover_10 border-primary dark:border-dark-primary" : "hover:bg-primary_nav_block_hover_10 dark:hover:bg-dark-primary_nav_block_hover_10 hover:border-primary dark:hover:border-dark-primary" ' href="#99"> Crossword Puzzle </a> </span> </details> </div> </div> <div class="flex-1 z-0 z- min-w-0 lg:pt-6 "> <div class="prose prose-orange dark:prose-invert max-w-full"> <h1 class="font-normal text-4xl text-title mb-8">Exercises</h1> <div class="prose prose-orange dark:prose-invert max-w-full mb-5"> <p> This section is inspired by <a href="https://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html"> Ninety-Nine Lisp Problems</a> which in turn was based on “<a href="https://sites.google.com/site/prologsite/prolog-problems/">Prolog problem list</a>” by Werner Hett. For each of these questions, some simple tests are shown—they may also serve to make the question clearer if needed. To work on these problems, we recommend you first <a href="/docs/get-started">install OCaml</a> or use it <a href="/play">inside your browser</a>. The source of the following problems is available on <a href="https://github.com/ocaml/ocaml.org/tree/main/data/exercises">GitHub</a>. </p> <p> Every exercise has a difficulty level, ranging from beginner to advanced. </p> </div> <div class="leading-5 py-8 text-zinc-800"> <form action="/exercises" method="GET"> <label for="difficulty_level" class="sr-only">Difficulty</label> <select class="w-full appearance-none text-title dark:text-dark-white border rounded-lg py-3 pl-6 border-primary dark:border-dark-primary bg-white dark:bg-dark-card pr-14 h-12 outline-none focus:ring-2 ring-separator_20 dark:ring-dark-separator_30 focus:ring-primary dark:focus:ring-dark-primary w-48" id="difficulty_level" name="difficulty_level" onchange="this.form.submit()" > <option value="All" selected > Select Level </option> <option value="beginner" > Beginner </option> <option value="intermediate" > Intermediate </option> <option value="advanced" > Advanced </option> </select> </form> </div> <div x-data="{statement: true}" id="1" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Tail of a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Write a function <code>last : 'a list -&gt; 'a option</code> that returns the last element of a list</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>last</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>option</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>last</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>option</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>last</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>last</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>last</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>option</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="2" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Last Two Elements of a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Find the last two (last and penultimate) elements of a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>last_two</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>option</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>last_two</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>option</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>last_two</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>last_two</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>last_two</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>option</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="3" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> N&#x27;th Element of a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Find the N'th element of a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>at</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>option</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>at</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>option</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span></code></pre> <p><strong>Remark:</strong> OCaml has <code>List.nth</code> which numbers elements from <code>0</code> and raises an exception if the index is out of bounds.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>nth</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>nth</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Exception</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Failure</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>nth</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>at</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>at</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>at</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>option</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="4" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Length of a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Find the number of elements of a list.</p> <p>OCaml standard library has <code>List.length</code> but we ask that you reimplement it. Bonus for a <a href="http://en.wikipedia.org/wiki/Tail_call">tail recursive</a> solution.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><p>This function is tail-recursive: it uses a constant amount of stack memory regardless of list size.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="5" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Reverse a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Reverse a list.</p> <p>OCaml standard library has <code>List.rev</code> but we ask that you reimplement it.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="6" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Palindrome </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Find out whether a list is a palindrome.</p> <p><strong>Hint:</strong> A palindrome is its own reverse.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_palindrome</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>x</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>m</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>x</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>not</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>is_palindrome</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>is_palindrome</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> One can use either the rev function from the previous problem, or the built-in List.rev </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_palindrome</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="7" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Flatten a List </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Flatten a nested list structure.</p> <pre><code><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>node</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>node</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span></code></pre> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>flatten</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>node</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>node</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>node</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>node</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> This function traverses the list, prepending any encountered elements </span><span class='ocaml-comment-block'> to an accumulator, which flattens the list in inverse order. It can </span><span class='ocaml-comment-block'> then be reversed to obtain the actual flattened list. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>flatten</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>flatten</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>node</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="8" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Eliminate Duplicates </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Eliminate consecutive duplicates of list elements.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>compress</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>compress</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>compress</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>compress</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>smaller</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>smaller</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>compress</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="9" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Pack Consecutive Duplicates </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Pack consecutive duplicates of list elements into sublists.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>pack</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>pack</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>current</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Can only be reached if original list is empty </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>x</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>current</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>current</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>current</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>pack</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="10" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Run-Length Encoding </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>If you need so, refresh your memory about <a href="http://en.wikipedia.org/wiki/Run-length_encoding">run-length encoding</a>.</p> <p>Here is an example:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>encode</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>encode</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Can only be reached if original list is empty </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>x</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>encode</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>An alternative solution, which is shorter but requires more memory, is to use the <code>pack</code> function declared in problem 9:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>pack</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>current</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Can only be reached if original list is empty </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>x</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>current</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>current</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>current</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>pack</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>encode</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>hd</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>pack</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>encode</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="11" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Modified Run-Length Encoding </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Modify the result of the previous problem in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N E) lists.</p> <p>Since OCaml lists are homogeneous, one needs to define a type to hold both single elements and sub-lists.</p> <!-- $MDX skip --> <pre><code><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span></code></pre> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>encode</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>encode</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>create_tuple</span><span class='ocaml-source'> </span><span class='ocaml-source'>cnt</span><span class='ocaml-source'> </span><span class='ocaml-source'>elem</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>cnt</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-source'>elem</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>cnt</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>elem</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>x</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>create_tuple</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>hd</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>snd</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>hd</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>snd</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>create_tuple</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>hd</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>encode</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="12" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Decode a Run-Length Encoded List </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Given a run-length code list generated as specified in the previous problem, construct its uncompressed version.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>decode</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>decode</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>many</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>many</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>decode</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="13" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Run-Length Encoding of a List (Direct Solution) </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Implement the so-called run-length encoding data compression method directly. I.e. don't explicitly create the sublists containing the duplicates, as in problem &quot;<a href="#9">Pack consecutive duplicates of list elements into sublists</a>&quot;, but only count them. As in problem &quot;<a href="#10">Modified run-length encoding</a>&quot;, simplify the result list by replacing the singleton lists (1 X) by X.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>encode</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>encode</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>One</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Many</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Can only be reached if original list is empty </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>x</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>encode</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="14" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Duplicate the Elements of a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Duplicate the elements of a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>duplicate</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>duplicate</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>duplicate</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>duplicate</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <blockquote> <p>Remark: this function is not tail recursive. Can you modify it so it becomes so?</p> </blockquote> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="15" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Replicate the Elements of a List a Given Number of Times </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Replicate the elements of a list a given number of times.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>replicate</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>replicate</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>prepend</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>prepend</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>prepend</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> This could also be written as: </span><span class='ocaml-comment-block'> List.fold_left (prepend n) [] (List.rev list) </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>replicate</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <blockquote> <p>Note that <code>List.rev list</code> is needed only because we want <code>aux</code> to be <a href="http://en.wikipedia.org/wiki/Tail_call">tail recursive</a>.</p> </blockquote> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="16" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Drop Every N&#x27;th Element From a List </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Drop every N'th element from a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>drop</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>i</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>j</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>j</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>drop</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>drop</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="17" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Split a List Into Two Parts; The Length of the First Part Is Given </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Split a list into two parts; the length of the first part is given.</p> <p>If the length of the first part is longer than the entire list, then the first part is the list and the second part is empty.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>split</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>i</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>j</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>i</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>j</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>split</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>split</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>split</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="18" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Extract a Slice From a List </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Given two indices, <code>i</code> and <code>k</code>, the slice is the list containing the elements between the <code>i</code>'th and <code>k</code>'th element of the original list (both limits included). Start counting the elements with 0 (this is the way the <code>List</code> module numbers elements).</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>slice</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>i</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>j</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>slice</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>take</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>take</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>drop</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>drop</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>take</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>drop</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>slice</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>This solution has a drawback, namely that the <code>take</code> function is not <a href="https://en.wikipedia.org/wiki/Tail_call">tail recursive</a> so it may exhaust the stack when given a very long list. You may also notice that the structure of <code>take</code> and <code>drop</code> is similar and you may want to abstract their common skeleton in a single function. Here is a solution.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>fold_until</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>acc</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>acc</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>fold_until</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>slice</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>fold_until</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>taken</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>fold_until</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>taken</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>fold_until</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>slice</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="19" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Rotate a List N Places to the Left </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Rotate a list N places to the left.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>rotate</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>split</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>rotate</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Compute a rotation value between 0 and len - 1 </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>split</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>split</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>rotate</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="20" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Remove the K&#x27;th Element From a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Remove the K'th element from a list.</p> <p>The first element of the list is numbered 0, the second 1,...</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>remove_at</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>remove_at</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>remove_at</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>remove_at</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="21" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Insert an Element at a Given Position Into a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Start counting list elements with 0. If the position is larger or equal to the length of the list, insert the element at the end. (The behavior is unspecified if the position is negative.)</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>insert_at</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>alfa</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>alfa</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>insert_at</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>x</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>insert_at</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>insert_at</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="22" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Create a List Containing All Integers Within a Given Range </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>If first argument is greater than second, produce a list in decreasing order.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>range</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>range</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>range</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>A tail recursive implementation:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>range</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>high</span><span class='ocaml-source'> </span><span class='ocaml-source'>low</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>high</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;=</span><span class='ocaml-source'> </span><span class='ocaml-source'>low</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>high</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>high</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>low</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>range</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="23" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Extract a Given Number of Randomly Selected Elements From a List </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>The selected items shall be returned in a list. We use the <code>Random</code> module but and initialise it with <code>Random.init 0</code> at the start of the function for reproducibility and validate the solution. To make the function truly random, however, one should remove the call to <code>Random.init 0</code></p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>rand_select</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>rand_select</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Random</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>init</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>extract</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>raise</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Not_found</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>extract</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>extract_rand</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>extract</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Random</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>picked</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>rest</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>extract_rand</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>picked</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>rest</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>min</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>rand_select</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="24" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Lotto: Draw N Different Random Numbers From the Set 1..M </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Draw N different random numbers from the set <code>1..M</code>.</p> <p>The selected numbers shall be returned in a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>lotto_select</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>49</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>20</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>28</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>45</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>16</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>24</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>38</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [range] and [rand_select] defined in problems above </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>lotto_select</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>m</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>rand_select</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>range</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>m</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>lotto_select</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="25" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Generate a Random Permutation of the Elements of a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Generate a random permutation of the elements of a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>permutation</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>permutation</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>extract</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>raise</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Not_found</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>extract</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>extract_rand</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>extract</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Random</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>picked</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>rest</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>extract_rand</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>picked</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>rest</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>permutation</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="26" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Generate the Combinations of K Distinct Objects Chosen From the N Elements of a List </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Generate the combinations of K distinct objects chosen from the N elements of a list.</p> <p>In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficients). For pure mathematicians, this result may be great. But we want to really generate all the possibilities in a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>ext</span><span class='ocaml-source'>ract </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>extract</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>with_h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>extract</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>without_h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>extract</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>with_h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>without_h</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>extract</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="27" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Group the Elements of a Set Into Disjoint Subsets </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Group the elements of a set into disjoint subsets</p> <ol> <li>In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons? Write a function that generates all the possibilities and returns them in a list.</li> <li>Generalize the above function in a way that we can specify a list of group sizes and the function will return a list of groups.</li> </ol> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>group</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> This implementation is less streamlined than the one-extraction </span><span class='ocaml-comment-block'> version, because more work is done on the lists after each </span><span class='ocaml-comment-block'> transform to prepend the actual items. The end result is cleaner </span><span class='ocaml-comment-block'> in terms of code, though. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>group</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>sizes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>initial</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>size</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>size</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>sizes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> The core of the function. Prepend accepts a list of groups, </span><span class='ocaml-comment-block'> each with the number of items that should be added, and </span><span class='ocaml-comment-block'> prepends the item to every group that can support it, thus </span><span class='ocaml-comment-block'> turning [1,a ; 2,b ; 0,c] into [ [0,x::a ; 2,b ; 0,c ]; </span><span class='ocaml-comment-block'> [1,a ; 1,x::b ; 0,c]; [ 1,a ; 2,b ; 0,c ]] </span><span class='ocaml-comment-block'> </span><span class='ocaml-comment-block'> Again, in the prolog language (for which these questions are </span><span class='ocaml-comment-block'> originally intended), this function is a whole lot simpler. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>prepend</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>emit</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>emit</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>emit</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>emit</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>emit</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>emit</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>initial</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>concat_map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>prepend</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>all</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Don't forget to eliminate all group sets that have non-full </span><span class='ocaml-comment-block'> groups </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>complete</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>filter</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>for_all</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>all</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>snd</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>complete</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>group</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="28" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Sorting a List of Lists According to Length of Sublists </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Sorting a list of lists according to length of sublists.</p> <ol> <li> <p>We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of this list according to their length. E.g. short lists first, longer lists later, or vice versa.</p> </li> <li> <p>Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort the elements of this list according to their <strong>length frequency</strong>; i.e., in the default, where sorting is done ascendingly, lists with rare lengths are placed first, others with a more frequent length come later.</p> </li> </ol> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>length_sort</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>i</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>j</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>k</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>l</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>m</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>n</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>o</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>o</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>m</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>n</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>i</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>j</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>k</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>l</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>frequency_sort</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>i</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>j</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>k</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>l</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>m</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>n</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>o</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>i</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>j</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>k</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>l</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>o</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>h</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>m</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>n</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> We might not be allowed to use built-in List.sort, so here's an </span><span class='ocaml-comment-block'> eight-line implementation of insertion sort — O(n²) time </span><span class='ocaml-comment-block'> complexity. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>insert</span><span class='ocaml-source'> </span><span class='ocaml-source'>cmp</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>e</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>cmp</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>insert</span><span class='ocaml-source'> </span><span class='ocaml-source'>cmp</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>sort</span><span class='ocaml-source'> </span><span class='ocaml-source'>cmp</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>insert</span><span class='ocaml-source'> </span><span class='ocaml-source'>cmp</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>sort</span><span class='ocaml-source'> </span><span class='ocaml-source'>cmp</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Sorting according to length : prepend length, sort, remove length </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>length_sort</span><span class='ocaml-source'> </span><span class='ocaml-source'>lists</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>lists</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>lists</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>lists</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>sort</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>compare</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>fst</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>fst</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>lists</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>snd</span><span class='ocaml-source'> </span><span class='ocaml-source'>lists</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Sorting according to length frequency : prepend frequency, sort, </span><span class='ocaml-comment-block'> remove frequency. Frequencies are extracted by sorting lengths </span><span class='ocaml-comment-block'> and applying RLE to count occurrences of each length (see problem </span><span class='ocaml-comment-block'> </span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'>Run-length encoding of a list.</span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'>) </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Can only be reached if original list is empty </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>x</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>count</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>frequency_sort</span><span class='ocaml-source'> </span><span class='ocaml-source'>lists</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>lengths</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>lists</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>freq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>rle</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>sort</span><span class='ocaml-source'> </span><span class='ocaml-source'>compare</span><span class='ocaml-source'> </span><span class='ocaml-source'>lengths</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>by_freq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>assoc</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>freq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>lists</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>sorted</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>sort</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>compare</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>fst</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>fst</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>by_freq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>snd</span><span class='ocaml-source'> </span><span class='ocaml-source'>sorted</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="31" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Determine Whether a Given Integer Number Is Prime </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Determine whether a given integer number is prime.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>not</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>not</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>12</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><p>Recall that <code>d</code> divides <code>n</code> if and only if <code>n mod d = 0</code>. This is a naive solution. See the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a> for a more clever one.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>abs</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>is_not_divisor</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_not_divisor</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_not_divisor</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="32" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Determine the Greatest Common Divisor of Two Positive Integer Numbers </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Determine the greatest common divisor of two positive integer numbers.</p> <p>Use Euclid's algorithm.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>gcd</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>13</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>27</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>gcd</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>20536</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7826</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>gcd</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>gcd</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>gcd</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="33" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Determine Whether Two Positive Integer Numbers Are Coprime </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Determine whether two positive integer numbers are coprime.</p> <p>Two numbers are coprime if their greatest common divisor equals 1.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>coprime</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>13</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>27</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>not</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>coprime</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>20536</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7826</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [gcd] is defined in the previous question </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>coprime</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>gcd</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>coprime</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="34" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Calculate Euler&#x27;s Totient Function Φ(m) </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Euler's so-called totient function φ(m) is defined as the number of positive integers r (1 ≤ r &lt; m) that are coprime to m. We let φ(1) = 1.</p> <p>Find out what the value of φ(m) is if m is a prime number. Euler's totient function plays an important role in one of the most widely used public key cryptography methods (RSA). In this exercise you should use the most primitive method to calculate this function (there are smarter ways that we shall discuss later).</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>phi</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>10</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [coprime] is defined in the previous question </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>phi</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>count_coprime</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>count_coprime</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>coprime</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>count_coprime</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>phi</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="35" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Determine the Prime Factors of a Given Positive Integer </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Construct a flat list containing the prime factors in ascending order.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>factors</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>315</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Recall that d divides n iff [n mod d = 0] </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>factors</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>/</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>factors</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="36" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Determine the Prime Factors of a Given Positive Integer (2) </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Construct a list containing the prime factors and their multiplicity.</p> <p><strong>Hint:</strong> The problem is similar to problem <a href="#10">Run-length encoding of a list (direct solution)</a>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>factors</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>315</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>factors</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>/</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>when</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>d</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>factors</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="37" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Calculate Euler&#x27;s Totient Function Φ(m) (Improved) </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>See problem &quot;<a href="#34">Calculate Euler's totient function φ(m)</a>&quot; for the definition of Euler's totient function. If the list of the prime factors of a number m is known in the form of the previous problem then the function phi(m) can be efficiently calculated as follows: Let <code>[(p1, m1); (p2, m2); (p3, m3); ...]</code> be the list of prime factors (and their multiplicities) of a given number m. Then φ(m) can be calculated with the following formula:</p> <p>φ(m) = (p1 - 1) × p1<sup>m1 - 1</sup> × (p2 - 1) × p2<sup>m2 - 1</sup> × (p3 - 1) × p3<sup>m3 - 1</sup> × ⋯</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>phi_improved</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>10</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>phi_improved</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>13</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>12</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Naive power function. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>pow</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>pow</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [factors] is defined in the previous question. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>phi_improved</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>m</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>pow</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>m</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>factors</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="38" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Compare the Two Methods of Calculating Euler&#x27;s Totient Function </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Use the solutions of problems &quot;<a href="#34">Calculate Euler's totient function φ(m)</a>&quot; and &quot;<a href="#37">Calculate Euler's totient function φ(m) (improved)</a>&quot; to compare the algorithms. Take the number of logical inferences as a measure for efficiency. Try to calculate φ(10090) as an example.</p> <pre><code><span class='ocaml-source'>timeit</span><span class='ocaml-source'> </span><span class='ocaml-source'>phi</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>10090</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Naive [timeit] function. It requires the [Unix] module to be loaded. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>timeit</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Unix</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>gettimeofday</span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>ignore</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Unix</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>gettimeofday</span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-.</span><span class='ocaml-source'> </span><span class='ocaml-source'>t0</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>timeit</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>float</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="39" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> A List of Prime Numbers </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>all_primes</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7920</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1000</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>max</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'>n</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>is_not_divisor</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_not_divisor</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>is_not_divisor</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>all_primes</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>rest</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>all_primes</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>rest</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>rest</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>all_primes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="40" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Goldbach&#x27;s Conjecture </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case. It has been <em>numerically confirmed</em> up to very large numbers. Write a function to find the two prime numbers that sum up to a given even integer.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>goldbach</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>28</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>23</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [is_prime] is defined in the previous solution </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>goldbach</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_prime</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>d</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>goldbach</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="41" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> A List of Goldbach Compositions </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.</p> <p>In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. Very rarely, the primes are both bigger than say 50. Try to find out how many such cases there are in the range 2..3000.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>goldbach_list</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>20</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>10</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>12</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>14</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>11</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>16</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>13</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>18</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>13</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>20</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>17</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [goldbach] is defined in the previous question. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>goldbach_list</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>goldbach_list</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>goldbach</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>goldbach_list</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>goldbach_limit</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>lim</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>filter</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>lim</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>lim</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>goldbach_list</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>goldbach_list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>goldbach_limit</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="46" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Truth Tables for Logical Expressions (2 Variables) </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Let us define a small &quot;language&quot; for boolean expressions containing variables:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Not</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Or</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Not</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Or</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span></code></pre> <p>A logical expression in two variables can then be written in prefix notation. For example, <code>(a ∨ b) ∧ (a ∧ b)</code> is written:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Or</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Or</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> <p>Define a function, <code>table2</code> which returns the truth table of a given logical expression in two variables (specified as arguments). The return value must be a list of triples containing <code>(value_of_a, value_of_b, value_of_expr)</code>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>table2</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Or</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_a</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_b</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>failwith</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>The expression contains an invalid variable</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Not</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>not</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_b</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'>(</span><span class='ocaml-source'>e1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>e2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_b</span><span class='ocaml-source'> </span><span class='ocaml-source'>e1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_b</span><span class='ocaml-source'> </span><span class='ocaml-source'>e2</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Or</span><span class='ocaml-source'>(</span><span class='ocaml-source'>e1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>e2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_b</span><span class='ocaml-source'> </span><span class='ocaml-source'>e1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_b</span><span class='ocaml-source'> </span><span class='ocaml-source'>e2</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>table2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>table2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="48" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Truth Tables for Logical Expressions </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Generalize the previous problem in such a way that the logical expression may contain any number of logical variables. Define <code>table</code> in a way that <code>table variables expr</code> returns the truth table for the expression <code>expr</code>, which contains the logical variables enumerated in <code>variables</code>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>table</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Or</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [val_vars] is an associative list containing the truth value of </span><span class='ocaml-comment-block'> each variable. For efficiency, a Map or a Hashtlb should be </span><span class='ocaml-comment-block'> preferred. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>eval</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Var</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>assoc</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Not</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>not</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>eval</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>And</span><span class='ocaml-source'>(</span><span class='ocaml-source'>e1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>e2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>e1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>e2</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Or</span><span class='ocaml-source'>(</span><span class='ocaml-source'>e1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>e2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>e1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>e2</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Again, this is an easy and short implementation rather than an </span><span class='ocaml-comment-block'> efficient one. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>table_make</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>vars</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>table_make</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>table_make</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>val_vars</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>table</span><span class='ocaml-source'> </span><span class='ocaml-source'>vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>table_make</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>vars</span><span class='ocaml-source'> </span><span class='ocaml-source'>expr</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>eval</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>table_make</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>table</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>bool_expr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="49" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Gray Code </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>An n-bit Gray code is a sequence of n-bit strings constructed according to certain rules. For example,</p> <pre><code class="language-text">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']. </code></pre> <p>Find out the construction rules and write a function with the following specification: <code>gray n</code> returns the <code>n</code>-bit Gray code.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>gray</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>0</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>1</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>gray</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>00</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>01</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>11</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>10</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>gray</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>000</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>001</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>011</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>010</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>110</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>111</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>101</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>100</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>gray</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>gray_next_level</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> This is the core part of the Gray code construction. </span><span class='ocaml-comment-block'> * first_half is reversed and has a </span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'>0</span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'> attached to every element. </span><span class='ocaml-comment-block'> * Second part is reversed (it must be reversed for correct gray code). </span><span class='ocaml-comment-block'> * Every element has </span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'>1</span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'> attached to the front.</span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>first_half</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'>second_half</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>acc1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'>acc2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>0</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>^</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>1</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>^</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc2</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> List.rev_append turns first_half around and attaches it to second_half. </span><span class='ocaml-comment-block'> * The result is the modified first_half in correct order attached to </span><span class='ocaml-comment-block'> * the second_half modified in reversed order.</span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>gray_next_level</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev_append</span><span class='ocaml-source'> </span><span class='ocaml-source'>first_half</span><span class='ocaml-source'> </span><span class='ocaml-source'>second_half</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>gray_next_level</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>0</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>1</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>gray</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="50" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Huffman Code </h2> <div class="inline px-3 py-1 bg-primary dark:bg-dark-primary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Advanced difficulty</div>"> Advanced </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>First of all, consult a good book on discrete mathematics or algorithms for a detailed description of Huffman codes (you can start with the <a href="http://en.wikipedia.org/wiki/Huffman_coding">Wikipedia page</a>)!</p> <p>We consider a set of symbols with their frequencies. For example, if the alphabet is <code>&quot;a&quot;</code>,..., <code>&quot;f&quot;</code> (represented as the positions 0,...5) and respective frequencies are 45, 13, 12, 16, 9, 5:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>fs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>45</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>13</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>12</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>16</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>fs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>45</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>13</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>12</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>16</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> <p>Our objective is to construct the Huffman code <code>c</code> word for all symbols <code>s</code>. In our example, the result could be <code>hs = [(&quot;a&quot;, &quot;0&quot;); (&quot;b&quot;, &quot;101&quot;); (&quot;c&quot;, &quot;100&quot;); (&quot;d&quot;, &quot;111&quot;); (&quot;e&quot;, &quot;1101&quot;); (&quot;f&quot;, &quot;1100&quot;)]</code> (or <code>hs = [(&quot;a&quot;, &quot;1&quot;);...]</code>). The task shall be performed by the function <code>huffman</code> defined as follows: <code>huffman(fs)</code> returns the Huffman code table for the frequency table <code>fs</code></p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>huffman</span><span class='ocaml-source'> </span><span class='ocaml-source'>fs</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>0</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>c</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>100</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>101</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>f</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>1100</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>e</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>1101</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>d</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>111</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Simple priority queue where the priorities are integers 0..100. </span><span class='ocaml-comment-block'> The node with the lowest probability comes first. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>module</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Pq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>struct</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>data</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>array</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>mutable</span><span class='ocaml-source'> </span><span class='ocaml-source'>first</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>make</span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>data</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Array</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>make</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>101</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>first</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>101</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>data</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;-</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>data</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;-</span><span class='ocaml-source'> </span><span class='ocaml-source'>min</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>get_min</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>101</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>data</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>assert</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>p</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>data</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;-</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>while</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>101</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>data</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>do</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;-</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>first</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>done</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>end</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Leaf</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>huffman_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Pq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>get_min</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Pq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>get_min</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'>(</span><span class='ocaml-source'>p1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'>(</span><span class='ocaml-source'>p2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Pq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>p1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>p2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'>(</span><span class='ocaml-source'>t1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>huffman_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>assert</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Build the prefix-free binary code from the tree </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>prefixes_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>prefix</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Leaf</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-source'>s</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>prefix</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'>(</span><span class='ocaml-source'>t0</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>prefixes_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>prefix</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>^</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>0</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t0</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>prefixes_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>prefix</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>^</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>1</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>huffman</span><span class='ocaml-source'> </span><span class='ocaml-source'>fs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>fs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>100</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>failwith</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>huffman: sum of weights must be 100</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>q</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Pq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>make</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>iter</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>s</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Pq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Leaf</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>fs</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>prefixes_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>huffman_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>q</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>module</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Pq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>sig</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>'a </span><span class='ocaml-entity-name-function-binding'>t</span><span class='ocaml-source'> = { data : 'a list array; </span><span class='ocaml-keyword-other-ocaml'>mutable</span><span class='ocaml-source'> first : int; } </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>make</span><span class='ocaml-source'> : unit -&gt; 'a t </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>add</span><span class='ocaml-source'> : 'a t -&gt; int -&gt; 'a -&gt; unit </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>get_min</span><span class='ocaml-source'> : 'a t -&gt; (int * 'a) option </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>end</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Leaf</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>huffman_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Pq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>prefixes_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>huffman</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="55" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Construct Completely Balanced Binary Trees </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/binary-tree.gif" alt="Binary Tree" ></p> <p><em>A binary tree is either empty or it is composed of a root element and two successors, which are binary trees themselves.</em></p> <p>In OCaml, one can define a new type <code>binary_tree</code> that carries an arbitrary value of type <code>'a</code> (thus is polymorphic) at each node.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span></code></pre> <p>An example of tree carrying <code>char</code> data is:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>example_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> <p>In OCaml, the strict type discipline <em>guarantees</em> that, if you get a value of type <code>binary_tree</code>, then it must have been created with the two constructors <code>Empty</code> and <code>Node</code>.</p> <p>In a completely balanced binary tree, the following property holds for every node: The number of nodes in its left subtree and the number of nodes in its right subtree are almost equal, which means their difference is not greater than one.</p> <p>Write a function <code>cbal_tree</code> to construct completely balanced binary trees for a given number of nodes. The function should generate all solutions via backtracking. Put the letter <code>'x'</code> as information into all nodes of the tree.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>cbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-keyword-operator'>/</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Build all trees with given [left] and [right] subtrees. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>add_trees_with</span><span class='ocaml-source'> </span><span class='ocaml-source'>left</span><span class='ocaml-source'> </span><span class='ocaml-source'>right</span><span class='ocaml-source'> </span><span class='ocaml-source'>all</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>add_right_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>all</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>all</span><span class='ocaml-source'> </span><span class='ocaml-source'>right</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_right_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>all</span><span class='ocaml-source'> </span><span class='ocaml-source'>left</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>cbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>cbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>/</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>add_trees_with</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> n even: n-1 nodes for the left &amp; right subtrees altogether. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>cbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>/</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>cbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>/</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>add_trees_with</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>add_trees_with</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_trees_with</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>cbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="56" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Symmetric Binary Trees </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Let us call a binary tree symmetric if you can draw a vertical line through the root node and then the right subtree is the mirror image of the left subtree. Write a function <code>is_symmetric</code> to check whether a given binary tree is symmetric.</p> <p><strong>Hint:</strong> Write a function <code>is_mirror</code> first to check whether one tree is the mirror image of another. We are only interested in the structure, not in the contents of the nodes.</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>is_mirror</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r1</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>is_mirror</span><span class='ocaml-source'> </span><span class='ocaml-source'>l1</span><span class='ocaml-source'> </span><span class='ocaml-source'>r2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_mirror</span><span class='ocaml-source'> </span><span class='ocaml-source'>r1</span><span class='ocaml-source'> </span><span class='ocaml-source'>l2</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>is_symmetric</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_mirror</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_mirror</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_symmetric</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="57" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Binary Search Trees (Dictionaries) </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Construct a <a href="http://en.wikipedia.org/wiki/Binary_search_tree">binary search tree</a> from a list of integer numbers.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>construct</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> <p>Then use this function to test the solution of the previous problem.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_symmetric</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>construct</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>18</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>12</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>21</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>not</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>is_symmetric</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>construct</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>insert</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>y</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>y</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>insert</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>y</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>insert</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>construct</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>insert</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>insert</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>construct</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="58" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Generate-and-Test Paradigm </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Apply the generate-and-test paradigm to construct all symmetric, completely balanced binary trees with a given number of nodes.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>sym_cbal_trees</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> <p>How many such trees are there with 57 nodes? Investigate about how many solutions there are for a given number of nodes? What if the number is even? Write an appropriate function.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>sym_cbal_trees</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>57</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>256</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>sym_cbal_trees</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>filter</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_symmetric</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>cbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>sym_cbal_trees</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="59" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Construct Height-Balanced Binary Trees </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>In a height-balanced binary tree, the following property holds for every node: The height of its left subtree and the height of its right subtree are almost equal, which means their difference is not greater than one.</p> <p>Write a function <code>hbal_tree</code> to construct height-balanced binary trees for a given height. The function should generate all solutions via backtracking. Put the letter <code>'x'</code> as information into all nodes of the tree.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>hbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><p>The function <code>add_trees_with</code> is defined in the solution of <a href="#55">Construct completely balanced binary trees</a>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>hbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [add_trees_with left right trees] is defined in a question above. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>hbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>and</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>hbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>add_trees_with</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>add_trees_with</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>add_trees_with</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>hbal_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="60" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Construct Height-Balanced Binary Trees With a Given Number of Nodes </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Consider a height-balanced binary tree of height <code>h</code>. What is the maximum number of nodes it can contain? Clearly, max_nodes = 2<sup><code>h</code></sup> - 1.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>max_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>lsl</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <h2>Minimum of nodes</h2> <p>However, what is the minimum number min_nodes? This question is more difficult. Try to find a recursive statement and turn it into a function <code>min_nodes</code> defined as follows: <code>min_nodes h</code> returns the minimum number of nodes in a height-balanced binary tree of height <code>h</code>.</p> <h2>Minimum height</h2> <p>On the other hand, we might ask: what are the minimum (resp. maximum) height H a height-balanced binary tree with N nodes can have? <code>min_height</code> (resp. <code>max_height n</code>) returns the minimum (resp. maximum) height of a height-balanced binary tree with <code>n</code> nodes.</p> <h2>Constructing trees</h2> <p>Now, we can attack the main problem: construct all the height-balanced binary trees with a given number of nodes. <code>hbal_tree_nodes n</code> returns a list of all height-balanced binary tree with <code>n</code> nodes.</p> <p>Find out how many height-balanced trees exist for <code>n = 15</code>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>hbal_tree_nodes</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>15</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1553</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><h2>Minimum of nodes</h2> <p>The following solution comes directly from translating the question.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>It is not the more efficient one however. One should use the last two values as the state to avoid the double recursion.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>min_nodes_loop</span><span class='ocaml-source'> </span><span class='ocaml-source'>m0</span><span class='ocaml-source'> </span><span class='ocaml-source'>m1</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>m1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_nodes_loop</span><span class='ocaml-source'> </span><span class='ocaml-source'>m1</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>m1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>m0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_nodes_loop</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_nodes_loop</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>It is not difficult to show that <code>min_nodes h</code> = F<sub>h+2‌</sub> - 1, where (F<sub>n</sub>) is the <a href="https://en.wikipedia.org/wiki/Fibonacci_number">Fibonacci sequence</a>.</p> <h2>Minimum height</h2> <p>Inverting the formula max_nodes = 2<sup><code>h</code></sup> - 1, one directly find that Hₘᵢₙ(n) = ⌈log₂(n+1)⌉ which is readily implemented:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>min_height</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>int_of_float</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>ceil</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>log</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>float</span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>/.</span><span class='ocaml-source'> </span><span class='ocaml-source'>log</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-float'>2.</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_height</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>Let us give a proof that the formula for Hₘᵢₙ is valid. First, if h = <code>min_height</code> n, there exists a height-balanced tree of height h with n nodes. Thus 2ʰ - 1 = <code>max_nodes h</code> ≥ n i.e., h ≥ log₂(n+1). To establish equality for Hₘᵢₙ(n), one has to show that, for any n, there exists a height-balanced tree with height Hₘᵢₙ(n). This is due to the relation Hₘᵢₙ(n) = 1 + Hₘᵢₙ(n/2) where n/2 is the integer division. For n odd, this is readily proved — so one can build a tree with a top node and two sub-trees with n/2 nodes of height Hₘᵢₙ(n) - 1. For n even, the same proof works if one first remarks that, in that case, ⌈log₂(n+2)⌉ = ⌈log₂(n+1)⌉ — use log₂(n+1) ≤ h ∈ ℕ ⇔ 2ʰ ≥ n + 1 and the fact that 2ʰ is even for that. This allows to have a sub-tree with n/2 nodes. For the other sub-tree with n/2-1 nodes, one has to establish that Hₘᵢₙ(n/2-1) ≥ Hₘᵢₙ(n) - 2 which is easy because, if h = Hₘᵢₙ(n/2-1), then h+2 ≥ log₂(2n) ≥ log₂(n+1).</p> <p>The above function is not the best one however. Indeed, not every 64 bits integer can be represented exactly as a floating point number. Here is one that only uses integer operations:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>ceil_log2_loop</span><span class='ocaml-source'> </span><span class='ocaml-source'>log</span><span class='ocaml-source'> </span><span class='ocaml-source'>plus1</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>plus1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>log</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>log</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>ceil_log2_loop</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>log</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>plus1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>land</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>/</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>ceil_log2</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>ceil_log2_loop</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>ceil_log2_loop</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>ceil_log2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>This algorithm is still not the fastest however. See for example the <a href="http://www.hackersdelight.org/">Hacker's Delight</a>, section 5-3 (and 11-4).</p> <p>Following the same idea as above, if h = <code>max_height</code> n, then one easily deduces that <code>min_nodes</code> h ≤ n &lt; <code>min_nodes</code>(h+1). This yields the following code:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>max_height_search</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_height_search</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>max_height</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_height_search</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_height_search</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_height</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>Of course, since <code>min_nodes</code> is computed recursively, there is no need to recompute everything to go from <code>min_nodes h</code> to <code>min_nodes(h+1)</code>:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>max_height_search</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-source'>m_h</span><span class='ocaml-source'> </span><span class='ocaml-source'>m_h1</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>m_h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_height_search</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>m_h1</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>m_h1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>m_h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>max_height</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_height_search</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_height_search</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_height</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <h2>Constructing trees</h2> <p>First, we define some convenience functions <code>fold_range</code> that folds a function <code>f</code> on the range <code>n0</code>...<code>n1</code> i.e., it computes <code>f (... f (f (f init n0) (n0+1)) (n0+2) ...) n1</code>. You can think it as performing the assignment <code>init ← f init n</code> for <code>n = n0,..., n1</code> except that there is no mutable variable in the code.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>fold_range</span><span class='ocaml-source'> </span><span class='ocaml-source'>~</span><span class='ocaml-source'>f</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>init</span><span class='ocaml-source'> </span><span class='ocaml-source'>n0</span><span class='ocaml-source'> </span><span class='ocaml-source'>n1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>init</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>fold_range</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>f</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>init</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'>(</span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>init</span><span class='ocaml-source'> </span><span class='ocaml-source'>n0</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>fold_range</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>init</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>When constructing trees, there is an obvious symmetry: if one swaps the left and right sub-trees of a balanced tree, we still have a balanced tree. The following function returns all trees in <code>trees</code> together with their permutation.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>add_swap_left_right</span><span class='ocaml-source'> </span><span class='ocaml-source'>trees</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>trees</span><span class='ocaml-source'> </span><span class='ocaml-source'>trees</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_swap_left_right</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>Finally we generate all trees recursively, using a priori the bounds computed above. It could be further optimized but our aim is to straightforwardly express the idea.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>hbal_tree_nodes_height</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>assert</span><span class='ocaml-source'>(</span><span class='ocaml-source'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_hbal_tree_node</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_swap_left_right</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>add_hbal_tree_node</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>and</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_hbal_tree_node</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>h1</span><span class='ocaml-source'> </span><span class='ocaml-source'>h2</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>min_n1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>max</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>max_n1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>min</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>max_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>h2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>fold_range</span><span class='ocaml-source'> </span><span class='ocaml-source'>min_n1</span><span class='ocaml-source'> </span><span class='ocaml-source'>max_n1</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>init</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'>l</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>f</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>n1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>hbal_tree_nodes_height</span><span class='ocaml-source'> </span><span class='ocaml-source'>h1</span><span class='ocaml-source'> </span><span class='ocaml-source'>n1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>hbal_tree_nodes_height</span><span class='ocaml-source'> </span><span class='ocaml-source'>h2</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>n1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>hbal_tree_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>fold_range</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>min_height</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>max_height</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'>)</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>init</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>f</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev_append</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>hbal_tree_nodes_height</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>hbal_tree_nodes_height</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_hbal_tree_node</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>hbal_tree_nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="61A" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Count the Leaves of a Binary Tree </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>A leaf is a node with no successors. Write a function <code>count_leaves</code> to count them.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>count_leaves</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>count_leaves</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>count_leaves</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>count_leaves</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>count_leaves</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="61B" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Collect the Leaves of a Binary Tree in a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>A leaf is a node with no successors. Write a function <code>leaves</code> to collect them in a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaves</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Having an accumulator acc prevents using inefficient List.append. </span><span class='ocaml-comment-block'> * Every Leaf will be pushed directly into accumulator. </span><span class='ocaml-comment-block'> * Not tail-recursive, but that is no problem since we have a binary tree and </span><span class='ocaml-comment-block'> * and stack depth is logarithmic. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>leaves</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>leaves_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaves_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>leaves_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>leaves_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaves</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="62A" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Collect the Internal Nodes of a Binary Tree in a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>An internal node of a binary tree has either one or two non-empty successors. Write a function <code>internals</code> to collect them in a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>internals</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Having an accumulator acc prevents using inefficient List.append. </span><span class='ocaml-comment-block'> * Every internal node will be pushed directly into accumulator. </span><span class='ocaml-comment-block'> * Not tail-recursive, but that is no problem since we have a binary tree and </span><span class='ocaml-comment-block'> * and stack depth is logarithmic. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>internals</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>internals_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>internals_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>internals_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>internals_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>internals</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="62B" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Collect the Nodes at a Given Level in a List </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>A node of a binary tree is at level N if the path from the root to the node has length N-1. The root node is at level 1. Write a function <code>at_level t l</code> to collect all nodes of the tree <code>t</code> at level <code>l</code> in a list.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>example_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>at_level</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_tree</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> <p>Using <code>at_level</code> it is easy to construct a function <code>levelorder</code> which creates the level-order sequence of the nodes. However, there are more efficient ways to do that.</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Having an accumulator acc prevents using inefficient List.append. </span><span class='ocaml-comment-block'> * Every node at level N will be pushed directly into accumulator. </span><span class='ocaml-comment-block'> * Not tail-recursive, but that is no problem since we have a binary tree and </span><span class='ocaml-comment-block'> * and stack depth is logarithmic. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>at_level</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>level</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>at_level_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>counter</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>counter</span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'>level</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>at_level_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>at_level_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>counter</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>counter</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>at_level_aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>at_level</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="63" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Construct a Complete Binary Tree </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>A <em>complete</em> binary tree with height H is defined as follows: The levels 1,2,3,...,H-1 contain the maximum number of nodes (i.e 2<sup>i-1</sup> at the level i, note that we start counting the levels from 1 at the root). In level H, which may contain less than the maximum possible number of nodes, all the nodes are &quot;left-adjusted&quot;. This means that in a levelorder tree traversal all internal nodes come first, the leaves come second, and empty successors (the nil's which are not really nodes!) come last.</p> <p>Particularly, complete binary trees are used as data structures (or addressing schemes) for heaps.</p> <p>We can assign an address number to each node in a complete binary tree by enumerating the nodes in levelorder, starting at the root with number 1. In doing so, we realize that for every node X with address A the following property holds: The address of X's left and right successors are 2*A and 2*A+1, respectively, supposed the successors do exist. This fact can be used to elegantly construct a complete binary tree structure. Write a function <code>is_complete_binary_tree</code> with the following specification: <code>is_complete_binary_tree n t</code> returns <code>true</code> iff <code>t</code> is a complete binary tree with <code>n</code> nodes.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>complete_binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>split_n</span><span class='ocaml-source'> </span><span class='ocaml-source'>lst</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>lst</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>lst</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>split_n</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>myflatten</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>y</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>myflatten</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>ph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>pt</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>ph</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>myflatten</span><span class='ocaml-source'> </span><span class='ocaml-source'>pt</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>invalid_arg</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>myflatten</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>complete_binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>lst</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>lst</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>split_n</span><span class='ocaml-source'> </span><span class='ocaml-source'>lst</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>lsl</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>myflatten</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>hd</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>lst</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>split_n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>myflatten</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>complete_binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="64" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Layout a Binary Tree (1) </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>As a preparation for drawing the tree, a layout algorithm is required to determine the position of each node in a rectangular grid. Several layout methods are conceivable, one of them is shown in the illustration.</p> <p><img src="/media/problems/tree-layout1.gif" alt="Binary Tree Grid" ></p> <p>In this layout strategy, the position of a node v is obtained by the following two rules:</p> <ul> <li><em>x(v)</em> is equal to the position of the node v in the <em>inorder</em> sequence;</li> <li><em>y(v)</em> is equal to the depth of the node <em>v</em> in the tree.</li> </ul> <p>In order to store the position of the nodes, we will enrich the value at each node with the position <code>(x,y)</code>.</p> <p>The tree pictured above is</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'n'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'m'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'p'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'s'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'q'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'n'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'m'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'p'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'s'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'q'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout_binary_tree_1</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_layout_tree</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'n'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'m'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>12</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'p'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'s'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>11</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'q'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>10</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>layout_binary_tree_1</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>layout</span><span class='ocaml-source'> </span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-source'>x_left</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> This function returns a pair: the laid out tree and the first </span><span class='ocaml-comment-block'> * free x location </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x_left</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>l'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l_x_max</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>x_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>r'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r_x_max</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>l_x_max</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l_x_max</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>depth</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r_x_max</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>fst</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>layout</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout_binary_tree_1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="65" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Layout a Binary Tree (2) </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/tree-layout2.gif" alt="Binary Tree Grid" ></p> <p>An alternative layout method is depicted in this illustration. Find out the rules and write the corresponding OCaml function.</p> <p><strong>Hint:</strong> On a given level, the horizontal distance between neighbouring nodes is constant.</p> <p>The tree shown is</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'n'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'m'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'p'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'q'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'n'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'m'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'p'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'q'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout_binary_tree_2</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'n'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>15</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'m'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>11</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>23</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'p'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>19</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'q'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>21</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>layout_binary_tree_2</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>height</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>max</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>height</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>height</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>tree_height</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>height</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>find_missing_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree_height</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>find_missing_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>translate_dst</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>lsl</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>find_missing_left</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> remember than 1 lsl a = 2ᵃ </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>layout</span><span class='ocaml-source'> </span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-source'>x_root</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>spacing</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>lsl</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>tree_height</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>l'</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x_root</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>spacing</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>and</span><span class='ocaml-source'> </span><span class='ocaml-source'>r'</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x_root</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>spacing</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x_root</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>depth</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'>r'</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>layout</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>lsl</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>tree_height</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>translate_dst</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout_binary_tree_2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="66" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Layout a Binary Tree (3) </h2> <div class="inline px-3 py-1 bg-primary dark:bg-dark-primary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Advanced difficulty</div>"> Advanced </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/tree-layout3.gif" alt="Binary Tree Grid" ></p> <p>Yet another layout strategy is shown in the above illustration. The method yields a very compact layout while maintaining a certain symmetry in every node. Find out the rules and write the corresponding predicate.</p> <p><strong>Hint:</strong> Consider the horizontal distance between a node and its successor nodes. How tight can you pack together two subtrees to construct the combined binary tree? This is a difficult problem. Don't give up too early!</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'n'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'m'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'p'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'s'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'q'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'n'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'m'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'p'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'s'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'q'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout_binary_tree_3</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'n'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'m'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'p'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'s'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'q'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> <p>Which layout do you like most?</p> </div> <div x-show="!statement"><p>In order to pack the tree tightly, the layout function will return in addition to the layout of the tree the left and right profiles of the tree, that is lists of offsets relative to the position of the root node of the tree.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>layout_binary_tree_3</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>translate_x</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>translate_x</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>translate_x</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Distance between a left subtree given by its right profile [lr] </span><span class='ocaml-comment-block'> and a right subtree given by its left profile [rl]. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>dist</span><span class='ocaml-source'> </span><span class='ocaml-source'>lr</span><span class='ocaml-source'> </span><span class='ocaml-source'>rl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>lr</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>rl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>lrx</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>ltl</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>rlx</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>rtl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>max</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>lrx</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>rlx</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>dist</span><span class='ocaml-source'> </span><span class='ocaml-source'>ltl</span><span class='ocaml-source'> </span><span class='ocaml-source'>rtl</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>merge_profiles</span><span class='ocaml-source'> </span><span class='ocaml-source'>p1</span><span class='ocaml-source'> </span><span class='ocaml-source'>p2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>p1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>p2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>x1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>x1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>merge_profiles</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl1</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl2</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>p2</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>p1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>layout</span><span class='ocaml-source'> </span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>ll</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>lr</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>rl</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>rr</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>depth</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>dist</span><span class='ocaml-source'> </span><span class='ocaml-source'>lr</span><span class='ocaml-source'> </span><span class='ocaml-source'>rl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>/</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>ll</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>ll</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>and</span><span class='ocaml-source'> </span><span class='ocaml-source'>lr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>lr</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>and</span><span class='ocaml-source'> </span><span class='ocaml-source'>rl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>rl</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>and</span><span class='ocaml-source'> </span><span class='ocaml-source'>rr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>rr</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>merge_profiles</span><span class='ocaml-source'> </span><span class='ocaml-source'>ll</span><span class='ocaml-source'> </span><span class='ocaml-source'>rl</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>depth</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>translate_x</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'>d</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>translate_x</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-source'>r'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>merge_profiles</span><span class='ocaml-source'> </span><span class='ocaml-source'>rr</span><span class='ocaml-source'> </span><span class='ocaml-source'>lr</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>x_min</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>min</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>translate_x</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>x_min</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>layout_binary_tree_3</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="67" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> A String Representation of Binary Trees </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/binary-tree.gif" alt="Binary Tree" ></p> <p>Somebody represents binary trees as strings of the following type (see example): <code>&quot;a(b(d,e),c(,f(g,)))&quot;</code>.</p> <ul> <li>Write an OCaml function <code>string_of_tree</code> which generates this string representation, if the tree is given as usual (as <code>Empty</code> or <code>Node(x,l,r)</code> term). Then write a function <code>tree_of_string</code> which does this inverse; i.e. given the string representation, construct the tree in the usual form. Finally, combine the two predicates in a single function <code>tree_string</code> which can be used in both directions.</li> <li>Write the same predicate <code>tree_string</code> using difference lists and a single predicate <code>tree_dlist</code> which does the conversion between a tree and a difference list in both directions.</li> </ul> <p>For simplicity, suppose the information in the nodes is a single letter and there are no spaces in the string.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>leaf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_layout_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><p>A simple solution is:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'>(</span><span class='ocaml-source'>data</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>data</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>String</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>make</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>data</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>data</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>data</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>^</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>(</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>^</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>^</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>,</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>^</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>^</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>)</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>One can also use a buffer to allocate a lot less memory:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>buffer_add_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>data</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>data</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'('</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>buffer_add_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>','</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>buffer_add_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>')'</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>buf</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>create</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>128</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>buffer_add_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>contents</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>buffer_add_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>unit</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>For the reverse conversion, we assume that the string is well formed and do not deal with error reporting.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>tree_of_string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>make</span><span class='ocaml-source'> </span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>String</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>[</span><span class='ocaml-source'>ofs</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>','</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>[</span><span class='ocaml-source'>ofs</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>')'</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>ofs</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>v</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>[</span><span class='ocaml-source'>ofs</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>String</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>[</span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'('</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>make</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> skip </span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'>v(</span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'> </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>make</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> skip </span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'>,</span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'> </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> skip </span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'>)</span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'> </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>ofs</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>fst</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>make</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>tree_of_string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="68" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Preorder and Inorder Sequences of Binary Trees </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>We consider binary trees with nodes that are identified by single lower-case letters, as in the example of the previous problem.</p> <ol> <li>Write functions <code>preorder</code> and <code>inorder</code> that construct the <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order">preorder</a> and <a href="https://en.wikipedia.org/wiki/Tree_traversal#In-order_.28symmetric.29">inorder</a> sequence of a given binary tree, respectively. The results should be atoms, e.g. 'abdecfg' for the preorder sequence of the example in the previous problem.</li> <li>Can you use <code>preorder</code> from problem part 1 in the reverse direction; i.e. given a preorder sequence, construct a corresponding tree? If not, make the necessary arrangements.</li> <li>If both the preorder sequence and the inorder sequence of the nodes of a binary tree are given, then the tree is determined unambiguously. Write a function <code>pre_in_tree</code> that does the job.</li> <li>Solve problems 1 to 3 using <a href="https://en.wikipedia.org/wiki/Difference_list">difference lists</a>. Cool! Use the function <code>timeit</code> (defined in problem “<a href="#38">Compare the two methods of calculating Euler's totient function.</a>”) to compare the solutions.</li> </ol> <p>What happens if the same character appears in more than one node. Try for instance <code>pre_in_tree &quot;aba&quot; &quot;baa&quot;</code>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>preorder</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><p>We use lists to represent the result. Note that <code>preorder</code> and <code>inorder</code> can be made more efficient by avoiding list concatenations.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>preorder</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>preorder</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>preorder</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>inorder</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>inorder</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>inorder</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>split_pre_in</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'>accp</span><span class='ocaml-source'> </span><span class='ocaml-source'>acci</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>accp</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>acci</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>h1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>h2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>h2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>accp</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acci</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>split_pre_in</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>accp</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>acci</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>assert</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>pre_in_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>p</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t1</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>lp</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>rp</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>li</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>ri</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>split_pre_in</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-source'>h1</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Node</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>h1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>pre_in_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>lp</span><span class='ocaml-source'> </span><span class='ocaml-source'>li</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>pre_in_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>rp</span><span class='ocaml-source'> </span><span class='ocaml-source'>ri</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>invalid_arg</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>pre_in_tree</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>preorder</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>inorder</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>split_pre_in</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>pre_in_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>binary_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> <p>Solution using <a href="https://en.wikipedia.org/wiki/Difference_list">difference lists</a>.</p> <pre><code><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> solution pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="69" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Dotstring Representation of Binary Trees </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>We consider again binary trees with nodes that are identified by single lower-case letters, as in the example of problem “<a href="#67">A string representation of binary trees</a>”. Such a tree can be represented by the preorder sequence of its nodes in which dots (.) are inserted where an empty subtree (nil) is encountered during the tree traversal. For example, the tree shown in problem “<a href="#67">A string representation of binary trees</a>” is represented as 'abd..e..c.fg...'. First, try to establish a syntax (BNF or syntax diagrams) and then write a function <code>tree_dotstring</code> which does the conversion in both directions. Use difference lists.</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> solution pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="70A" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Tree Construction From a Node String </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/multiway-tree.gif" alt="Multiway Tree" ></p> <p><em>A multiway tree is composed of a root element and a (possibly empty) set of successors which are multiway trees themselves. A multiway tree is never empty. The set of successor trees is sometimes called a forest.</em></p> <p>To represent multiway trees, we will use the following type which is a direct translation of the definition:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span></code></pre> <p>The example tree depicted opposite is therefore represented by the following OCaml expression:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> <p>We suppose that the nodes of a multiway tree contain single characters. In the depth-first order sequence of its nodes, a special character <code>^</code> has been inserted whenever, during the tree traversal, the move is a backtrack to the previous level.</p> <p>By this rule, the tree in the figure opposite is represented as: <code>afg^^c^bd^e^^^</code>.</p> <p>Write functions <code>string_of_tree : char mult_tree -&gt; string</code> to construct the string representing the tree and <code>tree_of_string : string -&gt; char mult_tree</code> to construct the tree when the string is given.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> We could build the final string by string concatenation but </span><span class='ocaml-comment-block'> this is expensive due to the number of operations. We use a </span><span class='ocaml-comment-block'> buffer instead. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>add_string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>c</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>iter</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>add_string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'^'</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>buf</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>create</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>128</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>add_string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>contents</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>unit</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>string_of_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="70B" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Count the Nodes of a Multiway Tree </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>count_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>count_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>count_nodes</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>count_nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="71" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Determine the Internal Path Length of a Tree </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>We define the internal path length of a multiway tree as the total sum of the path lengths from the root to all nodes of the tree. By this definition, the tree <code>t</code> in the figure of the previous problem has an internal path length of 9. Write a function <code>ipl tree</code> that returns the internal path length of <code>tree</code>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>ipl</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>ipl_sub</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [len] is the distance of the current node to the root. Add the </span><span class='ocaml-comment-block'> distance of all sub-nodes. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>sum</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>sum</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>ipl_sub</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>len</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>ipl</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>ipl_sub</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>ipl_sub</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>ipl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="72" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Construct the Bottom-Up Order Sequence of the Tree Nodes </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Write a function <code>bottom_up t</code> which constructs the bottom-up sequence of the nodes of the multiway tree <code>t</code>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>bottom_up</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>bottom_up</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>prepend_bottom_up</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>c</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_right</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>prepend_bottom_up</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>bottom_up</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>prepend_bottom_up</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>prepend_bottom_up</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>bottom_up</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="73" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Lisp-Like Tree Representation </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>There is a particular notation for multiway trees in Lisp. The picture shows how multiway tree structures are represented in Lisp.</p> <p><img src="/media/problems/lisp-like-tree.png" alt="Lisp representation of trees" ></p> <p>Note that in the &quot;lispy&quot; notation a node with successors (children) in the tree is always the first element in a list, followed by its children. The &quot;lispy&quot; representation of a multiway tree is a sequence of atoms and parentheses '(' and ')'. This is very close to the way trees are represented in OCaml, except that no constructor <code>T</code> is used. Write a function <code>lispy : char mult_tree -&gt; string</code> that returns the lispy notation of the tree.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>lispy</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>a</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>lispy</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>(a b)</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>lispy</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>(a (f g) c (b d e))</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>add_lispy</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'>(</span><span class='ocaml-source'>c</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>T</span><span class='ocaml-source'>(</span><span class='ocaml-source'>c</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'('</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>iter</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>' '</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_lispy</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>sub</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add_char</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>')'</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>lispy</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>buf</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>create</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>128</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>add_lispy</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>contents</span><span class='ocaml-source'> </span><span class='ocaml-source'>buf</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>add_lispy</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Buffer</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>unit</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>lispy</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>mult_tree</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="80" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Conversions </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/graph1.gif" alt="A graph" ></p> <p><em>A graph is defined as a set of nodes and a set of edges, where each edge is a pair of different nodes.</em></p> <p>There are several ways to represent graphs in OCaml.</p> <ul> <li>One method is to list all edges, an edge being a pair of nodes. In this form, the graph depicted above is represented as the following expression:</li> </ul> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> <p>We call this <strong>edge-clause form</strong>. Obviously, isolated nodes cannot be represented.</p> <ul> <li>Another method is to represent the whole graph as one data object. According to the definition of the graph as a pair of two sets (nodes and edges), we may use the following OCaml type:</li> </ul> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>}</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'> </span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span></code></pre> <p>Then, the above example graph is represented by:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>example_graph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_graph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span></code></pre> <p>We call this <strong>graph-term form</strong>. Note, that the lists are kept sorted, they are really sets, without duplicated elements. Each edge appears only once in the edge list; i.e. an edge from a node x to another node y is represented as <code>(x, y)</code>, the couple <code>(y, x)</code> is not present. The <strong>graph-term form is our default representation.</strong> You may want to define a similar type using sets instead of lists.</p> <ul> <li>A third representation method is to associate with each node the set of nodes that are adjacent to that node. We call this the <strong>adjacency-list form</strong>. In our example:</li> </ul> <pre><code><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>adjacency_example</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>adjacency_example</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> <ul> <li>The representations we introduced so far are well suited for automated processing, but their syntax is not very user-friendly. Typing the terms by hand is cumbersome and error-prone. We can define a more compact and &quot;human-friendly&quot; notation as follows: A graph (with char labelled nodes) is represented by a string of atoms and terms of the type X-Y. The atoms stand for isolated nodes, the X-Y terms describe edges. If an X appears as an endpoint of an edge, it is automatically defined as a node. Our example could be written as:</li> </ul> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b-c f-c g-h d f-b k-f h-g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>b-c f-c g-h d f-b k-f h-g</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span></code></pre> <p>We call this the <strong>human-friendly form</strong>. As the example shows, the list does not have to be sorted and may even contain the same edge multiple times. Notice the isolated node <code>d</code>.</p> <p>Write functions to convert between the different graph representations. With these functions, all representations are equivalent; i.e. for the following problems you can always pick freely the most convenient form. This problem is not particularly difficult, but it's a lot of work to deal with all the special cases.</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> example pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="81" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Path From One Node to Another One </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Write a function <code>paths g a b</code> that returns all acyclic path <code>p</code> from node <code>a</code> to node <code>b ≠ a</code> in the graph <code>g</code>. The function should return the list of all paths via backtracking.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>example_graph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_graph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>paths</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_graph</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> The datastructures used here are far from the most efficient ones </span><span class='ocaml-comment-block'> but allow for a straightforward implementation. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Returns all neighbors satisfying the condition. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>neighbors</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>cond</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>edge</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>cond</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>cond</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>edge</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>list_path</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>to_b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>to_b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>assert</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> [to_b] contains the path to [b]. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>a'</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>a'</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>to_b</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>neighbors</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>a'</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>not</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>mem</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-source'>to_b</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>concat_map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>list_path</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>to_b</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>paths</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>assert</span><span class='ocaml-source'>(</span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>list_path</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>b</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>neighbors</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>list_path</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>paths</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="82" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Cycle From a Given Node </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Write a functions <code>cycle g a</code> that returns a closed path (cycle) <code>p</code> starting at a given node <code>a</code> in the graph <code>g</code>. The predicate should return the list of all cycles via backtracking.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>example_graph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_graph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>cycles</span><span class='ocaml-source'> </span><span class='ocaml-source'>example_graph</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'k'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>cycles</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>neighbors</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>p</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>concat_map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>list_path</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>a</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>c</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>a</span><span class='ocaml-source'>]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>cycles</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="83" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Construct All Spanning Trees </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/spanning-tree-graph1.gif" alt="Spanning tree graph" ></p> <p>Write a function <code>s_tree g</code> to construct (by backtracking) all <a href="http://en.wikipedia.org/wiki/Spanning_tree">spanning trees</a> of a given graph <code>g</code>. With this predicate, find out how many spanning trees there are for the graph depicted to the left. The data of this example graph can be found in the test below. When you have a correct solution for the <code>s_tree</code> function, use it to define two other useful functions: <code>is_tree graph</code> and <code>is_connected Graph</code>. Both are five-minutes tasks!</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>g</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> solution pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="84" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Construct the Minimal Spanning Tree </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/spanning-tree-graph2.gif" alt="Spanning tree graph" ></p> <p>Write a function <code>ms_tree graph</code> to construct the minimal spanning tree of a given labelled graph. A labelled graph will be represented as follows:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>labeled_graph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>labeled_edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>}</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>labeled_graph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>labeled_edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span></code></pre> <p>(Beware that from now on <code>nodes</code> and <code>edges</code> mask the previous fields of the same name.)</p> <p><strong>Hint:</strong> Use the <a href="http://en.wikipedia.org/wiki/Prim%27s_algorithm">algorithm of Prim</a>. A small modification of the solution of P83 does the trick. The data of the example graph to the right can be found below.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>g</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>labeled_edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-support-type'>char</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>labeled_graph</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>labeled_edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'b'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'c'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'d'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'e'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'f'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'g'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'h'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> solution pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="85" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Graph Isomorphism </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Two graphs G1(N1,E1) and G2(N2,E2) are isomorphic if there is a bijection f: N1 → N2 such that for any nodes X,Y of N1, X and Y are adjacent if and only if f(X) and f(Y) are adjacent.</p> <p>Write a function that determines whether two graphs are isomorphic.</p> <p><strong>Hint:</strong> Use an open-ended list to represent the function f.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>g</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>graph_term</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>nodes</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>edges</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> solution pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="86" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Node Degree and Graph Coloration </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><ul> <li>Write a function <code>degree graph node</code> that determines the degree of a given node.</li> <li>Write a function that generates a list of all nodes of a graph sorted according to decreasing degree.</li> <li>Use <a href="http://en.wikipedia.org/wiki/Graph_coloring#Greedy_coloring">Welsh-Powell's algorithm</a> to paint the nodes of a graph in such a way that adjacent nodes have different colors.</li> </ul> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> example pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="87" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Depth-First Order Graph Traversal </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Write a function that generates a <a href="https://en.wikipedia.org/wiki/Depth-first_search">depth-first order graph traversal</a> sequence. The starting point should be specified, and the output should be a list of nodes that are reachable from this starting point (in depth-first order).</p> <p>Specifically, the graph will be provided by its <a href="https://en.wikipedia.org/wiki/Adjacency_list">adjacency-list representation</a> and you must create a module <code>M</code> with the following signature:</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>module</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>GRAPH</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>sig</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>type</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>node</span><span class='ocaml-source'> = char </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>type</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>of_adjacency</span><span class='ocaml-source'> : (node * node list) list -&gt; t </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>dfs_fold</span><span class='ocaml-source'> : t -&gt; node -&gt; ('a -&gt; node -&gt; 'a) -&gt; 'a -&gt; 'a </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>end</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>module</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>GRAPH</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>sig</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>type</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>node</span><span class='ocaml-source'> = char </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>type</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>of_adjacency</span><span class='ocaml-source'> : (node * node list) list -&gt; t </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>dfs_fold</span><span class='ocaml-source'> : t -&gt; node -&gt; ('a -&gt; node -&gt; 'a) -&gt; 'a -&gt; 'a </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>end</span><span class='ocaml-source'> </span></code></pre> <p>where <code>M.dfs_fold g n f a</code> applies <code>f</code> on the nodes of the graph <code>g</code> in depth first order, starting with node <code>n</code>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>g</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>M</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>of_adjacency</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'u'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'v'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'v'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'y'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'w'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'z'</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'y'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'v'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'y'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'x'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'z'</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-string-quoted-single'>'z'</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>M</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'>abstr</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><p>In a depth-first search you fully explore the edges of the most recently discovered node <em>v</em> before 'backtracking' to explore edges leaving the node from which <em>v</em> was discovered. To do a depth-first search means keeping careful track of what vertices have been visited and when.</p> <p>We compute timestamps for each vertex discovered in the search. A discovered vertex has two timestamps associated with it : its discovery time (in map <code>d</code>) and its finishing time (in map <code>f</code>) (a vertex is finished when its adjacency list has been completely examined). These timestamps are often useful in graph algorithms and aid in reasoning about the behavior of depth-first search.</p> <p>We color nodes during the search to help in the bookkeeping (map <code>color</code>). All vertices of the graph are initially <code>White</code>. When a vertex is discovered it is marked <code>Gray</code> and when it is finished, it is marked <code>Black</code>.</p> <p>If vertex <em>v</em> is discovered in the adjacency list of previously discovered node <em>u</em>, this fact is recorded in the predecessor subgraph (map <code>pred</code>).</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>module</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>M</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>GRAPH</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>struct</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>module</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-constant-language-capital-identifier'>Make</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Char</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>node</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>node</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>of_adjacency</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_right</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>empty</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>colors</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>White</span><span class='ocaml-keyword-other'>|</span><span class='ocaml-constant-language-capital-identifier'>Gray</span><span class='ocaml-keyword-other'>|</span><span class='ocaml-constant-language-capital-identifier'>Black</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>state</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'>discovery time</span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'>finishing time</span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>pred</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>char</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'>predecessor</span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>color</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>colors</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'>vertex colors</span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'>user specified type used by 'fold'</span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>dfs_fold</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-source'>fn</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>dfs_visit</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>u</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>d</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>pred</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>color</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>edge</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>state</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>find</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-source'>state</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>color</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>White</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>dfs_visit</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>state</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'>pred</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-source'>u</span><span class='ocaml-source'> </span><span class='ocaml-source'>state</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>pred</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>state</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>d</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>pred</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>color</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>edge</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>t</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>u</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>pred</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>color</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>u</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Gray</span><span class='ocaml-source'> </span><span class='ocaml-source'>color</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>fn</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'>u</span><span class='ocaml-source'>}</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>find</span><span class='ocaml-source'> </span><span class='ocaml-source'>u</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>d</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>u</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>pred</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>color</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>u</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Black</span><span class='ocaml-source'> </span><span class='ocaml-source'>color</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>v</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_left</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>k</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>bindings</span><span class='ocaml-source'> </span><span class='ocaml-source'>g</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>initial_state</span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>{</span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>empty</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>empty</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>pred</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>empty</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>color</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold_right</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>add</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>White</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Char_map</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>empty</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>acc</span><span class='ocaml-source'>}</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>snd</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>dfs_visit</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-source'>initial_state</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>acc</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>end</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>module</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>M</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>GRAPH</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="88" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Connected Components </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Write a predicate that splits a graph into its <a href="http://en.wikipedia.org/wiki/Connected_component_%28graph_theory%29">connected components</a>.</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> example pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="89" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Bipartite Graphs </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Write a predicate that finds out whether a given graph is <a href="http://en.wikipedia.org/wiki/Bipartite_graph">bipartite</a>.</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> example pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="90" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Generate K-Regular Simple Graphs With N Nodes </h2> <div class="inline px-3 py-1 bg-primary dark:bg-dark-primary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Advanced difficulty</div>"> Advanced </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>In a <a href="http://en.wikipedia.org/wiki/K-regular_graph">K-regular graph</a> all nodes have a degree of K; i.e. the number of edges incident in each node is K. How many (non-isomorphic!) 3-regular graphs with 6 nodes are there?</p> <p>See also the <a href="https://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/p94.txt">table of results</a>.</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> example pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="91" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Eight Queens Problem </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>This is a classical problem in computer science. The objective is to place eight queens on a chessboard so that no two queens are attacking each other; i.e., no two queens are in the same row, the same column, or on the same diagonal.</p> <p><strong>Hint:</strong> Represent the positions of the queens as a list of numbers 1..N. Example: <code>[4; 2; 7; 3; 6; 8; 5; 1]</code> means that the queen in the first column is in row 4, the queen in the second column is in row 2, etc. Use the generate-and-test paradigm.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>queens_positions</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><p>This is a brute force algorithm enumerating all possible solutions. For a deeper analysis, look for example to <a href="https://en.wikipedia.org/wiki/Eight_queens_puzzle">Wikipedia</a>.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>possible</span><span class='ocaml-source'> </span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-source'>col</span><span class='ocaml-source'> </span><span class='ocaml-source'>used_rows</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD1</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>not</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>mem</span><span class='ocaml-source'> </span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-source'>used_rows</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>mem</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>col</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>mem</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>col</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>queens_positions</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-source'>col</span><span class='ocaml-source'> </span><span class='ocaml-source'>used_rows</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD1</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>col</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>rev</span><span class='ocaml-source'> </span><span class='ocaml-source'>used_rows</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>col</span><span class='ocaml-source'> </span><span class='ocaml-source'>used_rows</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD1</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD2</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>@</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>possible</span><span class='ocaml-source'> </span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-source'>col</span><span class='ocaml-source'> </span><span class='ocaml-source'>used_rows</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD1</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>col</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>used_rows</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>col</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>row</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>col</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>usedD2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'>aux</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>possible</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>queens_positions</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="92" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Knight&#x27;s Tour </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Another famous problem is this one: How can a knight jump on an N×N chessboard in such a way that it visits every square exactly once?</p> <p><strong>Hint:</strong> Represent the squares by pairs of their coordinates <code>(x,y)</code>, where both <code>x</code> and <code>y</code> are integers between 1 and N. Define the function <code>jump n (x,y)</code> that returns all coordinates <code>(u,v)</code> to which a knight can jump from <code>(x,y)</code> to on a <code>n</code>×<code>n</code> chessboard. And finally, represent the solution of our problem as a list knight positions (the knight's tour).</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> example pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="93" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Von Koch&#x27;s Conjecture </h2> <div class="inline px-3 py-1 bg-primary dark:bg-dark-primary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Advanced difficulty</div>"> Advanced </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Several years ago I met a mathematician who was intrigued by a problem for which he didn't know a solution. His name was Von Koch, and I don't know whether the problem has been solved since.</p> <p><img src="/media/problems/von-koch1.gif" alt="Tree numbering" ></p> <p>Anyway, the puzzle goes like this: Given a tree with N nodes (and hence N-1 edges). Find a way to enumerate the nodes from 1 to N and, accordingly, the edges from 1 to N-1 in such a way, that for each edge K the difference of its node numbers equals to K. The conjecture is that this is always possible.</p> <p>For small trees the problem is easy to solve by hand. However, for larger trees, and 14 is already very large, it is extremely difficult to find a solution. And remember, we don't know for sure whether there is always a solution!</p> <p><img src="/media/problems/von-koch2.gif" alt="Larger tree" ></p> <p>Write a function that calculates a numbering scheme for a given tree. What is the solution for the larger tree pictured here?</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> example pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="94" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> An Arithmetic Puzzle </h2> <div class="inline px-3 py-1 bg-primary dark:bg-dark-primary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Advanced difficulty</div>"> Advanced </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Given a list of integer numbers, find a correct way of inserting arithmetic signs (operators) such that the result is a correct equation. Example: With the list of numbers <code>[2; 3; 5; 7; 11]</code> we can form the equations 2 - 3 + 5 + 7 = 11 or 2 = (3 * 5 + 7) / 11 (and ten others!).</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> example pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="95" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> English Number Words </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>On financial documents, like cheques, numbers must sometimes be written in full words. Example: 175 must be written as one-seven-five. Write a function <code>full_words</code> to print (non-negative) integer numbers in full words.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>full_words</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>175</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>one-seven-five</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>full_words</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>digit</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>[|</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>zero</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>one</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>two</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>three</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>four</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>five</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>six</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>seven</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>eight</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>nine</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>|]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>words</span><span class='ocaml-source'> </span><span class='ocaml-source'>w</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>w</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>digit</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'>)</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>w</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>words</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>digit</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>10</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>w</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>/</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>10</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>String</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>concat</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>-</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>words</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>full_words</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="96" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Syntax Checker </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/syntax-graph.gif" alt="Syntax graph" ></p> <p>In a certain programming language (Ada) identifiers are defined by the syntax diagram (railroad chart) opposite. Transform the syntax diagram into a system of syntax diagrams which do not contain loops; i.e. which are purely recursive. Using these modified diagrams, write a function <code>identifier : string -&gt; bool</code> that can check whether or not a given string is a legal identifier.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>identifier</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>this-is-a-long-identifier</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>identifier</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>is_letter</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'a'</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'z'</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>is_letter_or_digit</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_letter</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>||</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-string-quoted-single'>'0'</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'9'</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>is_valid</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-source'>not_after_dash</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>not_after_dash</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_letter_or_digit</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>[</span><span class='ocaml-source'>i</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_valid</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>[</span><span class='ocaml-source'>i</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-single'>'-'</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>not_after_dash</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_valid</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>String</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>length</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_letter</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>[</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_valid</span><span class='ocaml-source'> </span><span class='ocaml-source'>s</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'>)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>identifier</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>string</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="97" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Sudoku </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Sudoku puzzles go like this:</p> <pre><code class="language-text"> Problem statement Solution . . 4 | 8 . . | . 1 7 9 3 4 | 8 2 5 | 6 1 7 | | | | 6 7 . | 9 . . | . . . 6 7 2 | 9 1 4 | 8 5 3 | | | | 5 . 8 | . 3 . | . . 4 5 1 8 | 6 3 7 | 9 2 4 --------+---------+-------- --------+---------+-------- 3 . . | 7 4 . | 1 . . 3 2 5 | 7 4 8 | 1 6 9 | | | | . 6 9 | . . . | 7 8 . 4 6 9 | 1 5 3 | 7 8 2 | | | | . . 1 | . 6 9 | . . 5 7 8 1 | 2 6 9 | 4 3 5 --------+---------+-------- --------+---------+-------- 1 . . | . 8 . | 3 . 6 1 9 7 | 5 8 2 | 3 4 6 | | | | . . . | . . 6 | . 9 1 8 5 3 | 4 7 6 | 2 9 1 | | | | 2 4 . | . . 1 | 5 . . 2 4 6 | 3 9 1 | 5 7 8 </code></pre> <p>Every spot in the puzzle belongs to a (horizontal) row and a (vertical) column, as well as to one single 3x3 square (which we call &quot;square&quot; for short). At the beginning, some of the spots carry a single-digit number between 1 and 9. The problem is to fill the missing spots with digits in such a way that every number between 1 and 9 appears exactly once in each row, in each column, and in each square.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> The board representation is not imposed. Here </span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'>0</span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'> stands for </span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'>.</span><span class='ocaml-comment-block'>&quot;</span><span class='ocaml-comment-block'> </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><p>A simple way of resolving this is to use brute force. The idea is to start filling with available values in each case and test if it works. When there is no available values, it means we made a mistake so we go back to the last choice we made, and try a different choice.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>open</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Printf</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>module</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Board</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>struct</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>array</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> 9×9, row-major representation. A value of 0 </span><span class='ocaml-comment-block'> means undecided. </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>is_valid</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>c</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>get</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>get_as_string</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>get</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_valid</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>string_of_int</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>.</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>with_val</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Array</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>copy</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;-</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>of_list</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Array</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>make</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>81</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>iteri</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>List</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>iteri</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;-</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&amp;&amp;</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>e</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>r</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>print</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>for</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>to</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>do</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>for</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>to</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>do</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>printf</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-constant-character-printf'>%s</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'> | </span><span class='ocaml-constant-character-printf'>%s</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'> </span><span class='ocaml-constant-character-printf'>%s</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>get_as_string</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>done</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>printf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-constant-character-escape'>\n</span><span class='ocaml-string-quoted-double'>--------+---------+--------</span><span class='ocaml-constant-character-escape'>\n</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>printf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-constant-character-escape'>\n</span><span class='ocaml-string-quoted-double'> | | </span><span class='ocaml-constant-character-escape'>\n</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>printf</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-constant-character-escape'>\n</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>done</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>available</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>avail</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Array</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>make</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>10</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>true</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>for</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>to</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>do</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>avail</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>get</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;-</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>avail</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>get</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>i</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;-</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>done</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>sq_x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>and</span><span class='ocaml-source'> </span><span class='ocaml-source'>sq_y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>mod</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>for</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>sq_x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>to</span><span class='ocaml-source'> </span><span class='ocaml-source'>sq_x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>do</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>for</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>sq_y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>to</span><span class='ocaml-source'> </span><span class='ocaml-source'>sq_y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>do</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>avail</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>get</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;-</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-boolean'>false</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>done</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>done</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>av</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>ref</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>for</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> not 0 </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>to</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>9</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>do</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>avail</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>(</span><span class='ocaml-source'>i</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>av</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>i</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>!</span><span class='ocaml-source'>av</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>done</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>!</span><span class='ocaml-source'>av</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>next</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> Try to fill the undecided entries. </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>fill</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>y</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> filled all entries </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>is_valid</span><span class='ocaml-source'>(</span><span class='ocaml-source'>get</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-source'>fill</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>next</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>available</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> no solution </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>try_values</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>and</span><span class='ocaml-source'> </span><span class='ocaml-source'>try_values</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>function</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>fill</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>with_val</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'> </span><span class='ocaml-source'>v</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>next</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>as</span><span class='ocaml-source'> </span><span class='ocaml-source'>res</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>res</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>try_values</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>pos</span><span class='ocaml-source'> </span><span class='ocaml-source'>l</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>end</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>sudoku</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Board</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fill</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>b</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>None</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>failwith</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>sudoku: no solution</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>module</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Board</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>sig</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>type</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>t</span><span class='ocaml-source'> = int array </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>is_valid</span><span class='ocaml-source'> : int -&gt; bool </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>get</span><span class='ocaml-source'> : t -&gt; int * int -&gt; int </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>get_as_string</span><span class='ocaml-source'> : t -&gt; int * int -&gt; string </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>with_val</span><span class='ocaml-source'> : t -&gt; int * int -&gt; int -&gt; int array </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>of_list</span><span class='ocaml-source'> : int list list -&gt; t </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>print</span><span class='ocaml-source'> : t -&gt; unit </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>available</span><span class='ocaml-source'> : t -&gt; int * int -&gt; int list </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>next</span><span class='ocaml-source'> : int * int -&gt; int * int </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>fill</span><span class='ocaml-source'> : t -&gt; int * int -&gt; t option </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>val</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>try_values</span><span class='ocaml-source'> : t -&gt; int * int -&gt; int list -&gt; t option </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>end</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>sudoku</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Board</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Board</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>&lt;</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-keyword-operator'>&gt;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="98" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Nonograms </h2> <div class="inline px-3 py-1 bg-primary dark:bg-dark-primary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Advanced difficulty</div>"> Advanced </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Around 1994, a certain kind of puzzles was very popular in England. The &quot;Sunday Telegraph&quot; newspaper wrote: &quot;Nonograms are puzzles from Japan and are currently published each week only in The Sunday Telegraph. Simply use your logic and skill to complete the grid and reveal a picture or diagram.&quot; As an OCaml programmer, you are in a better situation: you can have your computer do the work!</p> <p>The puzzle goes like this: Essentially, each row and column of a rectangular bitmap is annotated with the respective lengths of its distinct strings of occupied cells. The person who solves the puzzle must complete the bitmap given only these lengths.</p> <pre><code class="language-text"> Problem statement: Solution: |_|_|_|_|_|_|_|_| 3 |_|X|X|X|_|_|_|_| 3 |_|_|_|_|_|_|_|_| 2 1 |X|X|_|X|_|_|_|_| 2 1 |_|_|_|_|_|_|_|_| 3 2 |_|X|X|X|_|_|X|X| 3 2 |_|_|_|_|_|_|_|_| 2 2 |_|_|X|X|_|_|X|X| 2 2 |_|_|_|_|_|_|_|_| 6 |_|_|X|X|X|X|X|X| 6 |_|_|_|_|_|_|_|_| 1 5 |X|_|X|X|X|X|X|_| 1 5 |_|_|_|_|_|_|_|_| 6 |X|X|X|X|X|X|_|_| 6 |_|_|_|_|_|_|_|_| 1 |_|_|_|_|X|_|_|_| 1 |_|_|_|_|_|_|_|_| 2 |_|_|_|X|X|_|_|_| 2 1 3 1 7 5 3 4 3 1 3 1 7 5 3 4 3 2 1 5 1 2 1 5 1 </code></pre> <p>For the example above, the problem can be stated as the two lists <code>[[3]; [2; 1]; [3; 2]; [2; 2]; [6]; [1; 5]; [6]; [1]; [2]]</code> and <code>[[1; 2]; [3; 1]; [1; 5]; [7; 1]; [5]; [3]; [4]; [3]]</code> which give the &quot;solid&quot; lengths of the rows and columns, top-to-bottom and left-to-right, respectively. Published puzzles are larger than this example, e.g. 25×20, and apparently always have unique solutions.</p> <!-- $MDX skip --> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-source'>solve</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> <div x-show="!statement"><p>Brute force solution: construct boards trying all the fill possibilities for the columns given the prescribed patterns for them and reject the solution if it does not satisfy the row patterns.</p> <pre><code><span class='ocaml-keyword-other'>#</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>element</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>X</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> ensure we do not miss cases in patterns </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-source'>element</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Empty</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>X</span><span class='ocaml-source'> </span></code></pre> <p>You may want to look at <a href="http://link.springer.com/article/10.1007%2Fs10489-009-0200-0">more efficient algorithms</a> and implement them so you can solve the following within reasonable time:</p> <!-- $MDX skip --> <pre><code><span class='ocaml-source'> </span><span class='ocaml-source'>solve</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>14</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>8</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>12</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>5</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>6</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>4</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>3</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>2</span><span class='ocaml-source'>]</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>[</span><span class='ocaml-constant-numeric-decimal-integer'>7</span><span class='ocaml-source'>]</span><span class='ocaml-source'>]</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="99" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Crossword Puzzle </h2> <div class="inline px-3 py-1 bg-primary dark:bg-dark-primary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Advanced difficulty</div>"> Advanced </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p><img src="/media/problems/crossword.gif" alt="Crossword" ></p> <p>Given an empty (or almost empty) framework of a crossword puzzle and a set of words. The problem is to place the words into the framework.</p> <p>The particular crossword puzzle is specified in a text file which first lists the words (one word per line) in an arbitrary order. Then, after an empty line, the crossword framework is defined. In this framework specification, an empty character location is represented by a dot (.). In order to make the solution easier, character locations can also contain predefined character values. The puzzle above is defined in the file <a href="https://sites.google.com/site/prologsite/prolog-problems/7/solutions-7/p7_09a.dat?attredirects=0&amp;d=1">p7_09a.dat</a>, other examples are <a href="https://sites.google.com/site/prologsite/prolog-problems/7/solutions-7/p7_09b.dat?attredirects=0&amp;d=1">p7_09b.dat</a> and <a href="https://sites.google.com/site/prologsite/prolog-problems/7/solutions-7/p7_09d.dat?attredirects=0&amp;d=1">p7_09d.dat</a>. There is also an example of a puzzle (<a href="https://sites.google.com/site/prologsite/prolog-problems/7/solutions-7/p7_09c.dat?attredirects=0&amp;d=1">p7_09c.dat</a>) which does not have a solution.</p> <p>Words are strings (character lists) of at least two characters. A horizontal or vertical sequence of character places in the crossword puzzle framework is called a site. Our problem is to find a compatible way of placing words onto sites.</p> <p><strong>Hints:</strong></p> <ol> <li>The problem is not easy. You will need some time to thoroughly understand it. So, don't give up too early! And remember that the objective is a clean solution, not just a quick-and-dirty hack!</li> <li>For efficiency reasons it is important, at least for larger puzzles, to sort the words and the sites in a particular order.</li> </ol> </div> <div x-show="!statement"><pre><code><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> example pending </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="100" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Never-Ending Sequences </h2> <div class="inline px-3 py-1 bg-secondary dark:bg-dark-secondary rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Beginner difficulty</div>"> Beginner </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Lists are finite, meaning they always contain a finite number of elements. Sequences may be finite or infinite.</p> <p>The goal of this exercise is to define a type <code>'a stream</code> which only contains infinite sequences. Using this type, define the following functions:</p> <pre><code><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>hd</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> Returns the first element of a stream </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> Removes the first element of a stream </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>take</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>list</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> [take n seq] returns the n first values of [seq] </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>unfold</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> Similar to Seq.unfold </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>bang</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> [bang x] produces an infinitely repeating sequence of [x] values. </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>ints</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>int</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-comment-block'>(*</span><span class='ocaml-comment-block'> Similar to Seq.ints </span><span class='ocaml-comment-block'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> Similar to List.map and Seq.map </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>filter</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>bool</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> Similar to List.filter and Seq.filter </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>iter</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>unit</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'b</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> Similar to List.iter and Seq.iter </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>to_seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> Translates an ['a stream] into an ['a Seq.t] </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>val</span><span class='ocaml-source'> </span><span class='ocaml-source'>of_seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>t</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-comment-doc'>(**</span><span class='ocaml-comment-doc'> Translates an ['a Seq.t] into an ['a stream] </span><span class='ocaml-comment-doc'> @raise Failure if the input sequence is finite. </span><span class='ocaml-comment-doc'>*)</span><span class='ocaml-source'> </span></code></pre> <p><strong>Tip:</strong> Use <code>let ... =</code> patterns.</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword-other'>type</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>cons</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>of</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>*</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>and</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-support-type'>unit</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>cons</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>hd</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language'>_</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>tl</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-storage-type'>'a</span><span class='ocaml-source'> </span><span class='ocaml-source'>stream</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language'>_</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>take</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>0</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-list'>[]</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'> </span><span class='ocaml-source'>take</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>n</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>unfold</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>y</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>y</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>unfold</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>bang</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>unfold</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>ints</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>unfold</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>+</span><span class='ocaml-source'> </span><span class='ocaml-constant-numeric-decimal-integer'>1</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>filter</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>filter</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>if</span><span class='ocaml-source'> </span><span class='ocaml-source'>p</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>then</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>else</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>iter</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-separator-terminator punctuation-separator'>;</span><span class='ocaml-source'> </span><span class='ocaml-source'>iter</span><span class='ocaml-source'> </span><span class='ocaml-source'>f</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>to_seq</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>unfold</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-keyword-other'>fun</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Some</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>hd</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>of_seq</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>match</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>with</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-constant-language-capital-identifier'>Nil</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-source'>failwith</span><span class='ocaml-source'> </span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-string-quoted-double'>Not a infinite sequence</span><span class='ocaml-string-quoted-double'>&quot;</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>|</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>-&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>(</span><span class='ocaml-source'>x</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>of_seq</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq</span><span class='ocaml-source'>)</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <div x-data="{statement: true}" id="101" > <div class="flex mb-4 flex-col gap-4 md:flex-row md:items-center"> <h2 class="my-0 font-normal text-2xl text-title dark:text-dark-title"> Diagonal of a Sequence of Sequences </h2> <div class="inline px-3 py-1 bg-tertiary dark:bg-dark-tertiary_lighter rounded-3xl font-semibold text-center text-white dark:text-dark-white tracking-widest leading-5" title="Intermediate difficulty</div>"> Intermediate </div> </div> <div class="flex"> <button class="px-10 h-11 text-base font-medium rounded-l-md border flex gap-2 items-center" x-on:click="statement = true" :class="statement ? 'bg-primary dark:bg-dark-primary border-primary dark:border-dark-primary text-white dark:text-dark-white': 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30' " > <svg class="h-5 w-5" width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5" aria-hidden="true"> <path id="Vector" d="M13 4.97177C13 4.61677 13.186 4.29577 13.401 4.01277C13.622 3.72277 13.75 3.37877 13.75 3.00977C13.75 1.97377 12.743 1.13477 11.5 1.13477C10.257 1.13477 9.25 1.97477 9.25 3.00977C9.25 3.37877 9.378 3.72277 9.599 4.01277C9.814 4.29577 10 4.61677 10 4.97177C10.0004 5.05751 9.98363 5.14247 9.95059 5.2216C9.91755 5.30073 9.86894 5.37241 9.80766 5.43239C9.74637 5.49236 9.67366 5.53941 9.59384 5.57074C9.51402 5.60207 9.42872 5.61704 9.343 5.61477C7.95166 5.57469 6.56271 5.47459 5.18 5.31477C5.366 6.92777 5.473 8.56477 5.495 10.2218C5.49593 10.3087 5.47958 10.3949 5.4469 10.4754C5.41422 10.556 5.36587 10.6292 5.30465 10.6909C5.24343 10.7526 5.17056 10.8015 5.09028 10.8347C5.01 10.868 4.92391 10.885 4.837 10.8848C4.482 10.8848 4.161 10.6988 3.878 10.4838C3.59137 10.2599 3.2387 10.1372 2.875 10.1348C1.839 10.1348 1 11.1418 1 12.3848C1 13.6278 1.84 14.6348 2.875 14.6348C3.244 14.6348 3.588 14.5068 3.878 14.2858C4.161 14.0708 4.482 13.8848 4.837 13.8848C5.147 13.8848 5.392 14.1448 5.369 14.4548C5.24461 16.1503 5.03031 17.838 4.727 19.5108C6.245 19.7008 7.785 19.8198 9.343 19.8648C9.42872 19.867 9.51402 19.8521 9.59384 19.8207C9.67366 19.7894 9.74637 19.7424 9.80766 19.6824C9.86894 19.6224 9.91755 19.5507 9.95059 19.4716C9.98363 19.3925 10.0004 19.3075 10 19.2218C10 18.8668 9.814 18.5458 9.599 18.2628C9.37511 17.9761 9.2524 17.6235 9.25 17.2598C9.25 16.2248 10.258 15.3848 11.5 15.3848C12.743 15.3848 13.75 16.2248 13.75 17.2598C13.75 17.6288 13.622 17.9728 13.401 18.2628C13.186 18.5458 13.001 18.8668 13.001 19.2218C13.001 19.5548 13.278 19.8208 13.611 19.8018C15.4305 19.6948 17.2424 19.4844 19.038 19.1718C19.3097 17.61 19.5039 16.0357 19.62 14.4548C19.6252 14.3817 19.6153 14.3083 19.5909 14.2393C19.5664 14.1702 19.528 14.1069 19.4779 14.0534C19.4279 13.9999 19.3673 13.9573 19.3001 13.9283C19.2328 13.8993 19.1603 13.8844 19.087 13.8848C18.732 13.8848 18.411 14.0708 18.128 14.2858C17.838 14.5068 17.494 14.6348 17.125 14.6348C16.09 14.6348 15.25 13.6278 15.25 12.3848C15.25 11.1418 16.09 10.1348 17.125 10.1348C17.495 10.1348 17.838 10.2628 18.128 10.4838C18.411 10.6988 18.732 10.8848 19.088 10.8848C19.1749 10.885 19.261 10.868 19.3413 10.8347C19.4216 10.8015 19.4944 10.7526 19.5557 10.6909C19.6169 10.6292 19.6652 10.556 19.6979 10.4754C19.7306 10.3949 19.7469 10.3087 19.746 10.2218C19.722 8.42963 19.5984 6.64021 19.376 4.86177C17.49 5.20377 15.566 5.43577 13.61 5.55077C13.5313 5.55513 13.4525 5.54335 13.3785 5.51613C13.3045 5.48892 13.2369 5.44686 13.1797 5.39253C13.1226 5.3382 13.0772 5.27276 13.0463 5.20023C13.0153 5.1277 12.9996 5.05061 13 4.97177Z" stroke-linecap="round" stroke-linejoin="round"/> </svg> Statement </button> <button class="px-10 h-11 text-base font-medium rounded-r-md border-t border-r border-b flex gap-2 items-center" x-on:click="statement = false" :class="statement ? 'text-separator_20 dark:text-dark-separator_30 border-separator_20 dark:border-dark-separator_30': 'bg-secondary dark:bg-dark-secondary border-secondary dark:border-dark-secondary text-white dark:text-dark-white' " > <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" /> </svg> Solution </button> </div> <div x-show="statement"><p>Write a function <code>diag : 'a Seq.t Seq.t -&gt; 'a Seq</code> that returns the <em>diagonal</em> of a sequence of sequences. The returned sequence is formed as follows: The first element of the returned sequence is the first element of the first sequence; the second element of the returned sequence is the second element of the second sequence; the third element of the returned sequence is the third element of the third sequence; and so on.</p> </div> <div x-show="!statement"><pre><code><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-keyword'>rec </span><span class='ocaml-entity-name-function-binding'>diag</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq_seq</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>hds</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>tls</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>filter_map</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>uncons</span><span class='ocaml-source'> </span><span class='ocaml-source'>seq_seq</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>|&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>split</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-source'>hd</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>uncons</span><span class='ocaml-source'> </span><span class='ocaml-source'>hds</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>|&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Option</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>fst</span><span class='ocaml-keyword-other-ocaml punctuation-comma punctuation-separator'>,</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>uncons</span><span class='ocaml-source'> </span><span class='ocaml-source'>tls</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>|&gt;</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Option</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>map</span><span class='ocaml-source'> </span><span class='ocaml-source'>snd</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-keyword'>let</span><span class='ocaml-source'> </span><span class='ocaml-entity-name-function-binding'>d</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Option</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>none</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>empty</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>some</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-source'>diag</span><span class='ocaml-source'> </span><span class='ocaml-source'>tl</span><span class='ocaml-source'> </span><span class='ocaml-keyword-other'>in</span><span class='ocaml-source'> </span><span class='ocaml-source'> </span><span class='ocaml-constant-language-capital-identifier'>Option</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>fold</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>none</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-constant-language-capital-identifier'>Fun</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>id</span><span class='ocaml-source'> ~</span><span class='ocaml-source'>some</span><span class='ocaml-keyword-other-ocaml punctuation-other-colon punctuation'>:</span><span class='ocaml-constant-language-capital-identifier'>Seq</span><span class='ocaml-keyword-other-ocaml punctuation-other-period punctuation-separator'>.</span><span class='ocaml-source'>cons</span><span class='ocaml-source'> </span><span class='ocaml-source'>hd</span><span class='ocaml-source'> </span><span class='ocaml-source'>d</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span></code></pre> </div> </div> <div class="my-12"></div> <script> document.addEventListener('alpine:init', () => { function computeSectionYPositions(toc_el) { function get_y(href) { let heading = document.getElementById(href.substring(1)); return heading.getBoundingClientRect().top + window.scrollY - 60; } let sections = {}; let els = toc_el.querySelectorAll("a"); let l = els.length; for (let i=0; i<l; i++) { let target_href = els[i].getAttribute("href"); let next_el_href = i < l-1 ? els[i+1].getAttribute("href") : null; sections[target_href] = { start: get_y(target_href), end: next_el_href ? get_y(next_el_href) : null, } } return sections; } function isWithin(scrollY, href, sectionYPositions) { if (sectionYPositions == null) return false; return scrollY > sectionYPositions[href].start && (scrollY <= sectionYPositions[href].end || sectionYPositions[href].end == null) } Alpine.data('exercises', () => ( { scrollY: window.scrollY, sectionYPositions: null, isWithin, computeSectionYPositions, } )) }) </script> </div> <footer x-data class="flex flex-col gap-3 md:gap-5 pt-5 mt-6 border-separator_20 dark:border-dark-separator_30 border-t"> <div class="flex w-full items-center justify-between"> <a href="/"> <img class="h-8 dark:hidden" src="/_/MDBjN2M4YjIxY2Y1YWExZWE3Njk4M2FiZTg1NWZjNTM/logo-with-name.svg" alt="OCaml"> <img src="/_/MTE3YWE5YzJlZmExNmYyMmI3ZmEwYjliMDEwNDBkMDY/logo-with-name-white.svg" width="132" alt="OCaml logo" class="h-8 hidden dark:inline"> </a> <div class="hidden md:flex"> <button class="flex grow basis-0 justify-center items-center py-2 px-4 text-sm border rounded-l rounded-l-full border-r-0" :class='$store.themeSettings.preference === "light" ? "bg-gradient-to-r from-[#FFA932] to-[#C24F1E] text-white dark:text-dark-title outline-none" : "bg-white text-content outline-2 outline-card_border dark:outline-dark-separator_30"' @click='$store.themeSettings.setPreference("light")' > <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 14 14" fill="none" class="mr-2"> <path d="M7 0.5V2.125M11.5962 2.40378L10.4472 3.55283M13.5 7H11.875M11.5962 11.5962L10.4472 10.4472M7 11.875V13.5M3.55283 10.4472L2.40378 11.5962M2.125 7H0.5M3.55283 3.55283L2.40378 2.40378M9.70833 7C9.70833 7.71829 9.42299 8.40717 8.91508 8.91508C8.40717 9.42299 7.71829 9.70833 7 9.70833C6.28171 9.70833 5.59283 9.42299 5.08492 8.91508C4.57701 8.40717 4.29167 7.71829 4.29167 7C4.29167 6.28171 4.57701 5.59283 5.08492 5.08492C5.59283 4.57701 6.28171 4.29167 7 4.29167C7.71829 4.29167 8.40717 4.57701 8.91508 5.08492C9.42299 5.59283 9.70833 6.28171 9.70833 7Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/> </svg> Light </button> <button class="flex grow basis-0 justify-center items-center py-2 px-4 text-sm border border-r-0" :class='$store.themeSettings.preference === "dark" ? "bg-gradient-to-r from-[#0F254F] to-[#0B1228] text-white dark:text-dark-title outline-none" : "bg-white text-content outline-2 outline-card_border dark:outline-dark-separator_30"' @click='$store.themeSettings.setPreference("dark")' > <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" class="mr-2"> <path d="M11.1573 7.90842C10.5134 8.17678 9.8226 8.3145 9.125 8.31358C6.20812 8.31358 3.84375 5.94921 3.84375 3.03233C3.84375 2.31192 3.98783 1.62562 4.24892 1C3.28663 1.40144 2.46465 2.07865 1.88651 2.94635C1.30836 3.81404 0.999911 4.83342 1 5.87608C1 8.79296 3.36437 11.1573 6.28125 11.1573C7.32391 11.1574 8.34329 10.849 9.21099 10.2708C10.0787 9.69268 10.7559 8.8707 11.1573 7.90842Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/> </svg> Dark </button> <button class="flex grow basis-0 justify-center items-center py-2 px-4 text-sm border rounded-r rounded-r-full" :class='$store.themeSettings.preference === "system" ? "bg-gradient-to-r from-[#2B7866] to-[#004039] text-white dark:text-dark-title outline-none" : "bg-white text-content outline-2 outline-card_border dark:outline-dark-separator_30"' @click='$store.themeSettings.setPreference("system")' > <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" class="mr-2"> <path d="M7.19553 2.955C7.26303 2.5485 7.61553 2.25 8.02803 2.25H9.97278C10.3853 2.25 10.7378 2.5485 10.8053 2.955L10.965 3.91575C11.0123 4.19625 11.1998 4.43025 11.4488 4.56825C11.5043 4.59825 11.559 4.6305 11.6138 4.6635C11.8575 4.8105 12.1538 4.85625 12.42 4.7565L13.3328 4.4145C13.5198 4.34416 13.7258 4.34248 13.9139 4.40978C14.1021 4.47708 14.2603 4.60899 14.3603 4.782L15.3323 6.46725C15.4321 6.64028 15.4672 6.8431 15.4315 7.03962C15.3958 7.23615 15.2916 7.41363 15.1373 7.5405L14.385 8.16075C14.1653 8.3415 14.0565 8.6205 14.0625 8.90475C14.0636 8.9685 14.0636 9.03225 14.0625 9.096C14.0565 9.3795 14.1653 9.6585 14.385 9.83925L15.138 10.4595C15.456 10.722 15.5385 11.1758 15.333 11.532L14.3595 13.2172C14.2597 13.3902 14.1017 13.5222 13.9137 13.5896C13.7257 13.657 13.5198 13.6556 13.3328 13.5855L12.42 13.2435C12.1538 13.1438 11.8575 13.1895 11.613 13.3365C11.5587 13.3696 11.5037 13.4016 11.448 13.4325C11.1998 13.5697 11.0123 13.8037 10.965 14.0842L10.8053 15.045C10.7378 15.4523 10.3853 15.75 9.97278 15.75H8.02728C7.61478 15.75 7.26303 15.4515 7.19478 15.045L7.03503 14.0842C6.98853 13.8037 6.80103 13.5698 6.55203 13.4318C6.49642 13.4011 6.44141 13.3693 6.38703 13.3365C6.14328 13.1895 5.84703 13.1438 5.58003 13.2435L4.66728 13.5855C4.48033 13.6556 4.27456 13.6572 4.08656 13.5899C3.89856 13.5226 3.74051 13.3908 3.64053 13.218L2.66778 11.5328C2.56799 11.3597 2.53282 11.1569 2.56852 10.9604C2.60423 10.7639 2.7085 10.5864 2.86278 10.4595L3.61578 9.83925C3.83478 9.65925 3.94353 9.3795 3.93828 9.096C3.93711 9.03226 3.93711 8.96849 3.93828 8.90475C3.94353 8.61975 3.83478 8.3415 3.61578 8.16075L2.86278 7.5405C2.70869 7.41367 2.60454 7.23633 2.56884 7.03997C2.53314 6.84361 2.56819 6.64095 2.66778 6.468L3.64053 4.78275C3.74042 4.6096 3.89854 4.47754 4.08672 4.4101C4.27489 4.34266 4.4809 4.34422 4.66803 4.4145L5.58003 4.7565C5.84703 4.85625 6.14328 4.8105 6.38703 4.6635C6.44103 4.6305 6.49653 4.599 6.55203 4.5675C6.80103 4.43025 6.98853 4.19625 7.03503 3.91575L7.19553 2.955Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/> <path d="M11.25 9C11.25 9.59674 11.0129 10.169 10.591 10.591C10.169 11.0129 9.59674 11.25 9 11.25C8.40326 11.25 7.83097 11.0129 7.40901 10.591C6.98705 10.169 6.75 9.59674 6.75 9C6.75 8.40326 6.98705 7.83097 7.40901 7.40901C7.83097 6.98705 8.40326 6.75 9 6.75C9.59674 6.75 10.169 6.98705 10.591 7.40901C11.0129 7.83097 11.25 8.40326 11.25 9Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/> </svg> System </button> </div> </div> <section class="flex flex-col gap-6 md:gap-0 md:flex-row md:justify-between md:items-end"> <div> <p class="text-base font-normal leading-6 text-content dark:text-dark-content mt-2">Innovation. Community. Security.</p> </div> <div class="flex items-center gap-4 pr-4"> <a href="https://github.com/ocaml" class="text-title dark:text-dark-title hover:text-primary dark:hover:text-dark-primary"> <span class="sr-only">GitHub</span> <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"> <path fill-rule="evenodd" d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z" clip-rule="evenodd" /> </svg> </a> <a href="https://discord.gg/cCYQbqN" class="text-title dark:text-dark-title hover:text-primary dark:hover:text-dark-primary"> <span class="sr-only">Discord</span> <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"> <path fill-rule="evenodd" d="M18.9419 5.29661C17.6473 4.69088 16.263 4.25066 14.8157 4C14.638 4.32134 14.4304 4.75355 14.2872 5.09738C12.7487 4.86601 11.2245 4.86601 9.7143 5.09738C9.57116 4.75355 9.3588 4.32134 9.17947 4C7.73067 4.25066 6.3448 4.6925 5.05016 5.29982C2.43887 9.24582 1.73099 13.0938 2.08493 16.8872C3.81688 18.1805 5.49534 18.9662 7.14548 19.4804C7.55291 18.9196 7.91628 18.3235 8.22931 17.6953C7.63313 17.4688 7.06211 17.1892 6.52256 16.8647C6.6657 16.7586 6.80571 16.6478 6.94098 16.5337C10.2318 18.0729 13.8074 18.0729 17.0589 16.5337C17.1958 16.6478 17.3358 16.7586 17.4774 16.8647C16.9362 17.1908 16.3637 17.4704 15.7675 17.697C16.0805 18.3235 16.4423 18.9212 16.8513 19.4819C18.503 18.9678 20.183 18.1822 21.915 16.8872C22.3303 12.4897 21.2056 8.67705 18.9419 5.29661ZM8.67765 14.5543C7.68977 14.5543 6.87963 13.632 6.87963 12.509C6.87963 11.3859 7.67247 10.4621 8.67765 10.4621C9.68285 10.4621 10.493 11.3843 10.4757 12.509C10.4772 13.632 9.68285 14.5543 8.67765 14.5543ZM15.3223 14.5543C14.3344 14.5543 13.5243 13.632 13.5243 12.509C13.5243 11.3859 14.3171 10.4621 15.3223 10.4621C16.3275 10.4621 17.1376 11.3843 17.1203 12.509C17.1203 13.632 16.3275 14.5543 15.3223 14.5543Z" clip-rule="evenodd" /> </svg> </a> <a href="https://twitter.com/ocaml_org" class="text-title dark:text-dark-title hover:text-primary dark:hover:text-dark-primary"> <span class="sr-only">Twitter</span> <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"> <path d="M8.29 20.251c7.547 0 11.675-6.253 11.675-11.675 0-.178 0-.355-.012-.53A8.348 8.348 0 0022 5.92a8.19 8.19 0 01-2.357.646 4.118 4.118 0 001.804-2.27 8.224 8.224 0 01-2.605.996 4.107 4.107 0 00-6.993 3.743 11.65 11.65 0 01-8.457-4.287 4.106 4.106 0 001.27 5.477A4.072 4.072 0 012.8 9.713v.052a4.105 4.105 0 003.292 4.022 4.095 4.095 0 01-1.853.07 4.108 4.108 0 003.834 2.85A8.233 8.233 0 012 18.407a11.616 11.616 0 006.29 1.84" /> </svg> </a> <a href="https://watch.ocaml.org/" class="text-title dark:text-dark-title hover:text-primary dark:hover:text-dark-primary"> <span class="sr-only">Peertube</span> <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"> <path fill-rule="evenodd" d="M5 2V12L12.4998 7.00047" clip-rule="evenodd" /> <path fill-rule="evenodd" d="M5 12V22L12.4998 17.0005" clip-rule="evenodd" /> <path fill-rule="evenodd" d="M12.4998 6.99951V16.9995L19.9995 12" clip-rule="evenodd" /> </svg> </a> <a href="/planet.xml" class="text-title dark:text-dark-title hover:text-primary dark:hover:text-dark-primary"> <span class="sr-only">RSS</span> <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"> <path fill-rule="evenodd" d="M15.2692 22H11.3846C11.3846 16.8462 7.11538 12.6154 2 12.6154V8.73077C9.26923 8.73077 15.2692 14.7308 15.2692 22ZM18 22C18 13.2308 10.7692 6 2 6V2C12.9615 2 22 11.0385 22 22H18Z" clip-rule="evenodd" /> <path fill-rule="evenodd" d="M4.73077 22C6.23893 22 7.46154 20.7774 7.46154 19.2692C7.46154 17.7611 6.23893 16.5385 4.73077 16.5385C3.22261 16.5385 2 17.7611 2 19.2692C2 20.7774 3.22261 22 4.73077 22Z" clip-rule="evenodd" /> </svg> </a> </div> <div class="flex w-full md:hidden"> <button class="flex grow basis-0 justify-center items-center py-2 px-4 text-sm border rounded-l rounded-l-full border-r-0" :class='$store.themeSettings.preference === "light" ? "bg-gradient-to-r from-[#FFA932] to-[#C24F1E] text-white dark:text-dark-title outline-none" : "bg-white text-content outline-2 outline-card_border dark:outline-dark-separator_30"' @click='$store.themeSettings.setPreference("light")' > <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 14 14" fill="none" class="mr-2"> <path d="M7 0.5V2.125M11.5962 2.40378L10.4472 3.55283M13.5 7H11.875M11.5962 11.5962L10.4472 10.4472M7 11.875V13.5M3.55283 10.4472L2.40378 11.5962M2.125 7H0.5M3.55283 3.55283L2.40378 2.40378M9.70833 7C9.70833 7.71829 9.42299 8.40717 8.91508 8.91508C8.40717 9.42299 7.71829 9.70833 7 9.70833C6.28171 9.70833 5.59283 9.42299 5.08492 8.91508C4.57701 8.40717 4.29167 7.71829 4.29167 7C4.29167 6.28171 4.57701 5.59283 5.08492 5.08492C5.59283 4.57701 6.28171 4.29167 7 4.29167C7.71829 4.29167 8.40717 4.57701 8.91508 5.08492C9.42299 5.59283 9.70833 6.28171 9.70833 7Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/> </svg> Light </button> <button class="flex grow basis-0 justify-center items-center py-2 px-4 text-sm border border-r-0" :class='$store.themeSettings.preference === "dark" ? "bg-gradient-to-r from-[#0F254F] to-[#0B1228] text-white dark:text-dark-title outline-none" : "bg-white text-content outline-2 outline-card_border dark:outline-dark-separator_30"' @click='$store.themeSettings.setPreference("dark")' > <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" class="mr-2"> <path d="M11.1573 7.90842C10.5134 8.17678 9.8226 8.3145 9.125 8.31358C6.20812 8.31358 3.84375 5.94921 3.84375 3.03233C3.84375 2.31192 3.98783 1.62562 4.24892 1C3.28663 1.40144 2.46465 2.07865 1.88651 2.94635C1.30836 3.81404 0.999911 4.83342 1 5.87608C1 8.79296 3.36437 11.1573 6.28125 11.1573C7.32391 11.1574 8.34329 10.849 9.21099 10.2708C10.0787 9.69268 10.7559 8.8707 11.1573 7.90842Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/> </svg> Dark </button> <button class="flex grow basis-0 justify-center items-center py-2 px-4 text-sm border rounded-r rounded-r-full" :class='$store.themeSettings.preference === "system" ? "bg-gradient-to-r from-[#2B7866] to-[#004039] text-white dark:text-dark-title outline-none" : "bg-white text-content outline-2 outline-card_border dark:outline-dark-separator_30"' @click='$store.themeSettings.setPreference("system")' > <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" class="mr-2"> <path d="M7.19553 2.955C7.26303 2.5485 7.61553 2.25 8.02803 2.25H9.97278C10.3853 2.25 10.7378 2.5485 10.8053 2.955L10.965 3.91575C11.0123 4.19625 11.1998 4.43025 11.4488 4.56825C11.5043 4.59825 11.559 4.6305 11.6138 4.6635C11.8575 4.8105 12.1538 4.85625 12.42 4.7565L13.3328 4.4145C13.5198 4.34416 13.7258 4.34248 13.9139 4.40978C14.1021 4.47708 14.2603 4.60899 14.3603 4.782L15.3323 6.46725C15.4321 6.64028 15.4672 6.8431 15.4315 7.03962C15.3958 7.23615 15.2916 7.41363 15.1373 7.5405L14.385 8.16075C14.1653 8.3415 14.0565 8.6205 14.0625 8.90475C14.0636 8.9685 14.0636 9.03225 14.0625 9.096C14.0565 9.3795 14.1653 9.6585 14.385 9.83925L15.138 10.4595C15.456 10.722 15.5385 11.1758 15.333 11.532L14.3595 13.2172C14.2597 13.3902 14.1017 13.5222 13.9137 13.5896C13.7257 13.657 13.5198 13.6556 13.3328 13.5855L12.42 13.2435C12.1538 13.1438 11.8575 13.1895 11.613 13.3365C11.5587 13.3696 11.5037 13.4016 11.448 13.4325C11.1998 13.5697 11.0123 13.8037 10.965 14.0842L10.8053 15.045C10.7378 15.4523 10.3853 15.75 9.97278 15.75H8.02728C7.61478 15.75 7.26303 15.4515 7.19478 15.045L7.03503 14.0842C6.98853 13.8037 6.80103 13.5698 6.55203 13.4318C6.49642 13.4011 6.44141 13.3693 6.38703 13.3365C6.14328 13.1895 5.84703 13.1438 5.58003 13.2435L4.66728 13.5855C4.48033 13.6556 4.27456 13.6572 4.08656 13.5899C3.89856 13.5226 3.74051 13.3908 3.64053 13.218L2.66778 11.5328C2.56799 11.3597 2.53282 11.1569 2.56852 10.9604C2.60423 10.7639 2.7085 10.5864 2.86278 10.4595L3.61578 9.83925C3.83478 9.65925 3.94353 9.3795 3.93828 9.096C3.93711 9.03226 3.93711 8.96849 3.93828 8.90475C3.94353 8.61975 3.83478 8.3415 3.61578 8.16075L2.86278 7.5405C2.70869 7.41367 2.60454 7.23633 2.56884 7.03997C2.53314 6.84361 2.56819 6.64095 2.66778 6.468L3.64053 4.78275C3.74042 4.6096 3.89854 4.47754 4.08672 4.4101C4.27489 4.34266 4.4809 4.34422 4.66803 4.4145L5.58003 4.7565C5.84703 4.85625 6.14328 4.8105 6.38703 4.6635C6.44103 4.6305 6.49653 4.599 6.55203 4.5675C6.80103 4.43025 6.98853 4.19625 7.03503 3.91575L7.19553 2.955Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/> <path d="M11.25 9C11.25 9.59674 11.0129 10.169 10.591 10.591C10.169 11.0129 9.59674 11.25 9 11.25C8.40326 11.25 7.83097 11.0129 7.40901 10.591C6.98705 10.169 6.75 9.59674 6.75 9C6.75 8.40326 6.98705 7.83097 7.40901 7.40901C7.83097 6.98705 8.40326 6.75 9 6.75C9.59674 6.75 10.169 6.98705 10.591 7.40901C11.0129 7.83097 11.25 8.40326 11.25 9Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/> </svg> System </button> </div> </section> <section class="mt-4 grid grid-cols-2"> <div class="flex flex-col gap-1"> <div class="flex flex-col"> <h5 class="font-bold text-base leading-7 py-2.5 text-title dark:text-dark-title">About</h5> <a href="/changelog" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Changelog</a> <a href="/releases" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Releases</a> <a href="/industrial-users" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Industrial Users</a> <a href="/academic-users" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Academic Users</a> <a href="/about" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Why OCaml</a> </div> <div class="flex flex-col"> <h5 class="font-bold text-base leading-7 py-2.5 text-title dark:text-dark-title"> Ecosystem </h5> <a href="/packages" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Packages</a> <a href="/community" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Community</a> <a href="/events" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Events</a> <a href="/ocaml-planet" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">OCaml Planet</a> <a href="/jobs" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Jobs</a> </div> </div> <div class="flex flex-col"> <h5 class="font-bold text-base leading-7 py-2.5 text-title dark:text-dark-title">Resources</h5> <a href="/install" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Install OCaml</a> <a href="/docs/get-started" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Get Started</a> <a href="/docs/tools" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Platform Tools</a> <a href="/manual" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Language Manual</a> <a href="/api" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Standard Library API</a> <a href="/books" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Books</a> <a href="/exercises" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Exercises</a> <a href="/papers" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Papers</a> <a href="/play" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">OCaml Playground</a> <a href="/logo" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Logo</a> </div> </section> <div class="flex flex-wrap gap-x-7 md:justify-between items-center py-2 border-solid border-t-[1px] border-separator_20 dark:border-dark-separator_30 bottom-0 bg-background dark:bg-dark-background"> <h5 class="font-bold text-base leading-7 py-2.5 text-title dark:text-dark-title">Policies</h5> <a href="/policies/carbon-footprint" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Carbon Footprint</a> <a href="/governance" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Governance</a> <a href="/policies/privacy-policy" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Privacy</a> <a href="/policies/code-of-conduct" class="font-normal text-content dark:text-dark-content hover:text-primary dark:hover:text-dark-primary leading-7 py-2.5">Code of Conduct</a> </div> <script> document.addEventListener('alpine:init', () => { Alpine.store('themeSettings', { init() { this.storageAccess = localStorage.getItem('storageAccess') this.preference = localStorage.getItem('theme') || "system" this.isSystemDefaultDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches }, selected: '', alertOpen: false, storageAccess: false, preference: undefined, isSystemDefaultDark: false, setPreference(preference) { if (!this.storageAccess) { if (!window.confirm("We will remember your choice in your browser's LocalStorage. Allow this?")) { return; } localStorage.setItem('storageAccess', true) storageAccess = true; } this.selected = preference; this.preference = preference localStorage.setItem('theme', preference) if (preference === 'dark') { document.body.classList.add("dark"); } if (preference === 'light') { document.body.classList.remove("dark"); } if (preference === 'system') { if (this.isSystemDefaultDark) { document.body.classList.add("dark"); } else { document.body.classList.remove("dark"); } localStorage.removeItem('theme') return } }, }) }) </script> </footer> </div> <div class="hidden xl:block top-0 sticky h-screen overflow-auto lg:pt-6"> <div class=""> </div> </div> </div> </div> <script> document.addEventListener('alpine:init', () => { function computeSectionYPositions(toc_el) { function get_y(href) { let heading = document.getElementById(href.substring(1)); return heading.getBoundingClientRect().top + window.scrollY - 60; } let sections = {}; let els = toc_el.querySelectorAll("a"); let l = els.length; for (let i=0; i<l; i++) { let target_href = els[i].getAttribute("href"); let next_el_href = i < l-1 ? els[i+1].getAttribute("href") : null; sections[target_href] = { start: get_y(target_href), end: next_el_href ? get_y(next_el_href) : null, } } return sections; } function isWithin(scrollY, href, sectionYPositions) { if (sectionYPositions == null) return false; return scrollY > sectionYPositions[href].start && (scrollY <= sectionYPositions[href].end || sectionYPositions[href].end == null) } Alpine.data('toc', () => ( { scrollY: window.scrollY, sectionYPositions: null, isWithin, computeSectionYPositions, } )) }) </script> </main> <button class="fixed bottom-8 right-10 md:bottom-[5rem] lg:bottom-[8.37rem] lg:right-[6.5rem] border-0 hidden focus:outline-none z-50 rounded-full shadow-custom p-4 bg-primary dark:bg-dark-primary" onclick="scrollToTop()" id="scrollToTop" title="Scroll to top"><svg class="h-6 w-6" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"> <path d="M4.5 10.5L12 3M12 3L19.5 10.5M12 3V21" stroke="white" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> </svg> </button> <script> function scrollToTop() { window.scrollTo(0, 0); } window.onscroll = function() { showScrollButton(); }; function showScrollButton() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { document.getElementById("scrollToTop").style.display = "block"; } else { document.getElementById("scrollToTop").style.display = "none"; } } </script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10