Skip to content

Instantly share code, notes, and snippets.

@deniok

deniok/Fp07.hs Secret

Last active Oct 17, 2020
Embed
What would you like to do?
FP_HSE2020Fall_07
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Fp07 where
import Data.List (unfoldr)
import Data.Semigroup
import Data.Monoid
import Data.Coerce
import qualified Data.Foldable as F
sum' :: [Integer] -> Integer
sum' [] = 0
sum' (x:xs) = x + sum' xs
concat' :: [[a]] -> [a]
concat' [] = []
concat' (x:xs) = x ++ concat' xs
allOdd :: [Integer] -> Bool
allOdd [] = undefined
allOdd (x:xs) = odd x && allOdd xs
sum'' :: [Integer] -> Integer
sum'' = foldr (+) 0
concat'' :: [[a]] -> [a]
concat'' = foldr (++) []
allOdd' :: [Integer] -> Bool
allOdd' = foldr (\n b -> odd n && b) True
any' :: (a -> Bool) -> [a] -> Bool
any' p = foldr (\x b -> p x || b) False
{-
GHCi> scanl (++) "!" ["a","b","c"]
["!","!a","!ab","!abc"]
GHCi> scanl (*) 1 [1..] !! 5
120
GHCi> scanr (+) 0 [1,2,3]
[6,5,3,0]
GHCi> scanr (++) "!" ["aa","bb","cc"]
["aabbcc!","bbcc!","cc!","!"]
GHCi> helper x = if x == 0 then Nothing else Just (x,x-1)
GHCi> unfoldr helper 10
[10,9,8,7,6,5,4,3,2,1]
-}
iterate' :: (a -> a) -> a -> [a]
iterate' f = unfoldr (\x -> Just (x, f x))
------------------------------------------------------
{-
GHCi> import Data.List.NonEmpty
GHCi> sconcat $ "AB" :| ["CDE","FG"]
"ABCDEFG"
GHCi> stimes 5 "Ab"
"AbAbAbAbAb"
GHCi> Sum 3 <> Sum 2
Sum {getSum = 5}
-}
newtype Product' a = Product' { getProduct' :: a }
deriving (Eq, Ord, Read, Show, Bounded)
instance Num a => Semigroup (Product' a) where
(<>) = coerce ((*) :: a -> a -> a) -- Data.Coerce
-- нужно расширение ScopedTypeVariables
instance Num a => Monoid (Product' a) where
mempty = Product' 1
{-
GHCi> Product 3 <> Product 2
Product {getProduct = 6}
-}
newtype Min' a = Min' { getMin' :: a }
deriving (Eq, Ord, Read, Show, Bounded)
instance Ord a => Semigroup (Min' a) where
(<>) = coerce (min :: a -> a -> a)
stimes = stimesIdempotent
instance (Ord a, Bounded a) => Monoid (Min' a) where
mempty = maxBound
{-
GHCi> Min "Hello" <> Min "Hi"
Min {getMin = "Hello"}
GHCi> mempty :: Min Int
Min {getMin = 9223372036854775807}
GHCi> (getMin . mconcat . fmap Min) [7,3,2,12] :: Int
2
GHCi> (getMin . mconcat . fmap Min) [] :: Int
9223372036854775807
GHCi> mempty :: Min Integer
<interactive>:26:1: error:
* No instance for (Bounded Integer) arising from a use of `mempty'
* In the expression: mempty :: Min Integer
In an equation for `it': it = mempty :: Min Integer
GHCi> (getMin . mconcat . fmap Min) ["Hello","Hi"]
<interactive>:27:11: error:
* No instance for (Bounded [Char]) arising from a use of `mconcat'
* In the first argument of `(.)', namely `mconcat'
In the second argument of `(.)', namely `mconcat . fmap Min'
In the expression: getMin . mconcat . fmap Min
GHCi> (getMin . sconcat . fromList . fmap Min) ["Hello","Hi"]
"Hello"
GHCi> on n = Min (Arg (n^2-2*n-24) n)
GHCi> :t on
on :: Num b => b -> Min (Arg b b)
GHCi> on 0 <> on 1 <> on 2
Min {getMin = Arg (-25) 1}
GHCi> on s = Min (Arg (F.length s) s)
GHCi> on "Hello" <> on "Hi" <> on "Greetings"
Min {getMin = Arg 2 "Hi"}
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.