Knowledge (XXG)

Guard (computer science)

Source 📝

36: 837: 892:
had a form of conditional expression without an obligatory fall-through case, thus separating guard from the concept of choosing either-or. In the case of ISWIM, if none of the alternatives could be used, the value was to be
907:(1976), was one of the first programming languages to use the term "guard". Its function definitions could have several clauses, and the one to apply was chosen based on the guards that followed each clause: 849:
If there are several parallel guards, they are normally tried in a top-to-bottom order, and the branch of the first to pass is chosen. Guards in a list of cases are typically parallel.
758: 985:
In 1996, Dyalog APL adopted an alternative pure functional style in which the guard is the only control structure. This example, in APL, computes the parity of the input number:
1318: 1040: 872:
in 1963, has a guard on first sub-expression, and another sub-expression to use in case the first one cannot be used. Some common ways to write this:
856:
the guards are in series, and if any of them fails, the list element is not produced. This would be the same as combining the separate guards with
1043:
in April 1997 and was used in the implementation of the proposal. The feature provides the ability to use patterns in the guards of a pattern.
1035:
in the context of a guard. In effect, a match of the pattern is taken to mean pass. This meaning was introduced in a proposal for Haskell by
57: 1309: 1338: 370:# Equivalent function with a guard clause. Note that most of the code is less indented, which makes it easier to read and reason about 79: 878:
If the second sub-expression can be a further simple conditional expression, we can give more alternatives to try before the last
1333: 639: 667: 290: 671: 663: 651: 141: 647: 286:
technique to improve code. In general, less nesting is good, as it simplifies the code and reduces cognitive burden.
50: 44: 643: 635: 904: 900: 832:{\displaystyle f(x)=\left\{{\begin{matrix}1&{\mbox{if }}x>0\\0&{\mbox{otherwise}}\end{matrix}}\right.} 695: 61: 1144: 869: 683: 442: 268: 1139: 105: 113: 93: 1160: 857: 1150: 1127: 1036: 853: 101: 1238: 694:
with the possibility to skip a pattern even if the structure matches. Boolean expressions in
1171: 1032: 691: 109: 1256: 1155: 160: 687: 1327: 1165: 710:
In the following Haskell example, the guards occur between each pair of "|" and "=":
145: 129: 17: 1290: 675: 283: 152: 1130:, the guards are in series, and if any of them fails the branch is not taken. 860:, except that there can be other list comprehension clauses among the guards. 264: 260: 156: 1299: 1194: 979: 975: 1213: 271:, removing one level of nesting and resulting in flatter code: replacing 1124:
in case the lookups of the variables from the environment produce values
655: 1286: 974:
Use of guard clauses, and the term "guard clause", dates at least to
698:
usually also fit this definition of a guard although they are called
889: 659: 841:
In this case the guards are in the "if" and "otherwise" clauses.
1147:, a programming language based on non-deterministic conditionals 1122:
This would read: "Clunky for an environment and two variables,
112:
is to continue in the branch in question. Regardless of which
29: 1315:
The Glorious Glasgow Haskell Compilation System User's Guide
826: 1276: 753:
This is similar to the respective mathematical notation:
816: 791: 782: 761: 1168:, an object to represent the end of a data structure 868:
A simple conditional expression, already present in
897:, which was defined to never compute into a value. 1239:"Some History of Functional Programming Languages" 831: 875:(x>0) -> 1/x; 0 x>0 ? 1/x : 0 144:about to be processed is not null, which avoids 1027:In addition to a guard attached to a pattern, 1281:Free On-Line Dictionary of Computing - FOLDOC 151:Other uses include using a Boolean field for 8: 885:(x>0) -> 1/x; (x<0) -> -1/x; 0 634:The term is used with specific meaning in 543:// Equivalent function with a guard clause 246:// Rest of the method code follows here... 815: 790: 781: 760: 80:Learn how and when to remove this message 682:. Guards are the fundamental concept in 267:, and is a commonly used deviation from 43:This article includes a list of general 1183: 1126:, is the sum of the values. ..." As in 132:used to avoid errors during execution. 1189: 1187: 978:practice in the 1990s, as codified by 1310:2.3.5 Putting Constraints on Patterns 140:A typical example is checking that a 7: 450:// This function has no guard clause 1117:-- ...other equations for clunky... 298:# This function has no guard clause 104:that must evaluate to true if the 49:it lacks sufficient corresponding 25: 1199:Smalltalk Best Practice Patterns, 1257:"Direct Functions in Dyalog APL" 1046:An example in extended Haskell: 690:. Guards can be used to augment 34: 903:, a "miniaturized version" of 771: 765: 255:Flatter code with less nesting 1: 282:Using guard clauses can be a 237:"Username is null." 441:Another example, written in 27:Concept in computer science 1355: 1339:Formal methods terminology 674:programming languages. In 277:if not guard: return; ... 155:(so subsequent calls are 1197:(1997). "Guard Clause". 1145:Guarded Command Language 1048: 1031:can refer to the use of 987: 909: 712: 684:Guarded Command Language 447: 295: 165: 128:is a check of integrity 1317:, Version 6.4, section 670:from version 3.10, and 64:more precise citations. 1334:Conditional constructs 1283:, Denis Howe (editor). 833: 696:conditional statements 269:structured programming 259:The guard provides an 1319:7.3.2. Pattern guards 1306:The Mathematica Book, 1296:The Haskell 98 Report 834: 219:ArgumentNullException 1041:A new view of guards 852:However, in Haskell 759: 678:, guards are called 114:programming language 94:computer programming 1214:"guard & defer" 1201:. pp. 178–179. 1161:Logical conditional 1128:list comprehensions 854:list comprehensions 1151:Guarded suspension 1037:Simon Peyton Jones 829: 824: 820: 795: 102:Boolean expression 819: 794: 90: 89: 82: 18:Guard (computing) 16:(Redirected from 1346: 1264: 1263: 1261: 1252: 1246: 1245: 1243: 1234: 1228: 1227: 1225: 1224: 1209: 1203: 1202: 1191: 1172:Switch statement 1118: 1115: 1112: 1109: 1106: 1103: 1100: 1097: 1094: 1091: 1088: 1085: 1082: 1079: 1076: 1073: 1070: 1067: 1064: 1061: 1058: 1055: 1052: 1033:pattern matching 1018: 1015: 1012: 1009: 1006: 1003: 1000: 997: 994: 991: 970: 967: 964: 961: 958: 955: 952: 949: 946: 943: 940: 937: 934: 931: 928: 925: 922: 919: 916: 913: 838: 836: 835: 830: 828: 825: 821: 817: 796: 792: 749: 746: 743: 740: 737: 734: 731: 728: 725: 722: 719: 716: 692:pattern matching 686:, a language in 625: 622: 619: 616: 613: 610: 607: 604: 601: 598: 595: 592: 589: 586: 583: 580: 577: 574: 571: 568: 565: 562: 559: 556: 553: 550: 547: 544: 541: 538: 535: 532: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 496: 493: 490: 487: 484: 481: 478: 475: 472: 469: 466: 463: 460: 457: 454: 451: 437: 434: 431: 428: 425: 422: 419: 416: 413: 410: 407: 404: 401: 398: 395: 392: 389: 386: 383: 380: 377: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 308: 305: 302: 299: 289:For example, in 278: 274: 273:if guard { ... } 250: 247: 244: 241: 238: 235: 232: 229: 226: 223: 220: 217: 214: 211: 208: 205: 202: 199: 196: 193: 190: 187: 184: 181: 178: 175: 172: 169: 85: 78: 74: 71: 65: 60:this article by 51:inline citations 38: 37: 30: 21: 1354: 1353: 1349: 1348: 1347: 1345: 1344: 1343: 1324: 1323: 1273: 1268: 1267: 1259: 1255:Scholes, John. 1254: 1253: 1249: 1241: 1236: 1235: 1231: 1222: 1220: 1211: 1210: 1206: 1193: 1192: 1185: 1180: 1156:Iverson bracket 1136: 1120: 1119: 1116: 1113: 1110: 1107: 1104: 1101: 1098: 1095: 1092: 1089: 1086: 1083: 1080: 1077: 1074: 1071: 1068: 1065: 1062: 1059: 1056: 1053: 1050: 1025: 1020: 1019: 1016: 1013: 1010: 1007: 1004: 1001: 998: 995: 992: 989: 972: 971: 968: 965: 962: 959: 956: 953: 950: 947: 944: 941: 938: 935: 932: 929: 926: 923: 920: 917: 914: 911: 886: 876: 866: 847: 845:Multiple guards 823: 822: 813: 807: 806: 788: 777: 757: 756: 751: 750: 747: 744: 741: 738: 735: 732: 729: 726: 723: 720: 717: 714: 708: 632: 627: 626: 623: 620: 617: 614: 611: 608: 605: 602: 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 467: 464: 461: 458: 455: 452: 449: 439: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 327: 324: 321: 318: 315: 312: 309: 306: 303: 300: 297: 276: 272: 257: 252: 251: 248: 245: 242: 239: 236: 233: 230: 227: 224: 221: 218: 215: 212: 209: 206: 203: 200: 197: 194: 191: 188: 185: 182: 179: 176: 173: 170: 167: 161:dispose pattern 138: 126:guard statement 86: 75: 69: 66: 56:Please help to 55: 39: 35: 28: 23: 22: 15: 12: 11: 5: 1352: 1350: 1342: 1341: 1336: 1326: 1325: 1322: 1321: 1312: 1303: 1293: 1284: 1272: 1271:External links 1269: 1266: 1265: 1247: 1237:Turner, D. A. 1229: 1204: 1182: 1181: 1179: 1176: 1175: 1174: 1169: 1163: 1158: 1153: 1148: 1142: 1135: 1132: 1049: 1024: 1021: 1014:'even' 988: 910: 884: 874: 865: 862: 846: 843: 827: 814: 812: 809: 808: 805: 802: 799: 789: 787: 784: 783: 780: 776: 773: 770: 767: 764: 713: 707: 704: 688:formal methods 631: 628: 448: 296: 256: 253: 166: 137: 134: 88: 87: 70:September 2010 42: 40: 33: 26: 24: 14: 13: 10: 9: 6: 4: 3: 2: 1351: 1340: 1337: 1335: 1332: 1331: 1329: 1320: 1316: 1313: 1311: 1307: 1304: 1301: 1300:3 Expressions 1297: 1294: 1292: 1288: 1285: 1282: 1278: 1275: 1274: 1270: 1258: 1251: 1248: 1240: 1233: 1230: 1219: 1215: 1208: 1205: 1200: 1196: 1190: 1188: 1184: 1177: 1173: 1170: 1167: 1166:Sentinel node 1164: 1162: 1159: 1157: 1154: 1152: 1149: 1146: 1143: 1141: 1138: 1137: 1133: 1131: 1129: 1125: 1047: 1044: 1042: 1038: 1034: 1030: 1029:pattern guard 1023:Pattern guard 1022: 1011:'odd' 986: 983: 981: 977: 908: 906: 902: 898: 896: 891: 883: 881: 873: 871: 863: 861: 859: 855: 850: 844: 842: 839: 810: 803: 800: 797: 785: 778: 774: 768: 762: 754: 711: 705: 703: 701: 697: 693: 689: 685: 681: 677: 673: 669: 665: 661: 657: 653: 649: 645: 641: 637: 629: 446: 444: 294: 292: 287: 285: 280: 270: 266: 262: 254: 164: 162: 159:), as in the 158: 154: 149: 147: 143: 135: 133: 131: 130:preconditions 127: 123: 119: 115: 111: 107: 103: 99: 95: 84: 81: 73: 63: 59: 53: 52: 46: 41: 32: 31: 19: 1314: 1305: 1295: 1287:Guard Clause 1280: 1250: 1232: 1221:. Retrieved 1217: 1212:Cook, Nate. 1207: 1198: 1123: 1121: 1045: 1028: 1026: 984: 973: 899: 894: 887: 880:fall-through 879: 877: 867: 851: 848: 840: 755: 752: 709: 699: 679: 633: 440: 288: 281: 258: 150: 146:null-pointer 139: 125: 121: 118:guard clause 117: 97: 91: 76: 67: 48: 1291:WikiWikiWeb 858:logical AND 706:Mathematics 680:constraints 676:Mathematica 630:Terminology 456:funcNoGuard 284:refactoring 153:idempotence 116:is used, a 62:introducing 1328:Categories 1298:, chapter 1223:2016-02-26 1195:Beck, Kent 1178:References 700:conditions 394:isinstance 319:isinstance 265:subroutine 261:early exit 148:failures. 122:guard code 45:references 1218:NSHipster 1140:Assertion 980:Kent Beck 976:Smalltalk 895:undefined 864:Evolution 818:otherwise 742:otherwise 549:funcGuard 304:f_noguard 142:reference 106:execution 1308:section 1134:See also 888:In 1966 793:if  231:username 198:username 183:username 1039:titled 656:Promela 640:Haskell 376:f_guard 263:from a 110:program 108:of the 58:improve 1096:lookup 1075:lookup 1051:clunky 990:parity 668:Python 648:Erlang 609:return 606://code 603://code 600://code 588:return 528:return 504:return 501://code 498://code 495://code 427:return 412:return 364:return 346:return 291:Python 225:nameof 180:string 171:string 168:public 47:, but 1277:Guard 1260:(PDF) 1242:(PDF) 1093:<- 1072:<- 890:ISWIM 672:Scala 664:Swift 660:OCaml 652:occam 644:Clean 483:>= 424:#code 421:#code 418:#code 343:#code 340:#code 337:#code 275:with 213:throw 124:, or 100:is a 98:guard 1114:val2 1108:val1 1102:var2 1090:val2 1087:Just 1081:var1 1069:val1 1066:Just 1060:var2 1057:var1 966:> 905:SASL 801:> 727:> 576:< 522:else 415:None 367:None 358:else 204:null 157:nops 136:Uses 96:, a 1279:in 1099:env 1078:env 1054:env 945:fac 912:fac 901:KRC 870:CPL 636:APL 555:int 546:int 462:int 453:int 406:int 391:not 373:def 331:int 301:def 216:new 174:Foo 92:In 1330:: 1289:, 1216:. 1186:^ 982:. 960:), 882:: 702:. 666:, 662:, 658:, 654:, 650:, 646:, 642:, 638:, 567:if 474:if 445:: 409:): 388:if 385:): 334:): 316:if 313:): 293:: 279:. 240:); 234:), 201:== 192:if 120:, 1302:. 1262:. 1244:. 1226:. 1111:+ 1105:= 1084:, 1063:| 1017:} 1008:: 1005:⍵ 1002:∣ 999:2 996:{ 993:← 969:0 963:n 957:1 954:- 951:n 948:( 942:* 939:n 936:= 933:0 930:= 927:n 924:, 921:1 918:= 915:n 811:0 804:0 798:x 786:1 779:{ 775:= 772:) 769:x 766:( 763:f 748:0 745:= 739:| 736:1 733:= 730:0 724:x 721:| 718:x 715:f 624:} 621:; 618:1 615:+ 612:x 597:} 594:; 591:0 585:{ 582:) 579:0 573:x 570:( 564:{ 561:) 558:x 552:( 540:} 537:} 534:; 531:0 525:{ 519:} 516:; 513:1 510:+ 507:x 492:{ 489:) 486:0 480:x 477:( 471:{ 468:) 465:x 459:( 443:C 436:1 433:+ 430:x 403:, 400:x 397:( 382:x 379:( 361:: 355:1 352:+ 349:x 328:, 325:x 322:( 310:x 307:( 249:} 243:} 228:( 222:( 210:{ 207:) 195:( 189:{ 186:) 177:( 163:. 83:) 77:( 72:) 68:( 54:. 20:)

Index

Guard (computing)
references
inline citations
improve
introducing
Learn how and when to remove this message
computer programming
Boolean expression
execution
program
programming language
preconditions
reference
null-pointer
idempotence
nops
dispose pattern
early exit
subroutine
structured programming
refactoring
Python
C
APL
Haskell
Clean
Erlang
occam
Promela
OCaml

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.