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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:21:01+00:00 2026-05-26T07:21:01+00:00

I’m used to being able to define optional arguments like so in Python: def

  • 0

I’m used to being able to define optional arguments like so in Python:

def product(a, b=2):
    return a * b

Haskell doesn’t have default arguments, but I was able to get something similar by using a Maybe:

product a (Just b) = a * b
product a Nothing = a * 2

This becomes cumbersome very quickly if you have more than multiple parameters though. For example, what if I want to do something like this:

def multiProduct (a, b=10, c=20, d=30):
    return a * b * c * d

I would have to have eight definitions of multiProduct to account for all cases.

Instead, I decided to go with this:

multiProduct req1 opt1 opt2 opt3 = req1 * opt1' * opt2' * opt3'
    where opt1' = if isJust opt1 then (fromJust opt1) else 10
    where opt2' = if isJust opt2 then (fromJust opt2) else 20
    where opt3' = if isJust opt3 then (fromJust opt3) else 30

That looks very inelegant to me. Is there an idiomatic way to do this in Haskell that is cleaner?

  • 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-26T07:21:02+00:00Added an answer on May 26, 2026 at 7:21 am

    Here’s yet another way to do optional arguments in Haskell:

    {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
    module Optional where
    
    class Optional1 a b r where 
      opt1 :: (a -> b) -> a -> r
    
    instance Optional1 a b b where
      opt1 = id
    
    instance Optional1 a b (a -> b) where
      opt1 = const
    
    class Optional2 a b c r where 
      opt2 :: (a -> b -> c) -> a -> b -> r
    
    instance Optional2 a b c c where
      opt2 = id
    
    instance (Optional1 b c r) => Optional2 a b c (a -> r) where
      opt2 f _ b = \a -> opt1 (f a) b
    
    {- Optional3, Optional4, etc defined similarly -}
    

    Then

    {-# LANGUAGE FlexibleContexts #-}
    module Main where
    import Optional
    
    foo :: (Optional2 Int Char String r) => r
    foo = opt2 replicate 3 'f'
    
    _5 :: Int
    _5 = 5
    
    main = do
      putStrLn $ foo        -- prints "fff"
      putStrLn $ foo _5     -- prints "fffff"
      putStrLn $ foo _5 'y' -- prints "yyyyy"
    

    Update: Whoops, I got accepted. I honestly think that luqui’s answer is the best one here:

    • the type is clear, and easy to read, even for beginners
    • same for type errors
    • GHC doesn’t need hints to do type inference with it (try opt2 replicate 3 'f' in ghci to see what I mean)
    • the optional arguments are order-independent
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.