-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Problem:
I don't understand this code for set default function in Elixir
defmodule Sample.Default.Params do
def first(list, val \\ nil)
def first([head | _], _), do: head
def first([], val), do: val
end
# defmodule Sample.Default.Params do
# def first(list, val \\ nil), do: val # Why we remove this one?
# def first([head | _], _), do: head
# def first([], val), do: val
# end
# The warnning message
# warning: this clause for first/2 cannot match because a previous clause at line 2 always matches
# sample_default_params.exs:3
# warning: this clause for first/2 cannot match because a previous clause at line 2 always matches
# sample_default_params.exs:4
# Test cases
# Sample.Enum.first([1,2,3]) # => 1
# Sample.Enum.first([], 1) # => 1
# Sample.Enum.first([]) # => nil
# Sample.Enum.first([1,2,3], 0) # => 1
Solution:
Lesson:
Metadata
Metadata
Assignees
Labels
No labels