Skip to content

Commit

Permalink
Finished Book 1, Now Book of Monads
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasios Valtinos committed Oct 27, 2022
1 parent 5e00bab commit 7d76fe3
Show file tree
Hide file tree
Showing 56 changed files with 635 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module WithCounting where

data WithCounter a = Int -> (a, Int) deriving (Eq, Show)
7 changes: 4 additions & 3 deletions Haskell Excercises & Code/Chapter14 - Testing/Trivial.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Trivial where

import Test.QuickCheck
Expand Down Expand Up @@ -29,6 +30,6 @@ data Pair a b = Pair a b deriving (Eq, Show)

pairGen :: (Arbitrary a, Arbitrary b) => Gen (Pair a b)
pairGen = do
a <- arbitrary
b <- arbitrary
return (Pair a b)
a :: Int <- arbitrary
b :: Int <- arbitrary
return (Pair a b :: Pair Int Int)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ library
exposed-modules: Addition
ghc-options: -Wall -fwarn-tabs
build-depends: base >=4.7 && <5
, hspec
, hspec, QuickCheck
hs-source-dirs: .
default-language: Haskell2010
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE NoRebindableSyntax #-}
{-# OPTIONS_GHC -fno-warn-missing-import-lists #-}
{-# OPTIONS_GHC -w #-}
module Paths_addition (
version,
getBinDir, getLibDir, getDynLibDir, getDataDir, getLibexecDir,
getDataFileName, getSysconfDir
) where


import qualified Control.Exception as Exception
import qualified Data.List as List
import Data.Version (Version(..))
import System.Environment (getEnv)
import Prelude


#if defined(VERSION_base)

#if MIN_VERSION_base(4,0,0)
catchIO :: IO a -> (Exception.IOException -> IO a) -> IO a
#else
catchIO :: IO a -> (Exception.Exception -> IO a) -> IO a
#endif

#else
catchIO :: IO a -> (Exception.IOException -> IO a) -> IO a
#endif
catchIO = Exception.catch

version :: Version
version = Version [0,1,0,0] []

getDataFileName :: FilePath -> IO FilePath
getDataFileName name = do
dir <- getDataDir
return (dir `joinFileName` name)

getBinDir, getLibDir, getDynLibDir, getDataDir, getLibexecDir, getSysconfDir :: IO FilePath



bindir, libdir, dynlibdir, datadir, libexecdir, sysconfdir :: FilePath
bindir = "C:\\Users\\Administrator\\AppData\\Roaming\\cabal\\bin"
libdir = "C:\\Users\\Administrator\\AppData\\Roaming\\cabal\\x86_64-windows-ghc-9.2.1\\addition-0.1.0.0-inplace"
dynlibdir = "C:\\Users\\Administrator\\AppData\\Roaming\\cabal\\x86_64-windows-ghc-9.2.1"
datadir = "C:\\Users\\Administrator\\AppData\\Roaming\\cabal\\x86_64-windows-ghc-9.2.1\\addition-0.1.0.0"
libexecdir = "C:\\Users\\Administrator\\AppData\\Roaming\\cabal\\addition-0.1.0.0-inplace\\x86_64-windows-ghc-9.2.1\\addition-0.1.0.0"
sysconfdir = "C:\\Users\\Administrator\\AppData\\Roaming\\cabal\\etc"

getBinDir = catchIO (getEnv "addition_bindir") (\_ -> return bindir)
getLibDir = catchIO (getEnv "addition_libdir") (\_ -> return libdir)
getDynLibDir = catchIO (getEnv "addition_dynlibdir") (\_ -> return dynlibdir)
getDataDir = catchIO (getEnv "addition_datadir") (\_ -> return datadir)
getLibexecDir = catchIO (getEnv "addition_libexecdir") (\_ -> return libexecdir)
getSysconfDir = catchIO (getEnv "addition_sysconfdir") (\_ -> return sysconfdir)




joinFileName :: String -> String -> FilePath
joinFileName "" fname = fname
joinFileName "." fname = fname
joinFileName dir "" = dir
joinFileName dir fname
| isPathSeparator (List.last dir) = dir ++ fname
| otherwise = dir ++ pathSeparator : fname

pathSeparator :: Char
pathSeparator = '\\'

isPathSeparator :: Char -> Bool
isPathSeparator c = c == '/' || c == '\\'
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* DO NOT EDIT: This file is automatically generated by Cabal */

/* package addition-0.1.0.0 */
#ifndef VERSION_addition
#define VERSION_addition "0.1.0.0"
#endif /* VERSION_addition */
#ifndef MIN_VERSION_addition
#define MIN_VERSION_addition(major1,major2,minor) (\
(major1) < 0 || \
(major1) == 0 && (major2) < 1 || \
(major1) == 0 && (major2) == 1 && (minor) <= 0)
#endif /* MIN_VERSION_addition */
/* package QuickCheck-2.14.2 */
#ifndef VERSION_QuickCheck
#define VERSION_QuickCheck "2.14.2"
#endif /* VERSION_QuickCheck */
#ifndef MIN_VERSION_QuickCheck
#define MIN_VERSION_QuickCheck(major1,major2,minor) (\
(major1) < 2 || \
(major1) == 2 && (major2) < 14 || \
(major1) == 2 && (major2) == 14 && (minor) <= 2)
#endif /* MIN_VERSION_QuickCheck */
/* package base-4.16.0.0 */
#ifndef VERSION_base
#define VERSION_base "4.16.0.0"
#endif /* VERSION_base */
#ifndef MIN_VERSION_base
#define MIN_VERSION_base(major1,major2,minor) (\
(major1) < 4 || \
(major1) == 4 && (major2) < 16 || \
(major1) == 4 && (major2) == 16 && (minor) <= 0)
#endif /* MIN_VERSION_base */
/* package hspec-2.10.6 */
#ifndef VERSION_hspec
#define VERSION_hspec "2.10.6"
#endif /* VERSION_hspec */
#ifndef MIN_VERSION_hspec
#define MIN_VERSION_hspec(major1,major2,minor) (\
(major1) < 2 || \
(major1) == 2 && (major2) < 10 || \
(major1) == 2 && (major2) == 10 && (minor) <= 6)
#endif /* MIN_VERSION_hspec */

/* tool gcc-10.2.0 */
#ifndef TOOL_VERSION_gcc
#define TOOL_VERSION_gcc "10.2.0"
#endif /* TOOL_VERSION_gcc */
#ifndef MIN_TOOL_VERSION_gcc
#define MIN_TOOL_VERSION_gcc(major1,major2,minor) (\
(major1) < 10 || \
(major1) == 10 && (major2) < 2 || \
(major1) == 10 && (major2) == 2 && (minor) <= 0)
#endif /* MIN_TOOL_VERSION_gcc */
/* tool ghc-9.2.1 */
#ifndef TOOL_VERSION_ghc
#define TOOL_VERSION_ghc "9.2.1"
#endif /* TOOL_VERSION_ghc */
#ifndef MIN_TOOL_VERSION_ghc
#define MIN_TOOL_VERSION_ghc(major1,major2,minor) (\
(major1) < 9 || \
(major1) == 9 && (major2) < 2 || \
(major1) == 9 && (major2) == 2 && (minor) <= 1)
#endif /* MIN_TOOL_VERSION_ghc */
/* tool ghc-pkg-9.2.1 */
#ifndef TOOL_VERSION_ghc_pkg
#define TOOL_VERSION_ghc_pkg "9.2.1"
#endif /* TOOL_VERSION_ghc_pkg */
#ifndef MIN_TOOL_VERSION_ghc_pkg
#define MIN_TOOL_VERSION_ghc_pkg(major1,major2,minor) (\
(major1) < 9 || \
(major1) == 9 && (major2) < 2 || \
(major1) == 9 && (major2) == 2 && (minor) <= 1)
#endif /* MIN_TOOL_VERSION_ghc_pkg */
/* tool haddock-2.26.0 */
#ifndef TOOL_VERSION_haddock
#define TOOL_VERSION_haddock "2.26.0"
#endif /* TOOL_VERSION_haddock */
#ifndef MIN_TOOL_VERSION_haddock
#define MIN_TOOL_VERSION_haddock(major1,major2,minor) (\
(major1) < 2 || \
(major1) == 2 && (major2) < 26 || \
(major1) == 2 && (major2) == 26 && (minor) <= 0)
#endif /* MIN_TOOL_VERSION_haddock */
/* tool hpc-0.68 */
#ifndef TOOL_VERSION_hpc
#define TOOL_VERSION_hpc "0.68"
#endif /* TOOL_VERSION_hpc */
#ifndef MIN_TOOL_VERSION_hpc
#define MIN_TOOL_VERSION_hpc(major1,major2,minor) (\
(major1) < 0 || \
(major1) == 0 && (major2) < 68 || \
(major1) == 0 && (major2) == 68 && (minor) <= 0)
#endif /* MIN_TOOL_VERSION_hpc */
/* tool hsc2hs-0.68.8 */
#ifndef TOOL_VERSION_hsc2hs
#define TOOL_VERSION_hsc2hs "0.68.8"
#endif /* TOOL_VERSION_hsc2hs */
#ifndef MIN_TOOL_VERSION_hsc2hs
#define MIN_TOOL_VERSION_hsc2hs(major1,major2,minor) (\
(major1) < 0 || \
(major1) == 0 && (major2) < 68 || \
(major1) == 0 && (major2) == 68 && (minor) <= 8)
#endif /* MIN_TOOL_VERSION_hsc2hs */
/* tool runghc-9.2.1 */
#ifndef TOOL_VERSION_runghc
#define TOOL_VERSION_runghc "9.2.1"
#endif /* TOOL_VERSION_runghc */
#ifndef MIN_TOOL_VERSION_runghc
#define MIN_TOOL_VERSION_runghc(major1,major2,minor) (\
(major1) < 9 || \
(major1) == 9 && (major2) < 2 || \
(major1) == 9 && (major2) == 2 && (minor) <= 1)
#endif /* MIN_TOOL_VERSION_runghc */
/* tool strip-2.34 */
#ifndef TOOL_VERSION_strip
#define TOOL_VERSION_strip "2.34"
#endif /* TOOL_VERSION_strip */
#ifndef MIN_TOOL_VERSION_strip
#define MIN_TOOL_VERSION_strip(major1,major2,minor) (\
(major1) < 2 || \
(major1) == 2 && (major2) < 34 || \
(major1) == 2 && (major2) == 34 && (minor) <= 0)
#endif /* MIN_TOOL_VERSION_strip */

