Can you return from inside a switch?

new topic     » topic index » view thread      » older message » newer message

binary-search in Exercism question.

include std/math.e  
 
public function my_find(sequence values, object value) 
  if length(values) = 0 or value < values[1] or value > values[$] then 
    return -1 
  end if 
  integer first = 1 
  integer last = length(values) 
  while first <= last do 
    integer middle = ceil((first + last) / 2) 
    integer comparison = compare(values[middle], value) 
    switch comparison do 
      case -1 then 
        last = middle + 1 
      case 0 then 
        return middle 
      case 1 then 
        last = middle - 1 
    end switch 
    --if comparison = 0 then 
    --  return middle 
    --elsif comparison = 1 then -- lower slice 
    --  last = middle - 1 
    --else -- upper slice 
    --  first = middle + 1 
    --end if 
  end while 
  return -1 
end function 

If I do the if, it works. If I do the switch, it fails. Is that because I can't do a return from inside a switch?

The eutest test that fails is

test_equal("finds a value at the end of a sequence", 
            7, 
            my_find({1, 3, 4, 6, 8, 9, 11}, 11)) 

-Bruce

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu