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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:06:48+00:00 2026-05-27T10:06:48+00:00

I’ve created a function in which I can use (as far as I know)

  • 0

I’ve created a function in which I can use (as far as I know) case expressions or guards.

foo a b c = case a of 1 -> [...]
                      2 -> [...]
                      3 -> [...] 
                      [...]
                      otherwise -> error "..."

or

foo a b c | a == 1 = [...]
          | a == 2 = [...]
          | a == 3 = [...] 
          | [...]
          | otherwise = error "..."

So, the question is: which of those 2 (case or guards) are “better” coding? Are both basically the same?

  • 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-27T10:06:49+00:00Added an answer on May 27, 2026 at 10:06 am

    The first one is considered better style, for 2 reasons.

    First of all: Many people would say that it looks better, since you don’t have to type out all of the ==. This is a very subjective reason, of course. Also, you would normally not even introduce a new case statement, but just match the arguments in the function argument list like so:

    foo 1 b c = ... -- etc
    ...
    foo _ b c = ... -- for the "otherwise" part
    

    This makes the code even more compact and readable, which many people like.

    Secondly, there actually is a semantic difference. Imagine that you have a data type like this:

    data Cake = Apple | Cheese | Cream
    

    If you use the first method, you match against the constructors in the case..of expression:

    case a of
      Apple -> "fruit"
      _     -> "not fruit"
    

    However, if you try to do a guarded expression of some sort, like this:

    | a == Apple = "fruit"
    | otherwise  = "not fruit"
    

    … it won’t actually work, because the Cake type doesn’t have an Eq instance, so you can’t use == to compare two values. Introducing an Eq instance (with deriving (Eq) after the data definition) is not always wanted, so not having to do it in this case might be significant.

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

Sidebar

Related Questions

I want to create a simple menu function which can call it example get_menu()
I'm looking for a PHP library/function/class which can create Identicon s.
I have created a JS function which executes fine when it's included an the
I created webservice which contain Add function as 'webmethod' when we want to deploy
I created a sample webservice which contain default 'HelloWorld' function as 'webmethod' when tried
I have created a test (not real) encryption function which takes a byte[] and
I have encountered the following problem which proved to me that I know far
I want to create a function which grabs every users latest tweet from a
I'm trying to create a function which takes an array as an argument, adds
I am trying to create a function which will give me alphabet position when

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.