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

Week 10 Ex 1-2 #139

Merged
merged 50 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
f125a32
Week2 wip
Aug 15, 2019
3a91c6f
week2 - exercie1 using view patterns + exercise 2,3,4,5
Aug 18, 2019
3f35e4d
Merge remote-tracking branch 'fp-works/master'
Aug 20, 2019
745a3dc
Merge remote-tracking branch 'origin/master'
Aug 20, 2019
0c4f9e5
Week 4 ex1
Aug 30, 2019
8433f2c
Week3 Ex 3
Aug 31, 2019
b5d7467
Merge branch 'master' of github.com:tienwei/2019-winter-Haskell-school
Aug 31, 2019
296664e
Week5 Ex 1, 2, 3, 4, 5
Aug 30, 2019
24503e6
Refine code and add ex3 test
Sep 9, 2019
26f1c61
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
Sep 9, 2019
8c14438
Merge remote-tracking branch 'origin/master'
Sep 11, 2019
be98e9c
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Sep 11, 2019
efc50d7
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
Sep 13, 2019
0430223
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Sep 14, 2019
cdd7c35
Week 6 ex 1 - 4
Sep 18, 2019
7629f44
Merge branch 'master' of github.com:tienwei/2019-winter-Haskell-school
tienwei Sep 20, 2019
f9ca0f1
Week 6 Ex 5
tienwei Sep 22, 2019
2456321
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Sep 22, 2019
b20a442
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Sep 24, 2019
19785fe
Week 7 Ex 1
tienwei Sep 24, 2019
5af7648
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Sep 25, 2019
1de9a1d
Week 7 Ex 2.1
tienwei Sep 26, 2019
7546069
formatted
tienwei Sep 26, 2019
23fe4e6
Week7 Ex2.2
tienwei Sep 27, 2019
6771db7
ex 2.3 plus tests
tienwei Sep 27, 2019
45f4f2d
Week 7 Ex 3
tienwei Sep 27, 2019
930b87c
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Sep 27, 2019
774aba5
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Sep 29, 2019
1138dfc
Week 7 Ex 4
tienwei Sep 29, 2019
3a39599
Week 8 Ex 1
tienwei Sep 30, 2019
9982de8
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Sep 30, 2019
4f41a36
Fix week 7 ex 4 feedback
tienwei Sep 30, 2019
6bab814
Fix PR feedback for week8 ex1
tienwei Oct 1, 2019
a91e016
Week 8 Ex 2
tienwei Oct 2, 2019
d06339c
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Oct 2, 2019
48b13cd
Fix week 8 ex 2 feedback
tienwei Oct 2, 2019
c61c2c0
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
Oct 3, 2019
7697556
Merge branch 'master' of github.com:tienwei/2019-winter-Haskell-school
tienwei Oct 3, 2019
2842c37
Week 8 Ex 3-4
tienwei Oct 3, 2019
9f6e481
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Oct 3, 2019
365140a
Week8 Ex3(fixed)-5
tienwei Oct 4, 2019
29c7ca1
Week8 Fix nextLevel
tienwei Oct 6, 2019
a2a8f91
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Oct 6, 2019
1770951
Week8 update test and remove redundant code
tienwei Oct 7, 2019
217c464
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Oct 8, 2019
3589d8b
Week8 update based on the nit
tienwei Oct 9, 2019
8c8415f
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Oct 9, 2019
d21ff7a
Week 10 Ex 1
tienwei Oct 11, 2019
a4e66b5
Merge branch 'master' of https://github.com/fp-works/2019-winter-Hask…
tienwei Oct 11, 2019
18351f6
Week 10 Ex 2
tienwei Oct 14, 2019
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
3 changes: 3 additions & 0 deletions cis194/week10/tien/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.stack-work/
tien.cabal
*~
3 changes: 3 additions & 0 deletions cis194/week10/tien/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changelog for tien

## Unreleased changes
30 changes: 30 additions & 0 deletions cis194/week10/tien/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright Author name here (c) 2019

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Author name here nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1 change: 1 addition & 0 deletions cis194/week10/tien/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# tien
2 changes: 2 additions & 0 deletions cis194/week10/tien/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
4 changes: 4 additions & 0 deletions cis194/week10/tien/app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Main where

