O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Contains

To make this LinkedList a little more interesting, we will develop a contains method similar to the one that we developed for the BST:

static func contains(_ key: Element, list: LinkedList<Element>) -> Bool {      switch list {      case .end:          return false      case .node(let data, let next):          if key == data {              return true          } else {              return contains(key, list: next)          }      } } 

This method recursively checks for a specific element in LinkedList and returns true if it finds the element:

print(LinkedList.contains(1, list: functionalLinkedList)) 

The result of this expression is going to be true.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required