فهرست منبع

Document conditionals

David Peter 2 سال پیش
والد
کامیت
7c8ce1796e
2فایلهای تغییر یافته به همراه26 افزوده شده و 12 حذف شده
  1. 13 6
      book/src/example-numbat_syntax.md
  2. 13 6
      examples/numbat_syntax.nbt

+ 13 - 6
book/src/example-numbat_syntax.md

@@ -57,10 +57,10 @@ pi/3 + pi // cos  # Same, 'arg // f' is equivalent to 'f(arg)'
 
 # 4. Variable definitions
 
-let x = 4                          # Simple numerical variable
-let y1 = 2 m/s                     # Right hand side can be any expression
-let y2: Speed = 2 m/s              # With optional type annotation
-let y3: Length / Time = 2 m/s      # more complex type annotation
+let n = 4                          # Simple numerical variable
+let q1 = 2 m/s                     # Right hand side can be any expression
+let q2: Speed = 2 m/s              # With optional type annotation
+let q3: Length / Time = 2 m/s      # more complex type annotation
 
 # 5. Function definitions
 
@@ -89,9 +89,16 @@ unit warhol: Fame                # New base unit for the "Fame" dimension
 unit thing                       # New base unit with automatically generated
                                  # base dimension "Thing"
 
-# 8. Procedures
+# 8. Conditionals
 
-print(2 kilowarhol)              # Print a quantity
+fn step(x: Scalar) -> Scalar =   # Numbat has 'if <cond> then <expr> else <expr>'
+  if x < 0 then 0 else 1         # condition expressions
+
+# 9. Procedures
+
+print(2 kilowarhol)              # Print the value of an expression
 assert_eq(1 ft, 12 in)           # Assert that two quantities are equal
+assert_eq(1 yd, 1 m, 10 cm)      # Assert that two quantities are equal, up to
+                                 # the given precision
 type(2 m/s)                      # Print the type of an expression
 ```

+ 13 - 6
examples/numbat_syntax.nbt

@@ -52,10 +52,10 @@ pi/3 + pi // cos  # Same, 'arg // f' is equivalent to 'f(arg)'
 
 # 4. Variable definitions
 
-let x = 4                          # Simple numerical variable
-let y1 = 2 m/s                     # Right hand side can be any expression
-let y2: Speed = 2 m/s              # With optional type annotation
-let y3: Length / Time = 2 m/s      # more complex type annotation
+let n = 4                          # Simple numerical variable
+let q1 = 2 m/s                     # Right hand side can be any expression
+let q2: Speed = 2 m/s              # With optional type annotation
+let q3: Length / Time = 2 m/s      # more complex type annotation
 
 # 5. Function definitions
 
@@ -84,8 +84,15 @@ unit warhol: Fame                # New base unit for the "Fame" dimension
 unit thing                       # New base unit with automatically generated
                                  # base dimension "Thing"
 
-# 8. Procedures
+# 8. Conditionals
 
-print(2 kilowarhol)              # Print a quantity
+fn step(x: Scalar) -> Scalar =   # Numbat has 'if <cond> then <expr> else <expr>'
+  if x < 0 then 0 else 1         # condition expressions
+
+# 9. Procedures
+
+print(2 kilowarhol)              # Print the value of an expression
 assert_eq(1 ft, 12 in)           # Assert that two quantities are equal
+assert_eq(1 yd, 1 m, 10 cm)      # Assert that two quantities are equal, up to
+                                 # the given precision
 type(2 m/s)                      # Print the type of an expression