main :: IO ()
main = putStr "No Use"
49 changes: 49 additions & 0 deletions cis194/week10/tien/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: tien
version: 0.1.0.0
github: "githubuser/tien"
license: BSD3
author: "Tien"
maintainer: "tienlindev@gmail.com"
copyright: "2019 Tien"

extra-source-files:
- README.md
- ChangeLog.md

# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/tien#readme>

dependencies:
- base >= 4.7 && < 5
- hspec

library:
source-dirs: src

executables:
tien-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- tien

tests:
tien-test:
main: AParserSpec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- tien
75 changes: 75 additions & 0 deletions cis194/week10/tien/src/AParser.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{-# OPTIONS_GHC -Wall #-}

{- CIS 194 HW 10
-}
module AParser where

import Control.Applicative

import Data.Char

-- A parser for a value of type a is a function which takes a String
-- represnting the input to be parsed, and succeeds or fails; if it
-- succeeds, it returns the parsed value along with the remainder of
-- the input.
newtype Parser a =
Parser
{ runParser :: String -> Maybe (a, String)
}

-- For example, 'satisfy' takes a predicate on Char, and constructs a
-- parser which succeeds only if it sees a Char that satisfies the
-- predicate (which it then returns). If it encounters a Char that
-- does not satisfy the predicate (or an empty input), it fails.
satisfy :: (Char -> Bool) -> Parser Char
satisfy p = Parser f
where
f [] = Nothing -- fail on the empty input
f (x:xs) -- check if x satisfies the predicate
-- if so, return x along with the remainder
-- of the input (that is, xs)
| p x = Just (x, xs)
| otherwise = Nothing -- otherwise, fail

-- Using satisfy, we can define the parser 'char c' which expects to
-- see exactly the character c, and fails otherwise.
char :: Char -> Parser Char
char c = satisfy (== c)

{- For example:

*Parser> runParser (satisfy isUpper) "ABC"
Just ('A',"BC")
*Parser> runParser (satisfy isUpper) "abc"
Nothing
*Parser> runParser (char 'x') "xyz"
Just ('x',"yz")

-}
-- For convenience, we've also provided a parser for positive
-- integers.
posInt :: Parser Integer
posInt = Parser f
where
f xs
| null ns = Nothing
| otherwise = Just (read ns, rest)
where
(ns, rest) = span isDigit xs

------------------------------------------------------------
-- Your code goes below here
------------------------------------------------------------
-- exercise 1 --
first :: (a -> b) -> (a, c) -> (b, c)
first f (x, y) = (f x, y)

instance Functor Parser where
fmap f (Parser a) = Parser $ fmap (first f) . a

-- exercise 2 --
instance Applicative Parser where
pure a = Parser (\x -> Just (a, x))
Parser f <*> (Parser a) = Parser b
where
b xs = pure first <*> fmap fst (f xs) <*> (a xs)
66 changes: 66 additions & 0 deletions cis194/week10/tien/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/

# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-14.7

# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# subdirs:
# - auto-update
# - wai
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
# extra-deps: []

# Override default flag values for local packages and extra-deps
# flags: {}

# Extra package databases containing global packages
# extra-package-dbs: []

# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=2.1"
#
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
12 changes: 12 additions & 0 deletions cis194/week10/tien/stack.yaml.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files

packages: []
snapshots:
- completed:
size: 523700
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/14/7.yaml
sha256: 8e3f3c894be74d71fa4bf085e0a8baae7e4d7622d07ea31a52736b80f8b9bb1a
original: lts-14.7
16 changes: 16 additions & 0 deletions cis194/week10/tien/test/AParserSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import AParser
import Data.Char
import Test.Hspec

main :: IO ()
main =
hspec $ do
describe "AParser" $ do
let f = toInteger . digitToInt
let newParserInt = fmap f (char '2')
let testStr1 = "xyz234"
let testStr2 = "2xyz"
describe "exercise 1" $ do
it "should fmap Parser Char to Parser Integer" $ do
runParser newParserInt testStr1 `shouldBe` (runParser posInt testStr1)
runParser newParserInt testStr2 `shouldBe` (runParser posInt testStr2)