Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kulikovpavel/1574f9871f020fa3b16294d2225eed11 to your computer and use it in GitHub Desktop.
Save Kulikovpavel/1574f9871f020fa3b16294d2225eed11 to your computer and use it in GitHub Desktop.
goto-school-2017-06.programming_metaphysics.1-alpha
*Programming. The metaphysics way.*, exercises for
==================================================
Class 1-alpha
-------------
We covered lambda calculus, some common operators, defining functions, using `.hs` files and GHCi and just barely scratched data types (GADTs; the `data Bool = False | True` part).
So here are some refreshers and clarifications for that, as well as something to go a bit further on with.
A technical prelude
-------------------
As GitHub can't be bothered to render Literate Haskell (`.lhs`) or even just math in Markdown documents, you'll have to render this document yourself to view formulas in all their curvy mathy glory:
```shell
nix-shell -p pandoc
pandoc -s -r markdown+lhs --mathjax programming_metaphysics.1-alpha.lhs -o /tmp/programming_metaphysics.1-alpha.html
xdg-open /tmp/programming_metaphysics.1-alpha.html
```
This should open you a browser with this rendered.
To set up an environment with Haskell, use
```shell
nix-shell -p ghc
```
If you don't have `nix-shell` in scope, use
```shell
source ~/.nix-profile/etc/profile.d/nix.sh
```
Or, better yet, ask bash to auto-source it on every start:
```shell
echo 'source ~/.nix-profile/etc/profile.d/nix.sh' >> ~/.bash_profile
```
Lambda calculus
---------------
As a reminder, the **identity function**: $\lambda a.a$
It it **alpha equivalent** (the same by virtue of specific variable names being irrelevant) to $\lambda x.x$, but not $\lambda m.n$.
Let's give it an argument: $(\lambda a.a)\ 3$. This **beta reduces** to $3$. The process is also called normalization or evaluation, but here reduction is the canonical term.
Exercises
---------
Reduce:
$(\lambda a.a)(\lambda b.bc)$
$(\lambda j.j)$
$(\lambda g.\lambda o.\lambda t.goto)\ lt\ (\lambda a.a)\ 3$
$(\lambda a.a)(\lambda a.a)(\lambda a.a)(\lambda a.a)(\lambda a.a)$
$(\lambda d.d)(\lambda d.dd)(\lambda d.ddd)(\lambda d.dx)$
Which of the reduced redexes are combinators? Are there any diverging expressions?
Operators
---------
1. What is the priority of `+`? `-`? Check with `:i`.
2. How does `$` work? What is it used for?
Functions
---------
1. What's an **identity function**?
2. Consider:
> f x = x
> g a = a
> h = f g
> h 5
What will happen?
3. Consider:
>f x = x
>f :: t -> t
What does the last line mean?
Producing a binary
------------------
Remember you use `ghc file.hs` to compile, which produces a binary `file`, and that you have to have `main :: IO ()` defined for that.
You can use `putStr` or `putStrLn` of type `String -> IO ()` to print stuff.
1. Make a binary which prints hello world
2. *Mini-project*: Make a binary that prints "GoTo" 100 times
Hint: use either `repeat` and `take` or `replicate` for that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment