finished code

This commit is contained in:
Lars Brünjes 2021-05-27 08:27:21 +02:00
parent a596b3f086
commit 175f5c680f
No known key found for this signature in database
GPG key ID: B488B9045DC1A087
4 changed files with 85 additions and 1 deletions

View file

@ -10,10 +10,13 @@ License-files: LICENSE
library
hs-source-dirs: src
exposed-modules: Week08.TokenSale
exposed-modules: Week08.Lens
, Week08.QuickCheck
, Week08.TokenSale
build-depends: aeson
, base ^>=4.14.1.0
, containers
, lens
, playground-common
, plutus-contract
, plutus-ledger
@ -22,6 +25,7 @@ library
, plutus-tx
, plutus-use-cases
, prettyprinter
, QuickCheck
, text
default-language: Haskell2010
ghc-options: -Wall -fobject-code -fno-ignore-interface-pragmas -fno-omit-interface-pragmas -fno-strictness -fno-spec-constr -fno-specialise

View file

@ -0,0 +1,40 @@
{-# LANGUAGE TemplateHaskell #-}
module Week08.Lens where
import Control.Lens
newtype Company = Company {_staff :: [Person]} deriving Show
data Person = Person
{ _name :: String
, _address :: Address
} deriving Show
newtype Address = Address {_city :: String} deriving Show
alejandro, lars :: Person
alejandro = Person
{ _name = "Alejandro"
, _address = Address {_city = "Zacateca"}
}
lars = Person
{ _name = "Lars"
, _address = Address {_city = "Regensburg"}
}
iohk :: Company
iohk = Company { _staff = [alejandro, lars] }
goTo :: String -> Company -> Company
goTo there c = c {_staff = map movePerson (_staff c)}
where
movePerson p = p {_address = (_address p) {_city = there}}
makeLenses ''Company
makeLenses ''Person
makeLenses ''Address
goTo' :: String -> Company -> Company
goTo' there c = c & staff . each . address . city .~ there

View file

@ -0,0 +1,39 @@
module Week08.QuickCheck where
import Test.QuickCheck
prop_simple :: Bool
prop_simple = 2 + 2 == (4 :: Int)
-- Insertion sort code:
-- | Sort a list of integers in ascending order.
--
-- >>> sort [5,1,9]
-- [1,5,9]
--
sort :: [Int] -> [Int] -- not correct
sort [] = []
sort (x:xs) = insert x $ sort xs
-- | Insert an integer at the right position into an /ascendingly sorted/
-- list of integers.
--
-- >>> insert 5 [1,9]
-- [1,5,9]
--
insert :: Int -> [Int] -> [Int] -- not correct
insert x [] = [x]
insert x (y:ys) | x <= y = x : y : ys
| otherwise = y : insert x ys
isSorted :: [Int] -> Bool
isSorted [] = True
isSorted [_] = True
isSorted (x : y : ys) = x <= y && isSorted (y : ys)
prop_sort_sorts :: [Int] -> Bool
prop_sort_sorts xs = isSorted $ sort xs
prop_sort_preserves_length :: [Int] -> Bool
prop_sort_preserves_length xs = length (sort xs) == length xs

View file

@ -17,6 +17,7 @@
module Spec.Model
( tests
, test
, TSModel (..)
) where
import Control.Lens hiding (elements)