Control flow and looping constructs for the slope programming language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sloum 9af9600389 Updates while to only use a for loop since exists? cannot detect for since it is a special form 1 month ago
README.md Updates while to only use a for loop since exists? cannot detect for since it is a special form 1 month ago
main.slo Updates while to only use a for loop since exists? cannot detect for since it is a special form 1 month ago
module.json Updates while to only use a for loop since exists? cannot detect for since it is a special form 1 month ago

README.md

flow

Flow control macros and procedures for the slope programming language.

Included procedures

let

let creates what other languages might refer to as a block scope for the variables that let creates. The list that let gets passed should be a literally represented list: ((x 1)(y 2)) rather than a list expression: [[x 1][y 2]] or (list (list x 1) (list y 2)). The list should be a literally represented association of the variables that are to be avialable in the block and their associated values.

(load-mod flow)
(flow::let ((x 5) (y 22))
  (display-lines x y)
  (+ x y))

(display x y)

; Prints:
; 5
; 22

; Returns:
; 27

; The final display call throws an error
; since neither symbol exists outside of
; the let procedure

while

while as formulated here will use a for loop under the hood. Not all versions of the slope interpreter will have this built-in available (it was added in version 1.0.3), and while will cause an exception if run by such an interpreter. Otherwise, it behaves like a for loop: it evaluates the given test on each loop and if the test returns a truthy value will evaluate the body of the loop. The last evaluation from the body will be returned.

(load-mod flow)

(define x 1)

(flow::while (< x 6)
  (display-lines x)
  (set! x (+ x 1)))

; Prints:
; 1
; 2
; 3
; 4
; 5

; Returns:
; 6

when

Functions like an if with only a true block, but makes clear that only a truthy action will take place

(load-mod flow)

(define x 5)

(flow::when (positive? x) (display "Positive"))
(flow::when (negative? x) (display "Negative"))

; Prints:
; Positive

unless

Functions like an if with only a false block, but makes clear that only a falsy action will take place

(load-mod flow)

(define x 5)

(flow::unless (positive? x) (display "Not positive"))
(flow::unless (negative? x) (display "Not negative"))

; Prints:
; Not negative

for-count

Takes a count and a lambda and executes the lambda count number of times. If the lambda takes a value the current count will be passed (starting at zero, so that a count of 3 will produce the values 0, 1, and 2).

(load-mod flow)

(for-count 5 (lambda (x) (display (+ 1 x) "! ")))

; Prints:
; 1! 2! 3! 4! 5!

with

Creates a scope based on an open io-handle, runs all of the code in the body and returns the last evaluation of the body. with then closes the io-handle.

The first argument to with should be a literal representation of the list with a symbol and an open io-handle as the first and second item, respectively. After that comes all body content (there is an implicit begin wrapping the body content). Inside the body the io-handle can be referred to by the symbol given as the first item in the list passed as the first argument to with.

(load-mod flow)

(with (f (file-open-read "my-file.txt"))
  (display "Inside a with block!")
  (read-all f))

; Prints:
; Inside a with block!

; Returns:
; The contents of the given file