Skip to content

Commit 29d33ee

Browse files
committed
Add 'Intro to Haskell' content from haskell branch
1 parent c906c4e commit 29d33ee

14 files changed

+293
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Lunch-N-Learn
22

3-
Here is where we will be putting up the content for our Lunch and Learn Sessions
3+
Directories contain the content for our Lunch and Learn Sessions.
44

5-
Check the branches for the different topics covered in the sessions.
5+
- [Intro to Haskell](/haskell)

haskell/01/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.stack-work/
2+
Week1.cabal
3+
*~

haskell/01/ChangeLog.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Changelog for Week1
2+
3+
## Unreleased changes

haskell/01/LICENSE

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright Author name here (c) 2018
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Author name here nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

haskell/01/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Week1

haskell/01/Setup.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

haskell/01/app/Main.hs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Main where
2+
3+
import Stack
4+
5+
main :: IO ()
6+
main = someFunc

haskell/01/package.yaml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Week1
2+
version: 0.1.0.0
3+
github: "githubuser/Week1"
4+
license: BSD3
5+
author: "Author name here"
6+
maintainer: "[email protected]"
7+
copyright: "2018 Author name here"
8+
9+
extra-source-files:
10+
- README.md
11+
- ChangeLog.md
12+
13+
# Metadata used when publishing your package
14+
# synopsis: Short description of your package
15+
# category: Web
16+
17+
# To avoid duplicated efforts in documentation and dealing with the
18+
# complications of embedding Haddock markup inside cabal files, it is
19+
# common to point users to the README.md file.
20+
description: Please see the README on GitHub at <https://github.com/githubuser/Week1#readme>
21+
22+
dependencies:
23+
- base >= 4.7 && < 5
24+
25+
library:
26+
source-dirs: src
27+
28+
executables:
29+
Week1-exe:
30+
main: Main.hs
31+
source-dirs: app
32+
ghc-options:
33+
- -threaded
34+
- -rtsopts
35+
- -with-rtsopts=-N
36+
dependencies:
37+
- Week1
38+
39+
tests:
40+
Week1-test:
41+
main: Spec.hs
42+
source-dirs: test
43+
ghc-options:
44+
- -threaded
45+
- -rtsopts
46+
- -with-rtsopts=-N
47+
dependencies:
48+
- Week1

haskell/01/src/ADT.hs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module ADT where
2+
3+
data TrafficLight = Green | Amber | Red
4+
5+
instance Show TrafficLight where
6+
show Green = "Green"
7+
show Amber = "Amber"
8+
show Red = "Red"
9+
10+
one_step_down :: TrafficLight -> TrafficLight
11+
one_step_down Green = Amber
12+
one_step_down Amber = Red
13+
one_step_down Red = Red
14+
15+
one_step_up :: TrafficLight -> TrafficLight
16+
one_step_up Green = Green
17+
one_step_up Amber = Green
18+
one_step_up Red = Amber
19+
20+
21+
stop_cars :: TrafficLight -> TrafficLight
22+
stop_cars Green = stop_cars (one_step_down Green)
23+
stop_cars Amber = one_step_down Amber
24+
stop_cars Red = Red