#ifndef CURRENT_PACKAGE_KEY
#define CURRENT_PACKAGE_KEY "addition-0.1.0.0-inplace"
#endif /* CURRENT_packageKey */
#ifndef CURRENT_COMPONENT_ID
#define CURRENT_COMPONENT_ID "addition-0.1.0.0-inplace"
#endif /* CURRENT_COMPONENT_ID */
#ifndef CURRENT_PACKAGE_VERSION
#define CURRENT_PACKAGE_VERSION "0.1.0.0"
#endif /* CURRENT_PACKAGE_VERSION */
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: addition
version: 0.1.0.0
visibility: public
id: addition-0.1.0.0-inplace
key: addition-0.1.0.0-inplace
maintainer: sky@isfalling.org
author: Chicken Little
category: Text
abi: inplace
exposed: True
exposed-modules: Addition
import-dirs:
"C:\\Users\\Administrator\\Desktop\\Haskell first Principles\\haskell-deepidive\\Haskell Excercises & Code\\Chapter14 - Testing\\dist-newstyle\\build\\x86_64-windows\\ghc-9.2.1\\addition-0.1.0.0\\build"

library-dirs:
"C:\\Users\\Administrator\\Desktop\\Haskell first Principles\\haskell-deepidive\\Haskell Excercises & Code\\Chapter14 - Testing\\dist-newstyle\\build\\x86_64-windows\\ghc-9.2.1\\addition-0.1.0.0\\build"

dynamic-library-dirs:
"C:\\Users\\Administrator\\Desktop\\Haskell first Principles\\haskell-deepidive\\Haskell Excercises & Code\\Chapter14 - Testing\\dist-newstyle\\build\\x86_64-windows\\ghc-9.2.1\\addition-0.1.0.0\\build"

data-dir:
"C:\\Users\\Administrator\\Desktop\\Haskell first Principles\\haskell-deepidive\\Haskell Excercises & Code\\Chapter14 - Testing\\."

hs-libraries: HSaddition-0.1.0.0-inplace
include-dirs: C:\tools\msys64\mingw64\include
depends:
QuickCheck-2.14.2-a86079fca27e4995eff71586bc69486728d69c5d
base-4.16.0.0 hspec-2.10.6-4755191b26c19bdcaf316d380b729c36bd3ac94c

haddock-interfaces:
"C:\\Users\\Administrator\\Desktop\\Haskell first Principles\\haskell-deepidive\\Haskell Excercises & Code\\Chapter14 - Testing\\dist-newstyle\\build\\x86_64-windows\\ghc-9.2.1\\addition-0.1.0.0\\doc\\html\\addition\\addition.haddock"

