![]() |
|
Navigation |
Synopsis Let the current alternative of a pattern match fail.
Syntax
fail;
Description A
fail statement is only allowed in statements that are controlled by the outcome of a pattern match:
fail statement is associated with the innermost pattern match by which it is controlled.
When fail is executed:
Examples Here is an example taken from Recipes:Bubble. It uses a
fail for the case that no unsorted element can be found in the list of numbers.
As a result, the whole case fails and the default case is used.
rascal>import IO; ok rascal>public list[int] sort(list[int] Numbers){ >>>>>>> switch(Numbers){ >>>>>>> case [list[int] Nums1, int P, int Q, list[int] Nums2]: >>>>>>> if(P > Q){ >>>>>>> return sort(Nums1 + [Q, P] + Nums2); >>>>>>> } else { >>>>>>> fail; >>>>>>> } >>>>>>> default: return Numbers; >>>>>>> } >>>>>>>} list[int] (list[int]): list[int] sort(list[int]); rascal>sort([10, 1, 5, 3]); list[int]: [10,1,5,3] ![]() |