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

  • SEARCH
  • Home
  • 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 8546237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:58:14+00:00 2026-06-11T12:58:14+00:00

If I had data and I wasn’t sure if this was an Integer or

  • 0

If I had data and I wasn’t sure if this was an Integer or a String, but I wanted to apply (+1) to it, if its an integer great, but if its a String- do nothing, how would I handle this? Is this where Nothing comes in?

  • 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-06-11T12:58:15+00:00Added an answer on June 11, 2026 at 12:58 pm

    Well, Haskell functions are strongly typed, which means they specify the types of their inputs and outputs. In order to even accept a value of multiple types in the first place, you need to use Either to hold them within the same type. So, for example, if you want to receive either a String or an Integer, then your function must have type:

    f :: Either String Integer -> ...
    

    Then, the way you write your function is to pattern match on the Either to see which type of value you received. You would write something like:

    -- str is a String
    f (Left  str) -> ... 
    -- int is an Integer
    f (Right int) -> ...
    

    So, the easiest way to do what you want is to only increment the number if it is an Integer, but leave the String untouched. You would write it like this:

    -- Don't do anything if it is a string
    f (Left  str) = Left  str
    -- Increment it if it is an integer
    f (Right int) = Right (int + 1)
    

    If you ask the compiler to infer the type of the above function, you will get:

    f :: Either String Int -> Either String Int
    

    Haskell has a nice trick to avoid the above boilerplate, which is to use fmap from the Functor class. This lets us automatically write functions on just the Right half of the Either while completely ignoring whatever is in the Left half, like so:

    f = fmap (+1)
    

    If we infer the type of the above function, it actually comes out to:

    f :: (Functor f, Num a) => f a -> f a
    

    But in our case we can specialize the type by setting a to Integer and f to Either String:

    f :: Either String Int -> Either String Int
    

    In other words, Either String is one example of an instance of the Functor class, of which there are many.

    However, note that in order to use this function on integers or strings, you must first wrap them in Left or Right constructors. For example, these will not type-check:

    f 1   -- WRONG!
    f "a" -- WRONG!
    

    But these will:

    f (Right  1) -- Correct!
    f (Left "a") -- Correct!
    

    This means that if you had, say, a list of integers an strings, you would have to write it something like:

    list = [Right 1, Left "a", Right 2]
    

    Notice that if you try to mix integers and strings within a list, you get a type error:

    list = [1, "a", 2] -- WRONG!
    

    Then we could map f over the first correct list to get:

    map f list = [Right 2, Left "a", Right 3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had put up a few questions related but i wasn't clear enough. I
In Python 2.5 I stored data using this code: def GLWriter(file_name, string): import cPickle
I've been trying this for hours, but haven't had any luck. Every tutorial I
I have a live database that had some data deleted from it and I
Suppose I'm making a JDBC call and I had fetched data from a database
I Had Drop down list and I want to fill it with data from
Note: I had another similar question about how to GZIP data using Ruby's zlib
I had to convert some code (which deals with building a VBO of data
I recently had a discussion with a colleague about serialization of byte data over
I've never had the need to really ever use any of the .NET Data

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.