Pattern Matching

The technique of using pattern matching to to implement logic in your code is a very standard technique in functional programming. In some sense it is a very natural way to have different code to handle different patterns. There are quite a few different things that you can use as patterns:

  • Values
  • Types
  • Guards/Expression
  • special patterns related to data structures (like [],(x::xs) for lists)

Right now let us use pattern matching on values.

import Graphics.Element exposing (..)
import List exposing (..)
--pattern matching and Recursion
facP : Int -> Int
facP n =
  case n of
    0 -> 0
    1 -> 1
    _ -> n * facP (n-1)

main = flow down [
    print "factorial(5) using recursion is : " (facP 5)]

--a helper function to make display easier
print message value = show (message ++ (toString value))

As you can see from the code above there are three patterns that are tested against the current value of n either n is 0, 1 or any other value.

Pattern Matching on Lists

Combining recursion with pattern matching on lists gives us the technique to write many interesting functions. Here we have shown three functions (length, reverse, head) for you to review.

import Graphics.Element exposing (..)
import List exposing (..)

length : List a -> number
length xs = case xs of    
    []    -> 0
    x::xs -> 1 + length xs

reverse : List a -> List a
reverse xs = case xs of
  [] -> []
  x::xs -> (reverse xs)++[x]

head : List number -> number
head xs = case xs of
  [] -> 0
  x::xs -> x

main =
  flow down [print "The length the list [2..10] is : " (length [1..10])
  ,print "The reversed elements of the list [2..5] is : " (reverse [1..5])
  ,print "The head element of the list [2..5] is : " (head [1..5])
  ,print "The first 2 elements of the list [1..5] is : " (take 2 [1..5])
  ]

print message value = show (message ++ (toString value))

Exercises

  1. Implement the function take n ys that returns the first nn elements of the list of ysys.

In case you want to just look at the solution here it is.

take:number -> List a -> List a
take m ys =
  case (m,ys) of
   (0,_) ->  []
   (_, [])->  []
   (n,x::xs) ->  [x] ++ take (n-1) xs

results matching ""

    No results matching ""