Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Cond and Case?

In the Elixir programming language, there are two similar constructs cond and case. Both resemble the switch or select statements from other langages

both cond and case are described on this page

like image 645
Lyndon White Avatar asked Jan 12 '14 13:01

Lyndon White


People also ask

What is condition case?

The condition-case special form causes the Lisp interpreter to evaluate the code in bodyform . If no error occurs, the special form returns the code's value and produces the side-effects, if any. In short, the bodyform part of a condition-case expression determines what should happen when everything works correctly.

What is Elixir case?

Case statement can be considered as a replacement for the switch statement in imperative languages. Case takes a variable/literal and applies pattern matching to it with different cases. If any case matches, Elixir executes the code associated with that case and exits the case statement.

How do you use if else in Elixir?

If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. If Boolean expression evaluates to false, then the code after the else keyword of the given if statement will be executed.

What is pattern matching in Elixir?

Pattern matching allows developers to easily destructure data types such as tuples and lists. As we will see in the following chapters, it is one of the foundations of recursion in Elixir and applies to other types as well, like maps and binaries.


2 Answers

Let me put if to the club too. You use if with one condition and a possible else, that's it. You use the cond statement when you have more than one condition and an if statement isn't enough, by the end, the case statement is used when you want to pattern match some data.

Let's explain by examples: suppose you want to eat apple if today is raining or rice if not, then you could use:

if weather == :raining do   IO.puts "I'm eating apple" else   IO.puts "I'm eating rice" end 

This is a limited world, so you want to expand your options and because of that you will eat different things on some conditions, so the cond statement is for that, like this:

cond do   weather == :raining and not is_weekend ->     IO.puts "I'm eating apple"   weather == :raining and is_weekend ->     IO.puts "I'm will eat 2 apples!"   weather == :sunny ->     IO.puts "I'm happy!"   weather != :raining and is_sunday ->     IO.puts "I'm eating rice"   true ->     IO.puts "I don't know what I'll eat" end 

The last true should be there otherwise it'll raise an exception.

Well so what about case? It is used to pattern match something. Let's suppose you receive the information about the weather and the day of week as a message in a tuple and you depend on that to take a decision, you could write your intentions as:

case { weather, weekday } do   { :raining, :weekend } ->     IO.puts "I'm will eat 2 apples!"    { :raining, _ } ->     IO.puts "I'm eating apple"    { :sunny, _ } ->     IO.puts "I'm happy!"    { _, :sunday } ->     IO.puts "I'm eating rice"    { _, _ } ->     IO.puts "I don't know what I'll eat" end 

So the case brings to you the pattern-matching approach to the data, that you don't have with if or cond.

like image 193
Guedes Avatar answered Sep 18 '22 12:09

Guedes


My simple answer is:

  • cond receives no arguments, and it allows you to use a different condition in each branch.
  • case receives an argument, and every branch is pattern-matched against the argument.
like image 43
asymmetric Avatar answered Sep 20 '22 12:09

asymmetric