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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:20:09+00:00 2026-06-18T02:20:09+00:00

I am attending a Coursera class, and I am trying to do my homeworks.

  • 0

I am attending a Coursera class, and I am trying to do my homeworks.

We have to write an SML program that takes a list of cards (caracterised by their suit and rank) and returns true if they all have the same color and false otherwise.

Here is my code (i can’t figure why its false, but i am quit a noob in programming):

datatype suit = Clubs | Diamonds | Hearts | Spades
datatype rank = Jack | Queen | King | Ace | Num of int 
type card = suit * rank

datatype color = Red | Black
datatype move = Discard of card | Draw 


fun card_color (c) = case c of
(Hearts,_) => Red
|(Diamonds,_)  => Red
|(_,_) => Black


fun all_same_color (cs) = case cs of
    [] => false
    |x::[] => true 
|x::y::[] =>  if card_color (x) = card_color (y) then true
|x::y::xs => if card_color(x)=card_color(y) then all_same_color(xs)
                        else false
  • 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-18T02:20:10+00:00Added an answer on June 18, 2026 at 2:20 am

    You cannot have an if ... then ... without an else ... case.

    You attempt to do this here:

    |x::y::[] =>  if card_color (x) = card_color (y) then true
    

    Remember, if ... then ... else ... is an expression, and thus needs a value whether or not the condition is true or false.

    In addition, if you get an if-then-else, where either the then or else parts are true or false directly, you can write it more succinctly. For instance,

    if card_color(x)=card_color(y) then all_same_color(xs) else false
    

    is the same as saying

    card_color x = card_color y andalso all_same_color xs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class that stores information about people attending a conference: class
I'm trying to get a list of facebook events I'm attending. I have these
I'm trying to generate a list of events that a user is attending. All
I'm trying to check whether a user is attending an event within a list
I have been trying to get list of event invitations sent to user. I
I working on a table that keeps record of student attending course. The data
I have been attending a lecture on XML where it was written ISO-8859-1 is
I want user to remove group that user attending groups by removing groupings. So,
I am trying to make a form to ask people if they are attending
Trying to implement an Attending button. It's simple (or so I thought). A user

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.