haskell - How to write Semigroup instance for this data type? -


i'm trying 1 of semigroup exercises in haskell book (chapter 15, "monoid, semigroup") i'm stuck. following given:

newtype combine b =   combine { uncombine :: (a -> b) } 

and i'm supposed write semigroup instance combine.

and book says must behave following:

 prelude> let f = combine $ \n -> sum (n + 1)  prelude> let g = combine $ \n -> sum (n - 1)  prelude> uncombine (f <> g) $ 0  sum {getsum = 0}  prelude> uncombine (f <> g) $ 1  sum {getsum = 2}  prelude> uncombine (f <> f) $ 1  sum {getsum = 4}  prelude> uncombine (g <> f) $ 1  sum {getsum = 2} 

so first started wrong solution type checks:

instance semigroup (combine b)   combine f <> combine g = combine f 

that not expected of course, step in right direction. , thinking following, in pseudocode:

 instance semigroup (combine b)    (combine f) <> (combine g) = combine (something) 

that something being: f , g appended, whatever concrete append operation (it depends on f , g); think requires <> data.monoid, have import data.semigroup in code, , therefore <> data.monoid coincides 1 data.semigroup. supposed do?

i tried find out how can state "combine (f monoid's <> g)", couldn't find out.

the book states unless i'm using ghc 8.x, i'll have import semigroup , might have "shadow" <> monoid; i'm struggling find out how have effect.

any ideas?

what want addition of functions. that, type b needs semigroup :

import data.semigroup  newtype combine b =   combine { uncombine :: (a -> b) }  instance semigroup b => semigroup (combine b)   (combine f) <> (combine g) = combine (\x -> f x <> g x) 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -