in Education by
I am trying to define instances for Functor,Applicative and Monad for the following type: data BTree a=Leaf a | Node (BTree a) (BTree a) deriving (Eq,Show) I have tried implementing the Functor instance like this: instance Functor BTree where fmap f (Leaf t) =Leaf (f t) fmap f (Node a b) =Node (f a) (f b) What did work fmap f (Node a b)=Node (fmap f a) (fmap f b) I understand it is not correct since being an instance of functor , the form has to be preserved f a -> f b (in our case Node). and in my implementation you would get f a -> b. What i don't understand: Why is it an infinite type? Considering Node (f a )(f b) somewhere down the hierarchy a child Node will be Leafand i will apply f to it. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You are trying to apply f to values of type a (in Leaf (f t)) and to values of type BTree a (in Node (f a) (f b)). For this to work, the type checker needs to find some way to unify a and BTree a, which is only possible if a is an infinitely nested stack of BTree types; adding one more layer of BTree on top of a wouldn't effectively change it. Changing Node (f a) (f b) to Node (fmap f a) (fmap f b) ensures that f is only applied to values of type a.

Related questions

0 votes
    Still new to Haskell, I have hit a wall with the following: I am trying to define some type ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    What would be a good place to go to understand arrows? Ideally, I am just looking for some place ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm learning Haskell in the hope that it will help me get closer to functional programming. Previously, I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
0 votes
    Construct logical expressions to represent the following conditions a) salary is not in the range 2500 to 5000 Select the correct answer from above options...
asked Dec 11, 2021 in Education by JackTerrance
0 votes
    The web development environment (JavaScript) offers which standard construct for data validation of the input entered ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    ________________ is a network construct over the internet that is encrypted. It offers anonymity to its users. Tor (The Onion ... . 1) Freenet 2) Darknet 3) ARPANET 4) Stuxnet...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    I need to react in a main process to random events happening in a child process. I have implemented ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    I have a QGraphicsItem element (subclassesed from QGraphicsItem) that has as child a QGraphicsTextItem. The problem ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    An infinite ladder is constructed with 1 Ω and 2 Ω resistor shown below. The current I flowing through the circuit ... GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    I have been wrestling with this for a while. I am trying to parse a JSON Api into a UITableview. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    Which of these types cannot be used to initiate a generic type? (a) Integer class (b) Float class (c) ... in section Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of the following type of data, phishers cannot steal from its target victims? (a) bank details (b) ... -for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    Which of these Exception handlers cannot be type parameterized? (a) catch (b) throw (c) throws (d ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Which of these types cannot be used to initiate a generic type? (a) Integer class (b) Float class ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    When new SaveModelAction() is called, the corresponding Effect gets stuck in an infinite loop. This the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...