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 7549377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:56:07+00:00 2026-05-30T09:56:07+00:00

I’m following this introduction to Haskell , and this particular place (user defined types

  • 0

I’m following this introduction to Haskell, and this particular place (user defined types 2.2) I’m finding particularly obscure. To the point, I don’t even understand what part of it is code, and what part is the thoughts of the author. (What is Pt – it is never defined anywhere?). Needless to say, I can’t execute / compile it.

As an example that would make it easier for me to understand, I wanted to define a type, which is a pair of an Integer and a String, or a String and an Integer, but nothing else.

The theoretical function that would use it would look like so:

combine :: StringIntPair -> String
combine a b = (show a) ++ b
combine a b = a ++ (show b)

If you need a working code, that does the same, here’s CL code for doing it:

(defgeneric combine (a b)
  (:documentation "Combines strings and integers"))

(defmethod combine ((a string) (b integer))
  (concatenate 'string a (write-to-string b)))

(defmethod combine ((a integer) (b string))
  (concatenate 'string (write-to-string a) b))

(combine 100 "500")
  • 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-30T09:56:09+00:00Added an answer on May 30, 2026 at 9:56 am

    Here’s one way to define the datatype:

    data StringIntPair = StringInt String Int | 
                         IntString Int String 
        deriving (Show, Eq, Ord)
    

    Note that I’ve defined two constructors for type StringIntPair, and they are StringInt and IntString.

    Now in the definition of combine:

    combine :: StringIntPair -> String
    combine (StringInt s i) = s ++ (show i)
    combine (IntString i s) = (show i) ++ s
    

    I’m using pattern matching to match the constructors and select the correct behavior.

    Here are some examples of usage:

    *Main> let y = StringInt "abc" 123
    *Main> let z = IntString 789 "a string"
    *Main> combine y
    "abc123"
    *Main> combine z
    "789a string"
    *Main> :t y
    y :: StringIntPair
    *Main> :t z
    z :: StringIntPair
    

    A few things to note about the examples:

    • StringIntPair is a type; doing :t <expression> in the interpreter shows the type of an expression
    • StringInt and IntString are constructors of the same type
    • the vertical bar (|) separates constructors
    • a well-written function should match each constructor of its argument’s types; that’s why I’ve written combine with two patterns, one for each constructor
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string

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.