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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:07:07+00:00 2026-05-16T08:07:07+00:00

I need a function which takes an arbitrary number of arguments (All of the

  • 0

I need a function which takes an arbitrary number of arguments (All of the same type), does something with them and afterwards gives a result back. A list of arguments is impracticable in my specific case.

As I looked through the haskell libs, I saw that the function printf (from module Text.Printf) uses a similar trick. Unfortunately, I couldn’t understand that magic by looking at the source.

Can somebody explain how to achieve this, or at least some webpage/paper/whatever where I could find a good description for this?

Motivation:

The reason I need this is really quite simple. For school (computer science class), we are required to write a module that is able to “record” a mathematical expression, express it as a string (Via writing an instance of Num/Real/etc for an own datatype), and perform various operations on it.

This datatype contains a special constructor for a variable, which may be replaced by a value or whatever by a specified function. One of the goals is to write a function, which takes such an expression with some number of variables (pairs of type (Char,Rational)) and calculates the result of the expression. We should look at how to express the goal of the function best. (My idea: The function returns another function which takes exactly as many arguments as vars that are defined in the function – seems to be impossible).

  • 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-16T08:07:08+00:00Added an answer on May 16, 2026 at 8:07 am

    The key points of printf is the ability to either return a String or a function. Copied from http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/src/Text-Printf.html,

    printf :: (PrintfType r) => String -> r
    printf fmts = spr fmts []
    
    class PrintfType t where
        spr :: String -> [UPrintf] -> t
    
    instance (IsChar c) => PrintfType [c] where
        spr fmts args = map fromChar (uprintf fmts (reverse args))
    
    instance (PrintfArg a, PrintfType r) => PrintfType (a -> r) where
        spr fmts args = \a -> spr fmts (toUPrintf a : args)
    

    and the basic structure we can extract out is

    variadicFunction :: VariadicReturnClass r => RequiredArgs -> r
    variadicFunction reqArgs = variadicImpl reqArgs mempty
    
    class VariadicReturnClass r where
       variadicImpl :: RequiredArgs -> AccumulatingType -> r
    
    instance VariadicReturnClass ActualReturnType where
       variadicImpl reqArgs acc = constructActualResult reqArgs acc
    
    instance (ArgClass a, VariadicReturnClass r) => VariadicReturnClass (a -> r) where
       variadicImpl reqArgs acc = \a -> variadicImpl reqArgs (specialize a `mappend` acc)
    

    For instance:

    class SumRes r where 
        sumOf :: Integer -> r
    
    instance SumRes Integer where
        sumOf = id
    
    instance (Integral a, SumRes r) => SumRes (a -> r) where
        sumOf x = sumOf . (x +) . toInteger
    

    then we could use

    *Main> sumOf 1 :: Integer
    1
    *Main> sumOf 1 4 7 10 :: Integer
    22
    *Main> sumOf 1 4 7 10 0 0  :: Integer
    22
    *Main> sumOf 1 4 7 10 2 5 8 22 :: Integer
    59
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function which takes in a list and outputs True if all
I need some help in getting this right, problem Write a function which takes
I need to have a function which takes a 2D array and generates random
I need a function which takes a line (known by its coordinates) and return
I need to have a sql function which takes a valid SQL Server date
I have a function which takes as an argument an XDocument object. I need
For a homework assignment, I need to implement a function which takes a char
I've written a function which takes a <select> ID, and replaces all the options
I have a seat function which takes a number of values, 3 of which
I have a templated math function which takes two values, does some math to

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.