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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:59:21+00:00 2026-05-28T18:59:21+00:00

I have a bunch of functions like: f1 :: String -> String -> …

  • 0

I have a bunch of functions like:

f1 :: String -> String -> ... -> String -> ()
f1 a b ... z = g [("a", a), ("b", b), ... ("z", z)]
...
fn :: String -> Int -> String -> ... -> String -> ()
fn a b ... z = g [("a", a), ("b", show b), ... ("z", z)]

So user can just call them like f1 "abc" "def". I don’t want him to do this because he can easily swap “abc” and “def” by mistake (and God knows how much time would be wasted while debugging). I want him to pass arguments like fk (A "abc") (B "def")
As far as I can see, there are 2 options:

  1. Massive data construction and massive unpack function:

    data Value = A String
               | B String
               | C Int
               | D String
               ...
    
     unpack :: Value -> String
     unpack (A a) = a
     unpack (B b) = b
     unpack (C c) = show c
     unpack (D c) = d
    

    Lots of code.

  2. Common typeclass and newtypes:
    EDIT: Okay then, we can use GeneralizedNewtypeDeriving in such simple case.

      {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    
      class Value a where
        unpack :: a -> String
      instance Value String where
        unpack = id
      instance Value Int where
        unpack = show
    
      newtype A = A String deriving Value
      newtype B = B String deriving Value
      newtype C = C Int deriving Value
      newtype D = D String deriving Value
    
      ...
    

    Looks much better but all fk would look like

       fk a b ... z = g [("a", unpack a), ("b", unpack b), ... ("z", unpack z)]
    

    Lots of code and duplication.

What I want is some magic trick which would allow me:

  1. fk a b ... z = g [("a", a), ("b", b), ... ("z", z)]
  2. g = h . map (second unpack)
  • 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-28T18:59:22+00:00Added an answer on May 28, 2026 at 6:59 pm

    I think the problems boils down to this: The list can have elements of the same type only; which means that either you have to ‘coalesce’ it into a single type in your f, or you cannot rely on haskells type checks. E.g. the following code would work for you, but the type check is runtime:

    {-# LANGUAGE GADTs #-}
    
    import Control.Arrow (second)
    
    data Item where
        A :: String -> Item
        B :: Int -> Item
    
    unpack (A s) = s
    unpack (B i) = show i
    
    myf a@(A {}) b@(B {}) c@(B {}) = 
        let g = [("a", a), ("b", b), ("c", c)]
        in map (second unpack) g
    myf _ _ _ = error "Bad types"
    
    main = do
        putStrLn $ show $ myf (A "test") (B 13) (B 14)
        putStrLn $ show $ myf (A "test") (B 13) (A "xxx")
    

    When you want compile-time type check, you can do something like this; however, you still have to retype the parameters to the same type, so in some sense, there is not much difference between unpacking it, only it might by slightly less error-prone, though. A nice trick comes from the json packages – they redefine some operator (e.g. =:) to create the type, so you would have:

    {-# LANGUAGE ExistentialQuantification #-}
    import Control.Arrow (second)
    
    class Value a where
        unpack :: a -> String
    newtype A = A String
    newtype B = B Int
    
    instance Value A where
        unpack (A a) = a
    
    instance Value B where
        unpack (B b) = show b
    
    data Item = forall b. Value b => Item b
    a =: b = (a, Item b)
    
    myf :: A -> B -> B -> [(String, String)]
    myf a b c = 
        let g = ["a" =: a, "b" =: b, "c" =: c]
        in map (second (\(Item x) -> unpack x)) g
    
    main = do
        putStrLn $ show $ myf (A "test") (B 13) (B 14)
    

    It’s not that much different from just defining a =: b = (a, unpack b) though.

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

Sidebar

Related Questions

I have a bunch of Python functions. Let's call them foo , bar and
I have a bunch of functions with signatures like: FUNCTION func1 (par1 IN VARCHAR2,
I have a bunch of generated functions from a supplier's tool are required to
I have a bunch of overloaded functions that operate on certain data types such
I have an mxml file with a bunch of functions in the script tag,
I have a bunch of javascript functions which depend on some server-side constants (such
I have a boost variant of looking like this: typedef boost::variant<int, float, double, long,
I have a bunch of F# functions that implement different algorithms for the same
I have a bunch of divs that are all hidden like this <div class=elements>
I have a bunch of legacy documents that are HTML-like. As in, they look

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.