DEV Community

Discussion on: No duplicates challenge in Elm

1hko profile image
1hko • Edited on

It's all about mastering inductive reasoning.

someFunc n = 
    if n == 0 then
        -- do something when n IS zero (base case)
        -- do something where n is (by inductive reasoning) NOT zero


someFunc list =
    if List.isEmpty list then
        -- handle the empty list (base case)
        -- the list (by inductive reasoning) has at least one element

Pattern matching streamlines this for us by allowing us to express the base case and any induction steps using a nice syntax

someFunc n =
    case n of
        0 ->  -- base case
        _ -> -- induction step


someFunc list =
    case list of
        [] -> -- base case
        first :: more -> -- induction step