Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernized Haskell training sessions #24

Merged
merged 7 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ the team.

### Slides

The slides use LaTeX. You'll need `xelatex` installed to build them. Simply
run `make` to create the PDFs. You must also have the [Yanone
The slides use LaTeX. Simply run `make` to create the PDFs, assuming you have
all the required dependencies.

You need `xelatex` and the [Yanone
Kaffeesatz](https://yanone.de/fonts/kaffeesatz/) fonts installed on your
machine.

On an Ubuntu system, you can get all of these by running

```
sudo apt-get install texlive-xetex texlive-fonts-extra fonts-yanone-kaffeesatz
```

### Exercises

The codelabs only require ghc to be installed and in the path. Please read
Expand Down
45 changes: 45 additions & 0 deletions haskell_101/codelab/00_setup/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

.DEFAULT_GOAL := run
.PHONY: repl run clean

TARGET = codelab

TOOL = .tool
REPL = .repl_tool
BUILD_GENERATED = dist dist-newstyle .stack-work stack.yaml.lock $(TOOL) $(REPL)

repl: $(REPL)
@`cat $(REPL)` :$(TARGET)

run: $(TOOL)
@`cat $(TOOL)` run $(TARGET)

clean:
@$(RM) -r $(BUILD_GENERATED)

#
# Private rules to check which toolchain is available
#

$(TOOL):
@([[ -e `which stack` ]] && echo stack > $(TOOL) || true)
@([[ -s $(TOOL) ]] || ([[ -e `which cabal` ]] && echo cabal > $(TOOL)) || true)
@([[ -s $(TOOL) ]] || (cat setup.md && false))

$(REPL):
@([[ -e `which stack` ]] && echo stack > $(TOOL) || true)
@([[ -s $(REPL) ]] || ([[ -e `which cabal` ]] && echo cabal repl > $(REPL)) || true)
@([[ -s $(REPL) ]] || (cat setup.md && false))
16 changes: 16 additions & 0 deletions haskell_101/codelab/00_setup/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Copyright 2021 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

import Distribution.Simple
main = defaultMain
32 changes: 32 additions & 0 deletions haskell_101/codelab/00_setup/codelab.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Copyright 2021 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

name: codelab
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10

executable codelab
hs-source-dirs: src
default-language: Haskell2010
main-is: Main.hs
build-depends:
base >=4.11 && <4.15

executable solution
hs-source-dirs: src
default-language: Haskell2010
main-is: Main.hs
build-depends:
base >=4.11 && <4.15
16 changes: 16 additions & 0 deletions haskell_101/codelab/00_setup/setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
============================================

HASKELL 101

============================================

If you are reading this text, probably you don't have Haskell environment setup
on your system. This should give you enough instructions to install it.

First, note that there are two different ways to get Haskell installed:

1. using [Stack](https://docs.haskellstack.org/en/stable/README/)
2. getting the [Haskell Platform](https://www.haskell.org/platform/)

This course is not taking any side, you are free to install either of these. The
links above point to the install pages for either of the toolchains.
23 changes: 23 additions & 0 deletions haskell_101/codelab/00_setup/src/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Copyright 2021 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

module Main where

main :: IO ()
main = do
putStrLn "============================================"
putStrLn ""
putStrLn "Haskell toolchain is installed! All is good."
putStrLn ""
putStrLn "============================================"
17 changes: 17 additions & 0 deletions haskell_101/codelab/00_setup/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

resolver: lts-14.25
ilya-bobyr marked this conversation as resolved.
Show resolved Hide resolved
packages:
- .
45 changes: 45 additions & 0 deletions haskell_101/codelab/01_functions/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

.DEFAULT_GOAL := run
.PHONY: repl run clean

TARGET = codelab

TOOL = .tool
REPL = .repl_tool
BUILD_GENERATED = dist dist-newstyle .stack-work stack.yaml.lock $(TOOL) $(REPL)

repl: $(REPL)
@`cat $(REPL)` :$(TARGET)

run: $(TOOL)
@`cat $(TOOL)` run $(TARGET)

clean:
@$(RM) -r $(BUILD_GENERATED)

#
# Private rules to check which toolchain is available
#

$(TOOL):
@([[ -e `which stack` ]] && echo stack > $(TOOL) || true)
@([[ -s $(TOOL) ]] || ([[ -e `which cabal` ]] && echo cabal > $(TOOL)) || true)
@([[ -s $(TOOL) ]] || (cat setup.md && false))

$(REPL):
@([[ -e `which stack` ]] && echo stack > $(TOOL) || true)
@([[ -s $(REPL) ]] || ([[ -e `which cabal` ]] && echo cabal repl > $(REPL)) || true)
@([[ -s $(REPL) ]] || (cat setup.md && false))
16 changes: 16 additions & 0 deletions haskell_101/codelab/01_functions/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Copyright 2021 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

import Distribution.Simple
main = defaultMain
41 changes: 41 additions & 0 deletions haskell_101/codelab/01_functions/codelab.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- Copyright 2021 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

name: codelab
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10

executable codelab
hs-source-dirs: src
default-language: Haskell2010
main-is: Main.hs
other-modules:
Codelab
, Internal
build-depends:
base >=4.11 && <4.15
, ansi-terminal >= 0.8.0.4

executable solution
hs-source-dirs: src
default-language: Haskell2010
main-is: Main.hs
cpp-options: -DSOLUTION
other-modules:
Solution
, Internal
build-depends:
base >=4.11 && <4.15
, ansi-terminal >= 0.8.0.4
16 changes: 16 additions & 0 deletions haskell_101/codelab/01_functions/setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
============================================

HASKELL MTV 101
ilya-bobyr marked this conversation as resolved.
Show resolved Hide resolved

============================================

If you are reading this text, probably you don't have Haskell environment setup
on your system. This should give you enough instructions to install it.

First, note that there are two different ways to get Haskell installed:

1. using [Stack](https://docs.haskellstack.org/en/stable/README/)
2. getting the [Haskell Platform](https://www.haskell.org/platform/)

This course is not taking any side, you are free to install either of these. The
links above point to the install pages for either of the toolchains.
ilya-bobyr marked this conversation as resolved.
Show resolved Hide resolved
65 changes: 65 additions & 0 deletions haskell_101/codelab/01_functions/src/Codelab.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- Copyright 2021 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}

module Codelab where

import Internal (codelab)
import Prelude hiding (gcd)

-- CODELAB 01: Number manipulation
--
-- As we have not looked at any complex data structures yet, the only thing
-- we have for now is numbers.

add :: Int -> Int -> Int
add x y = codelab

subtract :: Int -> Int -> Int
subtract x y = codelab

double :: Int -> Int
double x = codelab

multiply :: Int -> Int -> Int
multiply x y = codelab

-- Note that Haskell is strict about types even for basic integral types.
-- Int is never automatically converted to Double. But you can use
-- fromIntegral to convert from any integral type to any number type.
divide :: Int -> Int -> Double
divide x y = codelab

-- Remember that you can use if/then/else:
--
-- if <expr> then <expr> else <expr>
--
-- Integer is just like Int, except that it can store arbitrarily large
-- numbers. Without a type like this, we couldn't compute the factorial of
-- even relatively small numbers.
--
-- Remember that function application binds tighter than operators!
-- E.g.: `3 * f 4` is the same as `3 * (f 4)`, not `(3 * f) 4`.
factorial :: Integer -> Integer
factorial n = codelab

-- Consider Euclid's algorithm:
--
-- https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm
gcd :: Int -> Int -> Int
gcd a b = codelab
Loading