user124659
user124659

Reputation: 15

Replacing a list element by pattern matching

This function takes list, index number, and a replacement string. It goes through the list and replaces the element at the given index with the replacement string. What I'm not sure of is what case the pattern in the last line is trying to catch.

-- e.g., listSet1 ["a","x","k"] 2 "d" = ["a", "d","k"]
listSet1 (x:xs) 1 y = y:xs
listSet1 (x:xs) n y = x : listSet1 xs (n-1) y
listSet1 xs _ _ = xs

Upvotes: 0

Views: 558

Answers (2)

dfeuer
dfeuer

Reputation: 48611

You can and should add one of the following lines to the top of every Haskell file: {-# OPTIONS_GHC -Wall #-} or {-# OPTIONS_GHC -fwarn-incomplete-patterns #-}. These go above the module Foo where line if you have one of those. With one of these in place, commenting out that last pattern will give you a warning saying exactly what is not matched.

Upvotes: 1

JB.
JB.

Reputation: 42134

The last pattern handles the end of the list. You won't notice it unless you try and replace an index not present in the list (try it!)

Upvotes: 2

Related Questions