Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pattern match head in a list in function?

I have a list of mix of integer and atom. I want to match the head of the list with atom otherwise integer.

lst = [1,2,3,4,5,6, :eoe, 7,8,9,10,11,12. :eoe]

I initially tried this way:


defmodule Test do
  def test(lst) do
    helper(lst, 0, 0, 1)
  end

  def helper([], _p, total, e) do
    IO.puts "#{e} #{t}"
  end

  def helper([:eoe , t], _p, total, e) do   # <--- This function never called even though head is at some point :eoe
    IO.puts "#{e} #{total}"

    helper(t, "", 0, elf + 1)
  end

  def helper([h | t], p, total, e) do
    h
    |> is_atom()
    |> IO.inspect()

    helper(t, h, total + h, e)

  end
end

then added guards to explicitly narrow down pattern matching

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

...

def helper([:eoe = head , t], _p, total, e) when is_atom(head) do
...

def helper([h | t], p, total, e) when is_integer(h) do
...

def helper([:eoe = h , t], _p, total, e) when is_atom(h) do this function does’t get called. It always matches def helper([h | t], p, total, e) when is_integer(h) do this one. I even placed the former one before latter one. I would expect it be matched against :eoe

>Solution :

To match the head one should use the [h | t] notation. [h, t] would match the list of two elements.

- def helper([:eoe = h, t]
+ def helper([:eoe = h | t]

Also, when is_atom(h) guard is redundant, once you pattern-match directly on the atom. That said, any of the following would do.

def helper([:eoe | t], _p, total, e) do
def helper([h | t], _p, total, e) when h == :eoe do
def helper([h | t], _p, total, e) when h in [:eoe] do
def helper([h | t], _p, total, e) when is_atom(h) do # match any atom
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading