case
case(g_keyForml_clause1[l_clause2... ] ) =>g_result/nil
Description
Branches to one of the clauses depending on the value of the given expression. case() evaluates g_keyForm and matches the resulting value sequentially against the clauses until it finds a match. Once a match is found it stops searching the clauses, evaluates the forms in the matching clause, and returns the resulting value. This is a syntax function.
Each l_clause is in turn a list of the form (g_keys g_expr1 [g_expr2 ...]) in which the first element, that is g_keys, is either an atom (that is, a scalar) of any data type or a list of keys (to be compared with the given expression). When using a list of keys, specify it as a list of one or more lists to distinguish it from a list of scalar keys. If any of the keys matches the value from g_keyForm, that clause is selected. Keys are always treated as constants and are never evaluated.
The symbol t has special meaning as a key in that it matches anything. It acts as a catch-all and should be handled last to serve as a default case when no other match is found. To match the value t, use a list of t as the key.
Arguments
Value Returned
|
The value of the last expression evaluated in the matched clause, or |
|
Examples
nameofmonth = "February"
month = case( nameofmonth
("January" 1)
("February" 2)
(t 'Other))
=> 2
listofnums = list(2 4 6)
case( listofnums
(( (1 2 3) ) 'onetwothree)
(( (1 3 5)
(7 9 11) ) 'odd)
(( (2 4 6)
(8 10 12) ) 'even)
(t 'unknown))
=> even
case( myBool
(nil 'never) ; this will never match, it is a list of no keys
(( nil ) nil) ; matches nil
(( t ) t) ; matches t
(t (error "Expected t or nil"))
)
shapeType="line"
rectCount=0
labelOrLineCount=0
miscount=0
case( shapeType
("rect" ++rectCount println( "Shape is a rectangle" ))
(( "label" "line" ) ++labelOrLineCount println( "Shape is a line or a label" ))
(t ++miscount println( "Shape is miscellaneous" ))
) ; case
=> Shape is a line or a label
procedure(migrateShape(shape "d")
case(list(shape->layerName shape->purpose)
((("POLY" "test1"))
shape->layerName = "CPO"
shape->purpose = "drawing"
) ((("Oxide" "drawing") ("abcd" "efgh")) println(shape->layerName) ) ((("M1" "net")) shape->purpose = "label" ) ((("M2" "net")) shape->purpose = "label" ) ((("M3" "net")) shape->purpose = "label" ) ((("M4" "net")) shape->purpose = "label"
)
)
) ;
Related Topics
Return to top