Skip to content
LAM
Read Home Blog
Make Projects HTML Tools Games
Touch grass Notes Resume Links
Home Blog HTML Projects
Tools Games Notes Resume Links
Back CSE 240: Scheme Mastery Cheat Sheet Programming
Download Open
Show description 2,206 chars · Programming

CSE 240: Scheme Mastery Cheat Sheet

CSE 240: Scheme Mastery Cheat Sheet







CSE 240: Scheme Mastery




Q1: sumEven - The Recursive Challenge


1.1: Named Procedure

A standard recursive function. The base case checks for an empty list, and the recursive step branches based on whether the current element is even.

(define (sumEven lst)
; Base Case: If the list is empty, the sum is 0.
(if (null? lst)
0
; Recursive Step:
(if (even? (car lst))
; If head is even, add it to the sum of the rest.
(+ (car lst) (sumEven (cdr lst)))
; If head is odd, skip it and sum the rest.
(sumEven (cdr lst)))))

1.2: Unnamed Procedure (IILE)


Key Concept: Recursion Without a Name

The prompt asks for an unnamed lambda that calls itself. How? The solution is to use letrec to create a locally-scoped, recursive helper function. This keeps the main lambda anonymous while still allowing recursion.



; Immediately invoke an unnamed lambda that takes the list.
((lambda (lst)
; 'letrec' creates a local helper that can call itself.
(letrec (
(sum-even-helper
(lambda (sub-list)
(if (null? sub-list)
0
(if (even? (car sub-list))
(+ (car sub-list) (sum-even-helper (cdr sub-list)))
(sum-even-helper (cdr sub-list)))))))
; The body of the main lambda just starts the recursion.
(sum-even-helper lst)))
'(1 3 -4 5 6 -7))



Q2 & Q3: The `append` Power-Up

These are best solved using the built-in append procedure. It's the most direct and efficient tool for the job.

; Q2: Add to the back
(define (list-push-back lst new-list)
(append lst new-list))

; Q3: Add to the front
(define (list-push-front lst new-list)
(append new-list lst))



Q4 & Q5: Drawing from Lists

Q4: list-draw-front

A classic recursive pattern: build a new list by taking the `car` and `cons`-ing it onto the recursive result of the `cdr`.

(define (list-draw-front n lst)
; Base Case: Stop if n is 0 or the list is empty.
(if (or (= n 0) (null? lst))
'()
; Recursive Step: Take the first element and recurse.
(cons (car lst) (list-draw-front (- n 1) (cdr lst)))))

Q5: list-draw-back (The clever way)

A great example of functional composition. Instead of writing complex recursion, reuse existing functions.…

CSE 240: Scheme Mastery Cheat Sheet

13,915 bytes · HTML source
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSE 240: Scheme Mastery Cheat Sheet</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet">
    <style>
        :root {
            --black: #121212;
            --dark-grey: #1e1e1e;
            --light-grey: #f0f0f0;
            --orange: #ff9900;
            --red: #ff3333;
            --green: #aaffaa;
            --comment: #888888;
        }

        body {
            background-color: var(--black);
            color: var(--light-grey);
            font-family: 'Courier New', Courier, monospace;
            margin: 0;
            padding: 2em;
        }

        .container {
            max-width: 900px;
            margin: auto;
            border: 3px solid var(--orange);
            padding: 2em;
            box-shadow: 0 0 15px var(--orange);
        }

        h1, h2, h3 {
            font-family: 'Press Start 2P', cursive;
            color: var(--orange);
            text-shadow: 3px 3px 0 var(--red);
        }

        h1 {
            font-size: 2em;
            text-align: center;
        }
        
        h2 {
            border-bottom: 2px solid var(--orange);
            padding-bottom: 10px;
            margin-top: 2em;
        }

        hr {
            border: 1px solid var(--red);
            margin: 2em 0;
        }

        pre, code {
            font-family: 'Courier New', Courier, monospace;
            font-size: 1.1em;
            line-height: 1.5;
        }

        pre {
            background-color: var(--dark-grey);
            border: 2px solid var(--red);
            padding: 1em;
            border-radius: 5px;
            white-space: pre-wrap;
            word-wrap: break-word;
            box-shadow: inset 0 0 10px #000;
        }

        .concept-box {
            background-color: #2a2218;
            border: 2px dashed var(--orange);
            padding: 1em;
            margin-top: 1em;
            border-radius: 5px;
        }
        .concept-box h3 {
            margin-top: 0;
        }

        /* Manual Syntax Highlighting */
        .scheme-paren { color: var(--light-grey); }
        .scheme-keyword { color: var(--red); font-weight: bold; }
        .scheme-function { color: var(--orange); }
        .scheme-literal { color: var(--green); }
        .scheme-comment { color: var(--comment); font-style: italic; }

        .cursor {
            display: inline-block;
            background-color: var(--orange);
            width: 1.2rem;
            height: 2.2rem;
            animation: blink 1s step-end infinite;
            vertical-align: bottom;
        }

        @keyframes blink {
            from, to { background-color: transparent; }
            50% { background-color: var(--orange); }
        }

    </style>
</head>
<body>
    <div class="container">
        <h1>CSE 240: Scheme Mastery<span class="cursor"></span></h1>
        
        <hr>

        <h2>Q1: sumEven - The Recursive Challenge</h2>
        
        <h3>1.1: Named Procedure</h3>
        <p>A standard recursive function. The base case checks for an empty list, and the recursive step branches based on whether the current element is even.</p>
        <pre><code class="language-scheme"><span class="scheme-paren">(</span><span class="scheme-keyword">define</span> <span class="scheme-paren">(</span><span class="scheme-function">sumEven</span> lst<span class="scheme-paren">)</span>
  <span class="scheme-comment">; Base Case: If the list is empty, the sum is 0.</span>
  <span class="scheme-paren">(</span><span class="scheme-keyword">if</span> <span class="scheme-paren">(</span><span class="scheme-function">null?</span> lst<span class="scheme-paren">)</span>
      <span class="scheme-literal">0</span>
      <span class="scheme-comment">; Recursive Step:</span>
      <span class="scheme-paren">(</span><span class="scheme-keyword">if</span> <span class="scheme-paren">(</span><span class="scheme-function">even?</span> <span class="scheme-paren">(</span><span class="scheme-function">car</span> lst<span class="scheme-paren">))</span>
          <span class="scheme-comment">; If head is even, add it to the sum of the rest.</span>
          <span class="scheme-paren">(</span><span class="scheme-function">+</span> <span class="scheme-paren">(</span><span class="scheme-function">car</span> lst<span class="scheme-paren">)</span> <span class="scheme-paren">(</span><span class="scheme-function">sumEven</span> <span class="scheme-paren">(</span><span class="scheme-function">cdr</span> lst<span class="scheme-paren">)))</span>
          <span class="scheme-comment">; If head is odd, skip it and sum the rest.</span>
          <span class="scheme-paren">(</span><span class="scheme-function">sumEven</span> <span class="scheme-paren">(</span><span class="scheme-function">cdr</span> lst<span class="scheme-paren">)))))</span></code></pre>

        <h3>1.2: Unnamed Procedure (IILE)</h3>
        <div class="concept-box">
            <h3>Key Concept: Recursion Without a Name</h3>
            <p>The prompt asks for an unnamed lambda that calls itself. How? The solution is to use <strong>letrec</strong> to create a <em>locally-scoped</em>, recursive helper function. This keeps the main lambda anonymous while still allowing recursion.</p>
        </div>
        <pre><code class="language-scheme"><span class="scheme-comment">; Immediately invoke an unnamed lambda that takes the list.</span>
<span class="scheme-paren">((</span><span class="scheme-keyword">lambda</span> <span class="scheme-paren">(</span>lst<span class="scheme-paren">)</span>
   <span class="scheme-comment">; 'letrec' creates a local helper that can call itself.</span>
   <span class="scheme-paren">(</span><span class="scheme-keyword">letrec</span> <span class="scheme-paren">(</span>
        <span class="scheme-paren">(</span><span class="scheme-function">sum-even-helper</span>
         <span class="scheme-paren">(</span><span class="scheme-keyword">lambda</span> <span class="scheme-paren">(</span>sub-list<span class="scheme-paren">)</span>
           <span class="scheme-paren">(</span><span class="scheme-keyword">if</span> <span class="scheme-paren">(</span><span class="scheme-function">null?</span> sub-list<span class="scheme-paren">)</span>
               <span class="scheme-literal">0</span>
               <span class="scheme-paren">(</span><span class="scheme-keyword">if</span> <span class="scheme-paren">(</span><span class="scheme-function">even?</span> <span class="scheme-paren">(</span><span class="scheme-function">car</span> sub-list<span class="scheme-paren">))</span>
                   <span class="scheme-paren">(</span><span class="scheme-function">+</span> <span class="scheme-paren">(</span><span class="scheme-function">car</span> sub-list<span class="scheme-paren">)</span> <span class="scheme-paren">(</span><span class="scheme-function">sum-even-helper</span> <span class="scheme-paren">(</span><span class="scheme-function">cdr</span> sub-list<span class="scheme-paren">)))</span>
                   <span class="scheme-paren">(</span><span class="scheme-function">sum-even-helper</span> <span class="scheme-paren">(</span><span class="scheme-function">cdr</span> sub-list<span class="scheme-paren">)))))))</span>
     <span class="scheme-comment">; The body of the main lambda just starts the recursion.</span>
     <span class="scheme-paren">(</span><span class="scheme-function">sum-even-helper</span> lst<span class="scheme-paren">)))</span>
 <span class="scheme-literal">'(1 3 -4 5 6 -7)</span><span class="scheme-paren">)</span></code></pre>

        <hr>

        <h2>Q2 & Q3: The `append` Power-Up</h2>
        <p>These are best solved using the built-in <code>append</code> procedure. It's the most direct and efficient tool for the job.</p>
        <pre><code class="language-scheme"><span class="scheme-comment">; Q2: Add to the back</span>
<span class="scheme-paren">(</span><span class="scheme-keyword">define</span> <span class="scheme-paren">(</span><span class="scheme-function">list-push-back</span> lst new-list<span class="scheme-paren">)</span>
  <span class="scheme-paren">(</span><span class="scheme-function">append</span> lst new-list<span class="scheme-paren">))</span>

<span class="scheme-comment">; Q3: Add to the front</span>
<span class="scheme-paren">(</span><span class="scheme-keyword">define</span> <span class="scheme-paren">(</span><span class="scheme-function">list-push-front</span> lst new-list<span class="scheme-paren">)</span>
  <span class="scheme-paren">(</span><span class="scheme-function">append</span> new-list lst<span class="scheme-paren">))</span></code></pre>
        
        <hr>

        <h2>Q4 & Q5: Drawing from Lists</h2>
        <h3>Q4: list-draw-front</h3>
        <p>A classic recursive pattern: build a new list by taking the `car` and `cons`-ing it onto the recursive result of the `cdr`.</p>
        <pre><code class="language-scheme"><span class="scheme-paren">(</span><span class="scheme-keyword">define</span> <span class="scheme-paren">(</span><span class="scheme-function">list-draw-front</span> n lst<span class="scheme-paren">)</span>
  <span class="scheme-comment">; Base Case: Stop if n is 0 or the list is empty.</span>
  <span class="scheme-paren">(</span><span class="scheme-keyword">if</span> <span class="scheme-paren">(</span><span class="scheme-function">or</span> <span class="scheme-paren">(</span><span class="scheme-function">=</span> n <span class="scheme-literal">0</span><span class="scheme-paren">)</span> <span class="scheme-paren">(</span><span class="scheme-function">null?</span> lst<span class="scheme-paren">))</span>
      <span class="scheme-literal">'()</span>
      <span class="scheme-comment">; Recursive Step: Take the first element and recurse.</span>
      <span class="scheme-paren">(</span><span class="scheme-function">cons</span> <span class="scheme-paren">(</span><span class="scheme-function">car</span> lst<span class="scheme-paren">)</span> <span class="scheme-paren">(</span><span class="scheme-function">list-draw-front</span> <span class="scheme-paren">(</span><span class="scheme-function">-</span> n <span class="scheme-literal">1</span><span class="scheme-paren">)</span> <span class="scheme-paren">(</span><span class="scheme-function">cdr</span> lst<span class="scheme-paren">)))))</span></code></pre>

        <h3>Q5: list-draw-back (The clever way)</h3>
        <p>A great example of functional composition. Instead of writing complex recursion, reuse existing functions. To get the last N items, just reverse the list, take the *first* N items, and reverse the result back!</p>
        <pre><code class="language-scheme"><span class="scheme-paren">(</span><span class="scheme-keyword">define</span> <span class="scheme-paren">(</span><span class="scheme-function">list-draw-back</span> n lst<span class="scheme-paren">)</span>
  <span class="scheme-paren">(</span><span class="scheme-function">reverse</span> <span class="scheme-paren">(</span><span class="scheme-function">list-draw-front</span> n <span class="scheme-paren">(</span><span class="scheme-function">reverse</span> lst<span class="scheme-paren">))))</span></code></pre>
        
        <hr>
        
        <h2>Q6: Perfect Shuffle</h2>
        <p>This recursion masterfully weaves two lists together. It takes one element from each list and prepends them to the result of shuffling the rest of the lists.</p>
        <pre><code class="language-scheme"><span class="scheme-comment">; The list-shuffle procedure works by recursively building the shuffled list.
; If the first list is empty, both lists are empty, so we return an empty
; list to end the recursion. The algorithm takes the first element from lst1
; and the first element from lst2, prepends them to the result of shuffling
; the rest of the lists, and builds the final list in an alternating order.</span>
<span class="scheme-paren">(</span><span class="scheme-keyword">define</span> <span class="scheme-paren">(</span><span class="scheme-function">list-shuffle</span> lst1 lst2<span class="scheme-paren">)</span>
  <span class="scheme-comment">; Base Case: If one list is empty, we're done.</span>
  <span class="scheme-paren">(</span><span class="scheme-keyword">if</span> <span class="scheme-paren">(</span><span class="scheme-function">null?</span> lst1<span class="scheme-paren">)</span>
      <span class="scheme-literal">'()</span>
      <span class="scheme-comment">; Recursive Step: Prepend head of lst1, then head of lst2...</span>
      <span class="scheme-paren">(</span><span class="scheme-function">cons</span> <span class="scheme-paren">(</span><span class="scheme-function">car</span> lst1<span class="scheme-paren">)</span>
            <span class="scheme-comment">; ...to the result of shuffling the rest of the lists.</span>
            <span class="scheme-paren">(</span><span class="scheme-function">cons</span> <span class="scheme-paren">(</span><span class="scheme-function">car</span> lst2<span class="scheme-paren">)</span>
                  <span class="scheme-paren">(</span><span class="scheme-function">list-shuffle</span> <span class="scheme-paren">(</span><span class="scheme-function">cdr</span> lst1<span class="scheme-paren">)</span> <span class="scheme-paren">(</span><span class="scheme-function">cdr</span> lst2<span class="scheme-paren">))))))</span></code></pre>

    </div>
</body>
</html>