How to write fmap for a Functor that has a function in it's data type definition?Functor fmap, pattern match function values, haskellHaving trouble writing my fmapHow can a time function exist in functional programming?Deriving functor instance, not on last type argumentHow to make the function type constructor instance of functor? Duplicated instanceGHC 7.10.x migration: why can't one write “pure = return” in the instance Applicative?Can I Create a Functor Data Type That 'Looks' Like an Int?Isn't it redundant for Control.Lens.Setter to wrap types in functors?Recursion schemes using `Fix` on a data-type that's already a Functor?How to implement a instance of Functor for a self defined data in haskell`(<*>)` definition for the Applicative functor?
Is this statement about cut time correct?
Why did the Dothraki not follow Jon?
What does $!# mean in Shell scripting?
Who decides how to classify a novel?
A steel cutting sword?
My players want to grind XP but we're using milestone advancement
Time complexity of an algorithm: Is it important to state the base of the logarithm?
In the 3D Zeldas, is it faster to roll or to simply walk?
Pirate democracy at its finest
Sankey diagram: not getting the hang of it
What are the meaning and grammar of "Crying isn't like you"?
Dad jokes are fun
Does this strict reading of the rules allow both Extra Attack and the Thirsting Blade warlock invocation to be used together?
Why aren't space telescopes put in GEO?
Website returning plaintext password
Which European Languages are not Indo-European?
Is it legal to meet with potential future employers in the UK, whilst visiting from the USA
What was the idiom for something that we take without a doubt?
USPS Back Room - Trespassing?
Did 20% of US soldiers in Vietnam use heroin, 95% of whom quit afterwards?
Why didn't Thanos use the Time Stone to stop the Avengers' plan?
Best material to absorb as much light as possible
Could a 19.25mm revolver actually exist?
The art of clickbait captions
How to write fmap for a Functor that has a function in it's data type definition?
Functor fmap, pattern match function values, haskellHaving trouble writing my fmapHow can a time function exist in functional programming?Deriving functor instance, not on last type argumentHow to make the function type constructor instance of functor? Duplicated instanceGHC 7.10.x migration: why can't one write “pure = return” in the instance Applicative?Can I Create a Functor Data Type That 'Looks' Like an Int?Isn't it redundant for Control.Lens.Setter to wrap types in functors?Recursion schemes using `Fix` on a data-type that's already a Functor?How to implement a instance of Functor for a self defined data in haskell`(<*>)` definition for the Applicative functor?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have data defined as below:
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a)
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)
FGM represents a number guessing game where the 2 integers are the upper and lower bounds, a is the value to be returned in the end, and playermsg is the guess that the player makes.
I would like to make FGM a Functor, so I'm trying to define a fmap for it. Problem is I don't know how do deal with the (PlayerMsg -> FGM a) part of FGM.
From my understanding of the testcases given, for fmap g (someFGM), g should only be applied to Pure, if it's GMAction, fmap should use PlayerMsg and generate the next FGM until it reaches a Pure.
This is what I've tried:
instance Functor FGM where
fmap g (Pure a) = Pure (g a)
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))
But when I try to run test on this I get this error:
Occurs check: cannot construct the infinite type:
b ~ FGM b
Expected type: PlayerMsg -> FGM b
Actual type: PlayerMsg -> b
In the third argument of 'GMAction', namely '(fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))'
In the expression:...
I've also tried
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g thing)
and got the same error.
I tried to search for examples with data that has a function as parameter like PlayerMsg -> FGM a, but can't find anything. this is kind of similar but didn't work for me(it's my first failed example). Can someone help me define this fmap or point me at where to learn about this?
haskell
add a comment |
I have data defined as below:
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a)
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)
FGM represents a number guessing game where the 2 integers are the upper and lower bounds, a is the value to be returned in the end, and playermsg is the guess that the player makes.
I would like to make FGM a Functor, so I'm trying to define a fmap for it. Problem is I don't know how do deal with the (PlayerMsg -> FGM a) part of FGM.
From my understanding of the testcases given, for fmap g (someFGM), g should only be applied to Pure, if it's GMAction, fmap should use PlayerMsg and generate the next FGM until it reaches a Pure.
This is what I've tried:
instance Functor FGM where
fmap g (Pure a) = Pure (g a)
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))
But when I try to run test on this I get this error:
Occurs check: cannot construct the infinite type:
b ~ FGM b
Expected type: PlayerMsg -> FGM b
Actual type: PlayerMsg -> b
In the third argument of 'GMAction', namely '(fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))'
In the expression:...
I've also tried
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g thing)
and got the same error.
I tried to search for examples with data that has a function as parameter like PlayerMsg -> FGM a, but can't find anything. this is kind of similar but didn't work for me(it's my first failed example). Can someone help me define this fmap or point me at where to learn about this?
haskell
1
Side note: the lambda with the case expression amounts tothing.
– duplode
Mar 24 at 2:26
add a comment |
I have data defined as below:
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a)
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)
FGM represents a number guessing game where the 2 integers are the upper and lower bounds, a is the value to be returned in the end, and playermsg is the guess that the player makes.
I would like to make FGM a Functor, so I'm trying to define a fmap for it. Problem is I don't know how do deal with the (PlayerMsg -> FGM a) part of FGM.
From my understanding of the testcases given, for fmap g (someFGM), g should only be applied to Pure, if it's GMAction, fmap should use PlayerMsg and generate the next FGM until it reaches a Pure.
This is what I've tried:
instance Functor FGM where
fmap g (Pure a) = Pure (g a)
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))
But when I try to run test on this I get this error:
Occurs check: cannot construct the infinite type:
b ~ FGM b
Expected type: PlayerMsg -> FGM b
Actual type: PlayerMsg -> b
In the third argument of 'GMAction', namely '(fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))'
In the expression:...
I've also tried
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g thing)
and got the same error.
I tried to search for examples with data that has a function as parameter like PlayerMsg -> FGM a, but can't find anything. this is kind of similar but didn't work for me(it's my first failed example). Can someone help me define this fmap or point me at where to learn about this?
haskell
I have data defined as below:
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a)
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)
FGM represents a number guessing game where the 2 integers are the upper and lower bounds, a is the value to be returned in the end, and playermsg is the guess that the player makes.
I would like to make FGM a Functor, so I'm trying to define a fmap for it. Problem is I don't know how do deal with the (PlayerMsg -> FGM a) part of FGM.
From my understanding of the testcases given, for fmap g (someFGM), g should only be applied to Pure, if it's GMAction, fmap should use PlayerMsg and generate the next FGM until it reaches a Pure.
This is what I've tried:
instance Functor FGM where
fmap g (Pure a) = Pure (g a)
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))
But when I try to run test on this I get this error:
Occurs check: cannot construct the infinite type:
b ~ FGM b
Expected type: PlayerMsg -> FGM b
Actual type: PlayerMsg -> b
In the third argument of 'GMAction', namely '(fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))'
In the expression:...
I've also tried
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g thing)
and got the same error.
I tried to search for examples with data that has a function as parameter like PlayerMsg -> FGM a, but can't find anything. this is kind of similar but didn't work for me(it's my first failed example). Can someone help me define this fmap or point me at where to learn about this?
haskell
haskell
edited Mar 24 at 2:23
duplode
25.1k45296
25.1k45296
asked Mar 24 at 2:11
Ceilvia CCeilvia C
485
485
1
Side note: the lambda with the case expression amounts tothing.
– duplode
Mar 24 at 2:26
add a comment |
1
Side note: the lambda with the case expression amounts tothing.
– duplode
Mar 24 at 2:26
1
1
Side note: the lambda with the case expression amounts to
thing.– duplode
Mar 24 at 2:26
Side note: the lambda with the case expression amounts to
thing.– duplode
Mar 24 at 2:26
add a comment |
2 Answers
2
active
oldest
votes
Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.
Yay it worked! Thank you! yes that's typo I will edit it
– Ceilvia C
Mar 24 at 2:22
add a comment |
GHC's built-in rules for deriving Functor are enough to handle simple cases like this.
-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)
Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!
– bradrn
Mar 24 at 7:56
Good to know this built-in exist, thanks!
– Ceilvia C
Mar 24 at 18:13
@CeilviaC: You can also see the code that GHC generates fromderivingclauses with the-ddump-derivflag. (If you’re using Stack, this will generate a.dump-derivfile in your.stack-workdirectory.)
– Jon Purdy
Mar 24 at 20:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320147%2fhow-to-write-fmap-for-a-functor-that-has-a-function-in-its-data-type-definition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.
Yay it worked! Thank you! yes that's typo I will edit it
– Ceilvia C
Mar 24 at 2:22
add a comment |
Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.
Yay it worked! Thank you! yes that's typo I will edit it
– Ceilvia C
Mar 24 at 2:22
add a comment |
Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.
Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.
edited Mar 24 at 2:24
duplode
25.1k45296
25.1k45296
answered Mar 24 at 2:18
bradrnbradrn
2,2331819
2,2331819
Yay it worked! Thank you! yes that's typo I will edit it
– Ceilvia C
Mar 24 at 2:22
add a comment |
Yay it worked! Thank you! yes that's typo I will edit it
– Ceilvia C
Mar 24 at 2:22
Yay it worked! Thank you! yes that's typo I will edit it
– Ceilvia C
Mar 24 at 2:22
Yay it worked! Thank you! yes that's typo I will edit it
– Ceilvia C
Mar 24 at 2:22
add a comment |
GHC's built-in rules for deriving Functor are enough to handle simple cases like this.
-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)
Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!
– bradrn
Mar 24 at 7:56
Good to know this built-in exist, thanks!
– Ceilvia C
Mar 24 at 18:13
@CeilviaC: You can also see the code that GHC generates fromderivingclauses with the-ddump-derivflag. (If you’re using Stack, this will generate a.dump-derivfile in your.stack-workdirectory.)
– Jon Purdy
Mar 24 at 20:35
add a comment |
GHC's built-in rules for deriving Functor are enough to handle simple cases like this.
-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)
Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!
– bradrn
Mar 24 at 7:56
Good to know this built-in exist, thanks!
– Ceilvia C
Mar 24 at 18:13
@CeilviaC: You can also see the code that GHC generates fromderivingclauses with the-ddump-derivflag. (If you’re using Stack, this will generate a.dump-derivfile in your.stack-workdirectory.)
– Jon Purdy
Mar 24 at 20:35
add a comment |
GHC's built-in rules for deriving Functor are enough to handle simple cases like this.
-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)
GHC's built-in rules for deriving Functor are enough to handle simple cases like this.
-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)
answered Mar 24 at 7:27
Daniel WagnerDaniel Wagner
106k7163291
106k7163291
Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!
– bradrn
Mar 24 at 7:56
Good to know this built-in exist, thanks!
– Ceilvia C
Mar 24 at 18:13
@CeilviaC: You can also see the code that GHC generates fromderivingclauses with the-ddump-derivflag. (If you’re using Stack, this will generate a.dump-derivfile in your.stack-workdirectory.)
– Jon Purdy
Mar 24 at 20:35
add a comment |
Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!
– bradrn
Mar 24 at 7:56
Good to know this built-in exist, thanks!
– Ceilvia C
Mar 24 at 18:13
@CeilviaC: You can also see the code that GHC generates fromderivingclauses with the-ddump-derivflag. (If you’re using Stack, this will generate a.dump-derivfile in your.stack-workdirectory.)
– Jon Purdy
Mar 24 at 20:35
Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!
– bradrn
Mar 24 at 7:56
Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!
– bradrn
Mar 24 at 7:56
Good to know this built-in exist, thanks!
– Ceilvia C
Mar 24 at 18:13
Good to know this built-in exist, thanks!
– Ceilvia C
Mar 24 at 18:13
@CeilviaC: You can also see the code that GHC generates from
deriving clauses with the -ddump-deriv flag. (If you’re using Stack, this will generate a .dump-deriv file in your .stack-work directory.)– Jon Purdy
Mar 24 at 20:35
@CeilviaC: You can also see the code that GHC generates from
deriving clauses with the -ddump-deriv flag. (If you’re using Stack, this will generate a .dump-deriv file in your .stack-work directory.)– Jon Purdy
Mar 24 at 20:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320147%2fhow-to-write-fmap-for-a-functor-that-has-a-function-in-its-data-type-definition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Side note: the lambda with the case expression amounts to
thing.– duplode
Mar 24 at 2:26