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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:24:20+00:00 2026-06-04T14:24:20+00:00

Possible Duplicate: Why is such a function definition not allowed in haskell? I’m a

  • 0

Possible Duplicate:
Why is such a function definition not allowed in haskell?

I’m a newcomer to the world of Haskell, migrating over from Lisp. I’m trying to adjust to Haskell’s fundamentally different worldview, and one of the many things that I find new and exciting is the type system. Being a Lisper, I thought I would try to implement in Haskell a function which is very important in the Lisp world: apply. For those who don’t know, apply takes a function and a list of arguments, and invokes the function on those arguments. In Scheme, (apply + '(1 2 3)) is the same as invoking (+ 1 2 3), and returns 6.

My Haskell code looks something like this:

apply x [] = x
apply f (x:xs) = apply (f x) xs

But Haskell complains:

ERROR line 2 - Type error in function binding
*** Term           : apply
*** Type           : (b -> a) -> [b] -> a
*** Does not match : a -> [b] -> a
*** Because        : unification would give infinite type

And I think that I understand why. Apply’s type needs to be different depending on the length of the list it is given. Given a list of, say, 3 items, apply’s type would need to be: (a -> a -> a -> b) -> [a] -> b, but given a list of 6 items, apply’s type would need to be: (a -> a -> a -> a -> a -> a -> b) -> [a] -> b.

I tried this horrendous work-around:

data FnOrDat a b = Dat b | Fn (a -> FnOrDat a b)

apply :: (FnOrDat a b) -> [a] -> (FnOrDat a b)
apply x [] = x
apply (Fn f) (x:xs) = apply (f x) xs
apply (Dat _) _ = error "Cannot apply something which is not a function!"

add a = Fn (\b -> Dat (a + b))

main = putStrLn $ show $ x where Dat x = apply (Fn add) [5,1]

This works, but it hardly counts as an apply function, because I can’t pass apply a normal function, I have to use one that has been written specifically to use my (awkward) FnOrDat abstraction. If I wanted to write a function that added four numbers, I would need to write

add4 a = Fn (\b -> Fn (\c -> Fn (\d -> Dat (a + b + c + d))))

Ew.

So – am I missing something, or is asking for a general-purpose apply basically like asking for a function that can manipulate an arbitrary-length tuple? Does apply even make sense in the statically-typed worldview of Haskell?

  • 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-04T14:24:21+00:00Added an answer on June 4, 2026 at 2:24 pm

    apply isn’t very useful in Haskell, since you can’t give a type to the function. As you see in your FnOrDat, you’re essentially embedding a Lisp language into Haskell as an EDSL to force something through.

    asking for a general-purpose apply basically like asking for a function that can manipulate an arbitrary-length tuple?

    Exactly. You can come up with type class instances for certain useful combinations of types, but there’s just not really a need or use for a general variadic apply.


    As a side note, you should consider upgrading to GHC and the Haskell Platform, instead of the obsolete Hugs system, since you’re missing out on most of libraries, tools and language features developed in the last 10 years.

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

Sidebar

Related Questions

Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Possible Duplicate: iOS - Detecting whether or not device support phone calls? I'm writing
Possible Duplicate: Why does second function declaration win even though I return before it?
Possible Duplicate: Convention question: When do you use a Getter/Setter function rather than using
Possible Duplicate: C++ Timer function to provide time in nano seconds High resolution timer
Possible Duplicate: Difference between try-catch syntax for function Few days ago I was reading
Possible Duplicate: C++ member-function pointer How to invoke pointer to member function when it's
Possible Duplicate: Converting words to numbers in PHP Is there a php function to
Possible Duplicate: C#: Order of function evaluation (vs C) Code snippet: i += ++i;

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.