Headline

Solve the search problem by iterating over the input list

Description

Consider the search problem, i.e., the problem of determining whether a given value occurs in a given list. This problem is an algorithmic problem, i.e., it can be solved by an algorithm, e.g., by linear search.

Semi-formally, linear search can be described by an algorithm as follows:

  • Given is a list l and a value v of the element type of l.
  • Perform the following steps to search v in l:
    • Initialize an index variable i to 0 (to refer to the first element of l, if any).
    • Repeat the following steps until a result is returned.
      • Return False, if i equals the length of l.
      • Return True, if l stores v at the index i.
      • Increment i.
Please note that this formulation also expresses that the element type must admit comparison for equality.

Illustration

Linear search in Haskell

-- Polymorphic linear search
search :: Eq a => [a] -> a -> Bool
search [] _ = False
search (x:xs) y = x==y || search xs y

The type of the search function is polymorphic in that admits arbitrary element types for as long as equality ("Eq") is supported for the type.

The implemented search function can be applied as follows:

-- Illustrate linear search
main = do
  let input = [2,4,3,1,4]
  print $ search input 1 -- True
  print $ search input 5 -- False

Citation

(http://en.wikipedia.org/wiki/Linear_search, 21 April 2013 with Knuth's "The Art of Computer Programming" credited for citation)

In computer science, linear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.