Skip to content

Commit

Permalink
Lecture 02 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmheim authored Feb 20, 2024
1 parent 6ed1eff commit 38ddfd3
Show file tree
Hide file tree
Showing 8 changed files with 728 additions and 23 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
name: Deploy VitePress site to Pages

on:
# Runs on pushes targeting the `main` branch. Change this to `master` if you're
# using the `master` branch as the default branch.
push:
branches: [main]

pull_request:
branches:
- main

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

Expand Down Expand Up @@ -54,6 +56,7 @@ jobs:

# Deployment job
deploy:
if: github.event.pull_request.base.ref == 'refs/heads/main'
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
Expand Down
3 changes: 2 additions & 1 deletion .vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export default defineConfig({
text: 'Lectures',
link: '/lectures/',
items: [
{ text: '01: Introduction', link: '/lectures/lecture01'}
{ text: '01: Introduction', link: '/lectures/lecture01'},
{ text: '02: Lists & Trees', link: '/lectures/lecture02'},
]
},

Expand Down
1 change: 1 addition & 0 deletions img/tree.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 12 additions & 14 deletions labs/lab01.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ outline: deep

# Lab 1: Introduction to Racket

This lab aims to familiarize the students with the IDE we will use for Racket and help them write
simple programs.
In this lab you will familiarize yourself with the IDE we will use to write Racket programs.

## Dr. Racket IDE

The IDE can be downloaded for free for Linux, Windows, and MAC from:
https://racket-lang.org/
The IDE can be downloaded for free for Linux, Windows, and MAC from: https://racket-lang.org/

The students can use the one installed in the lab computers. The teacher may help the students (to a
reasonable degree) to get the IDE running on students’ laptops.
You can use the one installed in the lab computers or install it on your own laptop. Don't hesitate
to ask for help!

Get familiar with the definition window and REPL in DrRacket. The documentation of implemented
functions is accessible via Help Desk in the menu.
To get familiar with the definition window and REPL in DrRacket you can:
* Read documentation of implemented functions which is accessible via **Help Desk** in the menu.

DrRacket allows writing programs in several languages. We focus on Racket, so your first line in the
code should be:
Expand All @@ -25,7 +23,7 @@ code should be:
```

## Exercises - Racket basics
Start interaction in REPL. Racket uses prefix notation for all functions. Let students compute
Start interaction in REPL. Racket uses prefix notation for all functions. Try and compute a
simple formulas, e.g., $2+3/5$.

### Exercise 1
Expand Down Expand Up @@ -80,7 +78,7 @@ consisting of a sequence of consecutive characters starting with `fst`, ending w
following the order in the ASCII table.
For example `(consecutive-chars #\A #\D) => "ABCD"` or `(consecutive-chars #\z #\u) => "zyxwvu"`.
For converting characters into positions in the ASCII table, use functions
`<nowiki>char->integer</nowiki>` and `<nowiki>integer->char</nowiki>`. To convert a character into a
`char->integer` and `integer->char`. To convert a character into a
string, apply the function `string`.

::: details Solution
Expand All @@ -98,9 +96,11 @@ string, apply the function `string`.
(iter (+ k step)
(string-append acc (integer->string k)))))
(iter first-index ""))
```

; alternatively:
#|
Alternatively, and maybe slightly more elegantly, you can define two helper functions `char+1` and
`char-1` and use an accumulator:
```scheme
(define (char+1 c) (integer->char (add1 (char->integer c))))
(define (char-1 c) (integer->char (sub1 (char->integer c))))
Expand All @@ -111,8 +111,6 @@ string, apply the function `string`.
(consecutive-chars (char+1 fst) lst (string-append acc (string fst)))]
[(char>? fst lst)
(consecutive-chars (char-1 fst) lst (string-append acc (string fst)))]))
|#
```
:::

Expand Down
9 changes: 6 additions & 3 deletions lectures/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ due to other obligations.
## [Lecture 01](lecture01): Introduction
Discusses the key ideas behind functional programming. It further
introduces the programming language [Racket](https://racket-lang.org/).

[Slides](https://drive.google.com/file/d/1e1aw5Mf5qts0DxLJFDZUWDbGlwI0hdeM/view?usp=drive_link).
[Log](https://drive.google.com/file/d/1I3uoTART5UmWsWLRR_bYuNrzolTl2cO5/view?usp=share_link).

## [Lecture 02](lecture02): Lists & Trees
Focuses on Racket lists and trees. Further, it introduces the unit testing library
[Rackunit](https://docs.racket-lang.org/rackunit/index.html).

<!--
2. [Lecture 2](lecture02) focuses on Racket lists and trees. Further, it introduces the unit testing
library [Rackunit](https://docs.racket-lang.org/rackunit/index.html).
[Slides](https://drive.google.com/file/d/1PalOH5j2BvNzBtmAVe4v9dZosf_B-jKQ/view?usp=drive_link).

<!--
3. [Lecture 3](lecture03) deals with higher-order functions, function closures and Racket structures.
4. [Lecture 4](lecture04) introduces pattern matching, and explains how to implement lazy
Expand Down
11 changes: 9 additions & 2 deletions lectures/lecture01.md
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,9 @@ grows exponentially in the recursion depth. So one should be careful when design
function. Still, tree-recursive functions are natural and powerful when dealing with tree-structured
data like algebraic expressions or tree-generative processes like fractals.


#### Drawing trees

As an example of the tree recursion, consider a tree-like fractal of a given size $n$ and direction
$d$ in degrees generated according to the following rules:

Expand All @@ -778,9 +781,13 @@ recursive calls.

![Recursive generation of a tree-like fractal](../img/fractal.svg){ style="width: 100%; margin: auto;" }

Now, let us see how to program this procedure in Racket. First, we need a drawing library if we want to draw something. Racket contains a simple library called
Now, let us see how to program this procedure in Racket. First, we need a drawing library if we want
to draw something. Racket contains a simple library called
[value-turtles](https://docs.racket-lang.org/turtles/Value_Turtles.html).
Functions in this library update an image containing "a turtle" with a position and direction. For example, we can draw a line of a given length in the turtle's direction or turn the turtle by an angle. Further, we can extract the turtle's state from a given image and restore the state later on. The code of the program is below.
Functions in this library update an image containing "a turtle" with a position and direction. For
example, we can draw a line of a given length in the turtle's direction or turn the turtle by an
angle. Further, we can extract the turtle's state from a given image and restore the state later on.
The code of the program is below.

```scheme:line-numbers
#lang racket
Expand Down
Loading

0 comments on commit 38ddfd3

Please sign in to comment.