haddock-html:
"C:\\Users\\Administrator\\Desktop\\Haskell first Principles\\haskell-deepidive\\Haskell Excercises & Code\\Chapter14 - Testing\\dist-newstyle\\build\\x86_64-windows\\ghc-9.2.1\\addition-0.1.0.0\\doc\\html\\addition"
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ instance Traversable TreeA where
-- instance Traversable [] where
-- sequenceA [] = pure []
-- sequenceA (x:xs) = (:) <$> x <*> sequenceA xs
-- sequenceA:: (Traversable t, Applicative f) => t (f a) -> f (t a)

-- instance Traversable [] where
-- traverse _ [] = pure []
Expand Down
4 changes: 3 additions & 1 deletion Haskell Excercises & Code/Chapter22 - Reader/chapterEx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ main = do
-- Solution to 2.
print $ sequA . fromMaybe 0 $ s'
-- Solution to 3.
print $ bolt . fromMaybe 0 $ ys
print $ bolt . fromMaybe 0 $ ys

newtype Coin a = Coin a
29 changes: 29 additions & 0 deletions Haskell Excercises & Code/Chapter22 - Reader/mondayMorning.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module ReaderTest where

-- Calls func2 if even length, func3 and func4 if odd
func1 :: String -> (Int, String)
func1 input = if length input `mod` 2 == 0
then func2 (0, input)
else (i1 + i2, str1 ++ str2)
where
(i1, str1) = func3 (0, tail input)
(i2, str2) = func4 (0, take 1 input)

-- Calls func4 on truncated version
func2 :: (Int, String) -> (Int, String)
func2 (prev, input) = if (length input) > 10
then func4 (prev + 1, take 9 input)
else (10, input)

-- Calls func2 on expanded version if a multiple of 3
func3 :: (Int, String) -> (Int, String)
func3 (prev, input) = if (length input) `mod` 3 == 0
then (prev + f2resI + 3, f2resStr)
else (prev + 1, tail input)
where
(f2resI, f2resStr) = func2 (prev, input ++ "ab")

func4 :: (Int, String) -> (Int, String)
func4 (prev, input) = if (length input) < 10
then (prev + length input, input ++ input)
else (prev + 5, take 5 input)
6 changes: 5 additions & 1 deletion Haskell Excercises & Code/Chapter23 - State/app/ChapterEx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ exec sa s = snd $ runMoi sa s

--4. Get the value that results
eval :: Moi s a -> s -> a
eval (Moi sa) s = fst $ runMoi (Moi sa) s
eval (Moi sa) s = fst $ runMoi (Moi sa) s

-- 5. Applies a function to return a new stte
modify :: (s -> s) -> Moi s ()
modify f = f . put
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ instance (Functor f) => Functor (IdentityT f) where
instance (Applicative f) => Applicative (IdentityT f) where
pure x = IdentityT $ pure x
(IdentityT fab) <*> (IdentityT fa) = IdentityT (fab <*> fa)
-- f (a -> b) f a
-- f (a -> b) f a

instance (Monad f) => Monad (IdentityT f) where
return = pure
(IdentityT fa) >>= fab = IdentityT $ fa >>= runIdentityT . fab

--

-- fa :: f a
-- fab :: (a -> f b)
-- fmap fab fa :: f (f b)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module TestIng where

-- /*

-- I = 1
-- V = 5
-- X = 10
-- L = 50
-- C = 100
-- D = 500
-- M = 1000

-- */

desiredList = [(1,'I'),(5,'V'),(10,'X'),(50,'L'),(100,'C'), (500,'D'),(1000,'M')]
indexing = [1,5,10,50,100,500,1000]


devisionIndex x = [ x / i | i <- reverse indexing]


-- General case
-- Always larger to smaller - left to right
-- 66 == LXVI

-- Special cases:
-- 4 = IV | 9 = IX
-- 40 = XL | 90 = XC
-- 400 = CD | 900 = CM

-- Initial test examples
-- 3 = III
-- 4 = IV
-- 9 = IX
-- 58 = LVIII
-- 1994 = MCMXCIV
-- 3000 = MMM

isBigGang :: Int -> IO () -- (Bool, String)
isBigGang x = print $ (x > 9, "Compared gang size to 9.")
Loading

0 comments on commit 7d76fe3

Please sign in to comment.