Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 692791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:41:13+00:00 2026-05-14T02:41:13+00:00

I want to write ‘twice’ function that takes a function and an argument and

  • 0

I want to write ‘twice’ function that takes a function and an argument and applies the function twice. However the function that it receives should work on union types.

eg.

    f a -> b 
    f b -> c

Output

   twice f a
 c
   f a
   b
   f b 
   c
   f c
   error

eg.

f :: Int -> String
f :: String -> Char
twice f :: Int -> Cha

how do I write f that takes two types and ‘twice’ that does the transitive thing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:41:14+00:00Added an answer on May 14, 2026 at 2:41 am

    You’re really asking two things here: “How do I write the function twice?”, and “how do I write an f with two different types?”

    Let’s think about the first question. Letting Haskell infer types for the moment, let’s think about what it should look like. It needs to take one argument: twice f = undefined. twice then returns a function which takes an argument and applies f to it twice: twice f = \x -> f (f x).

    But what’s the type of this function? Well, x must be of some type α. Since we evaluate (f x), this means that f must be a function that takes in an α and returns a β: f :: α -> β. However, we also evaluate f (f x), so f must take a β as input as well, returning a γ: f :: β -> γ. Any single variable can only have one type, so this tells us that α -> β = β -> γ, and so α = β and β = γ. Thus, f :: α -> α, and so \x -> f (f x) :: α -> α; this means that twice :: (α -> α) -> α -> α.

    This answers your first question. And you’ll notice that I said above that f must be a function from one type to the same type. This answers your second question: it is impossible to write an f with two different types. This is because, as I said, any single variable may only have one (possibly polymorphic) type. Why? Well, among other reasons, suppose we have a variable impossible with two type signatures, impossible :: Int and impossible :: String, and two bindings, impossible = 24 and impossible = "absz". Then what does show impossible return? The show function is of type show :: Show α => α -> String; since both Int and String are instances of the Show typeclass, we can’t tell if this would return "42" or "\"absz\"". Inconsistencies like this are why we allow only one type.

    All hope is not lost, however! You also mentioned using union types to implement f. In this context, you probably mean the Either type (although all datatypes in Haskell are a form of union types called discriminated unions). Either is a type which takes two type parameters (just like [], the list type, takes one); we say that it has kind [the type of a type] Either :: * -> * -> *). Either is the union type: Either A B consists of all the elements of A and all the elements of B, lifted into Either. As Michael Steele said, you can write your function with two type signatures as a function which returns an Either value: f :: Either δ ε -> Either δ ε. Note that this is a perfectly valid value to pass as a parameter to twice, since Either δ ε is a perfectly legal type. We define functions on Either via pattern matching; the two constructors of Either are Left :: δ -> Either δ ε and Right :: ε -> Either δ ε, for lifting the two types of values. A sample function, then, would look like

    f :: Either Int String -> Either Int String
    f (Left  n) = Right $ "The number " ++ show n
    f (Right s) = Left  $ length s
    
    -- f (Left 3)               == Right "The number 3"
    -- f (Right "The number 3") == Left 12
    -- twice f (Left 3)         == Left 12
    

    If you really want to mimic your example and go through three types, from α to β to γ, you can either use nested Eithers or define your own data type. With nested Eithers, you get

    f :: Either Int (Either String Char) -> Either Int (Either String Char)
    f (Left  n)         = Right $ Left  $ "The number " ++ show n
    f (Right (Left  s)) = Right $ Right $ head $ drop 11 s
    f (Right (Right c)) = Left  $ fromEnum c
    
    -- f (Left 42)                      == Right (Left "The number 42")
    -- f (Right (Left "The number 42")) == Right (Right '4')
    -- f (Right (Right '4'))            == Left 52
    -- twice f (Left 42)                == Right (Right '4')
    

    With a new type, you get:

    data Either3 a b c = Left3 a | Mid3 b | Right3 c deriving (Eq, Ord, Read, Show)
    
    f :: Either3 Int String Char -> Either3 Int String Char
    f (Left3  n) = Mid3   $ "The number " ++ show n
    f (Mid3   s) = Right3 $ head $ drop 11 s
    f (Right3 c) = Left3  $ fromEnum c
    
    -- f (Left3 42)             == Mid3 "The number 42"
    -- f (Mid3 "The number 42") == Right3 '4'
    -- f (Right3 '4')           == Left3 52
    -- twice f (Left3 42)       == Right3 '4'
    

    You could also define a specific data MyType = MyInt Int | MyStr String | MyChar Char, and replace every Either3 Int String Char with MyType, every Left3 with MyInt, every Mid3 with MyStr, and every Right3 with MyChar; this is effectively the same thing, but less general.

    Note that, thanks to Haskell’s currying, we can rewrite our original twice as twice f x = f (f x). And in fact, even more simply, we can write this as twice f = f (.) f, or twice = join (.), if we import Control.Monad. This is irrelevant for the purposes of answering this question, but is interesting for other reasons (especially the (->) α instance for Monad, which I don’t fully understand); you might want to take a look if you haven’t seen it before.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that takes an array of letters as an
I want to write a function in Python that returns different fixed values based
I want to write a command that specifies the word under the cursor in
I want to write a little DBQuery function in perl so I can have
I want to write a word addin that does some computations and updates some
I want to write some JavaScript that will change the onmousedown of a div
I want to write something that acts just like confirm() in javascript, but I
I want write a Special Messenger (should be able to some transaction) with RMI(Remote
Hello I want write my own desktop sharing application in Java. The application should
I have whole page HTML in my database and i want write that html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.