How to stop a for in Elixir?

Asked

Viewed 75 times

-1

I’m making a loop that will check my list. The problem is that the loop doesn’t stop when I get where I want, then it ends up showing everything that has been checked.

defmodule Tester do 
    def a(list,p) do 
        for x <- list  do 
            if(x == p) do 
                IO.puts "Number #{p} belongs to the list"
            else 
                IO.puts "Number #{p} does not belong to the list"
            end
        end
    end
end

This is the output when calling the function with the list [1,2,3] and a number 3.

The number 3 does not belong to the list
The number 3 does not belong to the list
The number 3 belongs to the list
  • Already tried using the command break or return?

  • 1

    "not when I get where I want" and where is what you want? Where should be stopping?

  • If the idea is just to check whether p is on the list, can do p in list. That expression will return true if you are on the list, or false otherwise.

  • @Danizavtz Turn and break, no elixir as far as I know.

  • @Luizf6843 our, what legal language!

  • @Woss I explained a little wrong, I’ll redo it. What I want, is for him to check if he has the number inside the list. Right. but instead of returning only "the number is in the list", it returns everything that has been checked, ex ["the number is not in the list',"the number is not in the list", "the number is in the list" ]

  • I managed to fix the "problem", with a help. Thanks there who answered me! :)

Show 2 more comments

2 answers

0

Use the Membership Operator, or membership operator instead of for, which is a list understanding in Elixir.

def a(list, p) do
  if p in list do
    IO.puts "Number #{p} belongs to the list"
  else
    IO.puts "Number #{p} does not belong to the list"
  end
end

I wouldn’t advise it, though, if you really want to stop the for, what you can generate an exception.

try do
  for x <- [1, 2, 3, 4, 0, 1],
    do: if x == 0, do: throw(:break), else: x
catch
  :break -> :break
end

0

What you want to do is what we call Return Early that you can read here. In the functional paradigm we do not usually use conventional repeating structures of object-oriented or structural languages, to make this return true or false if a number is in the list we would use the Enum.any?/2.

defmodule Tester do
  def evaluate_list(list, value) do
    # Enum.any? recebe dois parâmetros, uma lista e uma função
    # Essa função deverá retornar true ou false, indicando que
    # Aquele ítem da lista foi encontrado
    list
    |> Enum.any?(fn item -> item == value end)
  end

  def main do
    list = [1, 2, 3]
    value = 2

    case evaluate_list(list, value) do
      true -> IO.puts("Number #{value} belongs to the list")
      _ -> IO.puts("Number #{value} does not belong to the list")
    end
  end
end

This function could be even shorter using the function Enum.member?/2.

def evaluate_list(list, value), do: Enum.member?(list, value)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.