Jump to content

The IF* macro used in Allegro


Recommended Posts

Opusmodus 1.1.1.8740

New macro IF* added to Opusmodus system.

 

The IF* (public domain) macro used in Allegro:

if*

Arguments:

(test-form {then then-form+ | thenret} {elseif else-test-form {then else-then-form+ | thenret}}* [else else-form+])

This form consists of a series of clauses introduced by the symbols then, elseif, else, and thenret.
First the predicate test-form is evaluated. If it is true, the then-forms are evaluated,
and the value of the last such form is returned. If test-form evaluates to nil,
any remaining clauses are processed. If no clauses remain, if* returns nil.

When a thenret clause is encountered no further evaluation takes place,
and the value of the most recently evaluated test-form is returned.

When an elseif clause is encountered, the predicate else-test-form is evaluated.
If it is true, the else-then-forms are evaluated, and the value of the last such
form is returned; otherwise any remaining clauses are processed. If no clauses remain,
if* returns nil. And lastly, when an else clause is encountered,
the else-forms are evaluated, and the value of the last such form is returned.

Examples

;; The basic format of a IF* expression is:
;; 
;; (if* [test] then [do this 1] [do this 2] else [do other 1] [do other 2])
;;
;; When [test] is true, the forms after the THEN are evaluated and the
;; result of the last returned; if [test] if false, the forms after the
;; ELSE are evaluated and the result of the last is returned.

;; So:

(if* (> 3 2) then "three is bigger" 3
                     else "three is smaller" 2)
=> 3
;;  Your do not need an ELSE form:
(if* (> 3 2) then "three is bigger" 3)
=> 3

(if* (> 2 3) then "two is bigger" 2)
=> nil

;; You can have multiple fors after THEN or ELSE:
(defun foo (x)
 (if* x then (setq y 2) (print x) else (setq y -2) "no"))

(foo 2)
=> 2

(foo "hello")
=> "hello" 
    "hello"

(foo nil)
=> "no"

;; There are two more special symbols: THENRET and ELSEIF.

;; THENRET says when the test is true just return the value of the test
;; form just evaluated:

(if* (+ 4 5) thenret)
=> 9

;; ELSEIF introduces a new test, so you can have compound tests:

(setq score 77)
(if* (< score 60) then "F" 
     elseif (< score 70) then "D"
     elseif (< score 80) then "C" 
     elseif (< score 90) then "B" 
     else "A")
=> "C"

(setq score 55)
(if* (< score 60) then "F" 
     elseif (< score 70) then "D"
     elseif (< score 80) then "C" 
     elseif (< score 90) then "B" 
     else "A")
=> "F"

(setq score 92)
(if* (< score 60) then "F" 
     elseif (< score 70) then "D"
     elseif (< score 80) then "C" 
     elseif (< score 90) then "B" 
     else "A")
=> "A"

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy