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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:18:57+00:00 2026-05-26T05:18:57+00:00

The following code is an attempt to write a variadic function that acts like

  • 0

The following code is an attempt to write a variadic function that acts like this:

  • bind_variadic mx f = mx >>= f
  • bind_variadic mx my f = do { x <- mx; y <- my; f x y }

I can write it if one expresses the “rest of binding” as a variable k, but in order to write a typeclass I need to write one function in terms of the other. To be precise, I want to express l1 in terms of l0, l2 in terms of l1, etc.

import Prelude hiding ((>>=), (>>), Monad, return)

-- override the default monad so we don't get confusing
-- instances like "Monad (->)".
class Monad m where
  (>>=) :: m a -> (a -> m b) -> m b
  (>>) :: m a -> m b -> m b
  return :: a -> m a
  fail :: String -> m a

h :: Monad m => m a -> (t -> m b) -> (a -> t) -> m b
h mx k f = mx >>= \x -> k (f x)

l0 = h (return 3) id (\x -> return x)
l1 = h (return 3) (h (return 4) id) (\x y -> return x)
l2 = h (return 3) (h (return 4) (h (return 5) id)) (\x y z -> return x)

Perhaps the solution involves another continuation?

edit

here’s an idea that requires an additional join…

-- if not using Control.Monad, use this
join :: Monad  =>  ( α) ->  α
join mx = mx >>= id

-- idea: get side effects of evaluating first arguments first
h' mz k f = k f >>= \f' -> mz >>= (return . f')

l1' = h' (return 3) return
unary = join (l1' (\x -> return x))
l2' = h' (return 4) l1'
binary = join (l2' (\x y -> return x))
l3' = h' (return 5) l2'
ternary = join (l3' (\x y z -> return x))
  • 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-26T05:18:58+00:00Added an answer on May 26, 2026 at 5:18 am

    If you want to express this:

    ap_variadic mx f = mx >>= f
    ap_variadic mx my f = do { x <- mx; y <- my; f x y }
    

    I would use Control.Applicative instead. Then:

    join (f <$> mx)
    join (f <$> mx <*> my)
    join (f <$> mx <*> my <*> mz)
    

    I think this is better (simpler, more maintainable) than any polyvariadic solution would be.

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

Sidebar

Related Questions

I am using the following code to attempt to read a large file (280Mb)
I am using the following code to attempt to redirect a dynamic URL to
I'm using the following code in an attempt to show a dialog with a
consider the following code, which represents an attempt to implement partial matching. the intended
This is my first ever attempt at writing a recursive function in c. The
If I attempt to write two overloads of a method, one accepting an Expression<Func<T>>
i need a quick hint regarding the following exercise question: Write a program that
Following is my attempt to write BK-Tree , for 150000 word file it takes
Currently I have the following code for a project that represents some probability trees
The following code snippet attempts to create a Tib DaemonManager connecting to a particular

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.