input: Write a function to implement the binary search algorithm in Python. The function should take a sorted list of numbers `nums` and a target value `val`. It should return the index of the target value if it exists in the list, otherwise return -1. If the list is empty, return -1.
  assert binary_search([3, 4, 5, 6, 7, 8, 9], 8)==5
  assert binary_search([3, 4, 5, 6, 7, 8, 9], 10)==-1
  assert binary_search([], 5)==-1
output: def binary_search(nums, val):
      if not nums:
          return -1
      left, right = 0, len(nums) - 1
      while left <= right:
          mid = (left + right) // 2
          if nums[mid] == val:
              return mid
          elif nums[mid] < val:
              left = mid + 1
          else:
              right = mid - 1
      return -1