haskell/01/src/AdvancedStack.hs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{-# LANGUAGE GADTs #-}
2+
3+
module AdvancedStack where
4+
5+
import Data.Function ((&))
6+
7+
8+
data Z
9+
data S n
10+
11+
data Stack n a where
12+
Empty :: Stack Z a
13+
Top :: a -> Stack m a -> Stack (S m) a
14+
15+
instance Show a => Show (Stack n a) where
16+
show (Empty) = "Empty]"
17+
show (Top a s) = "[" ++ show a ++ "," ++ show s
18+
19+
instance Functor (Stack n) where
20+
fmap f Empty = Empty
21+
fmap f (Top a s) = (Top $ f a) $ fmap f s
22+
23+
pop :: Stack (S n) a -> (a, Stack n a)
24+
pop (Top a s) = (a , s)
25+
26+
peek :: Stack (S n) a -> (a, Stack (S n) a)
27+
peek t@(Top a s) = (a, t)
28+
29+
push :: a -> Stack n a -> Stack (S n) a
30+
push a s = Top a s
31+
32+
safe_DoublePop :: Num a => Stack (S (S n)) a -> (a, Stack n a)
33+
safe_DoublePop s = (pop s) & snd & pop
34+
35+
safe_do_stuff :: Integer
36+
safe_do_stuff = fst $ safe_DoublePop start
37+
where
38+
start = push 2 Empty

haskell/01/src/Stack.hs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Stack_bak where
2+
3+
data Stack a = Empty | Cons a (Stack a)
4+
5+
instance Show a => Show (Stack a) where
6+
show (Empty) = "Empty]"
7+
show (Cons a s) = "[" ++ show a ++ "," ++ show s
8+
9+
10+
instance Functor Stack where
11+
fmap f (Cons a s) = Cons (f a) (fmap f s)
12+
fmap _ (Empty) = Empty
13+
14+
someFunc :: IO ()
15+
someFunc = putStrLn "someFunc"
16+
17+
push :: a -> Stack a -> Stack a
18+
push x (Cons y s) = Cons x (Cons y s)
19+
push x Empty = Cons x (Empty)
20+
21+
pop :: Stack a -> (a, Stack a)
22+
pop (Cons y s) = (y,s)
23+
pop Empty = error "POPPING EMPTY STACK"
24+
25+
chainContext :: (a, Stack a) -> (Stack a -> (a, Stack a)) -> (a,Stack a)
26+
chainContext (_,s) f = f s
27+
28+
peek :: Stack a -> (a,Stack a)
29+
peek stack@(Cons y _) = (y ,stack)
30+
peek Empty = error "Peeking Empty list"
31+
32+
doublePop :: Num a => Stack a -> (a, Stack a)
33+
doublePop s = (pop s) `chainContext` pop
34+
35+
do_stuff :: Integer
36+
do_stuff = fst $ doublePop start
37+
where
38+
start = push 2 Empty
39+

haskell/01/stack.yaml

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# This file was automatically generated by 'stack init'
2+
#
3+
# Some commonly used options have been documented as comments in this file.
4+
# For advanced use and comprehensive documentation of the format, please see:
5+
# https://docs.haskellstack.org/en/stable/yaml_configuration/
6+
7+
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
8+
# A snapshot resolver dictates the compiler version and the set of packages
9+
# to be used for project dependencies. For example:
10+
#
11+
# resolver: lts-3.5
12+
# resolver: nightly-2015-09-21
13+
# resolver: ghc-7.10.2
14+
# resolver: ghcjs-0.1.0_ghc-7.10.2
15+
#
16+
# The location of a snapshot can be provided as a file or url. Stack assumes
17+
# a snapshot provided as a file might change, whereas a url resource does not.
18+
#
19+
# resolver: ./custom-snapshot.yaml
20+
# resolver: https://example.com/snapshots/2018-01-01.yaml
21+
resolver: lts-12.10
22+
23+
# User packages to be built.
24+
# Various formats can be used as shown in the example below.
25+
#
26+
# packages:
27+
# - some-directory
28+
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
29+
# - location:
30+
# git: https://github.com/commercialhaskell/stack.git
31+
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
32+
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
33+
# subdirs:
34+
# - auto-update
35+
# - wai
36+
packages:
37+
- .
38+
# Dependency packages to be pulled from upstream that are not in the resolver
39+
# using the same syntax as the packages field.
40+
# (e.g., acme-missiles-0.3)
41+
# extra-deps: []
42+
43+
# Override default flag values for local packages and extra-deps
44+
# flags: {}
45+
46+
# Extra package databases containing global packages
47+
# extra-package-dbs: []
48+
49+
# Control whether we use the GHC we find on the path
50+
# system-ghc: true
51+
#
52+
# Require a specific version of stack, using version ranges
53+
# require-stack-version: -any # Default
54+
# require-stack-version: ">=1.7"
55+
#
56+
# Override the architecture used by stack, especially useful on Windows
57+
# arch: i386
58+
# arch: x86_64
59+
#
60+
# Extra directories used by stack for building
61+
# extra-include-dirs: [/path/to/dir]
62+
# extra-lib-dirs: [/path/to/dir]
63+
#
64+
# Allow a newer minor version of GHC than the snapshot specifies
65+
# compiler-check: newer-minor

haskell/01/test/Spec.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main :: IO ()
2+
main = putStrLn "Test suite not yet implemented"

haskell/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Lunch-N-Learn: Haskell
2+
3+
4+
## Installing Haskell
5+
6+
```brew install haskell-stack```
7+
8+
This will include Stack, the build tool we will be using to ensure we don't end up in dependency hell thanks to Cabal
9+
10+
## Installing Haskero for VS Code
11+
12+
Haskell development (especially at the start) can be tedious if you need to run ghc everytime to see if you types compose. Running Haskero with VS Code allows us to do type checking within our editor
13+
14+
- Install vscode (https://code.visualstudio.com/)
15+
- Install 'code' in your $PATH to make it easier to open VSCode (https://code.visualstudio.com/docs/setup/mac)
16+
- Search for and install **Haskero** from the list of extensions in VSCode
17+
- Run ```stack build intero``` within a stack project
18+
- Re-launch VSCode using ```code .```
19+
20+
## Running the week's code
21+
22+
Each week's project code will be its own self-contained Stack project
23+
24+
``` cd <project-file> ```
25+
26+
``` stack build ``` --> Builds the executables
27+
28+
```stack ghci``` --> To access the GHC REPL
29+
30+

0 commit comments

Comments
 (0)