How to resolve the algorithm Extend your language step by step in the EchoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Extend your language step by step in the EchoLisp programming language

Table of Contents

Problem Statement

Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other languages go much further. Most notably in the Forth and Lisp families, programming per se is done by extending the language without any formal distinction between built-in and user-defined elements. If your language supports it, show how to introduce a new flow control mechanism. A practical and useful example is a four-way branch: Occasionally, code must be written that depends on two conditions, resulting in up to four branches (depending on whether both, only the first, only the second, or none of the conditions are "true"). In a C-like language this could look like the following: Besides being rather cluttered, the statement(s) for 'condition2isTrue' must be written down twice. If 'condition2isTrue' were a lengthy and involved expression, it would be quite unreadable, and the code generated by the compiler might be unnecessarily large. This can be improved by introducing a new keyword if2. It is similar to if, but takes two conditional statements instead of one, and up to three 'else' statements. One proposal (in pseudo-C syntax) might be: Pick the syntax which suits your language. The keywords 'else1' and 'else2' are just examples. The new conditional expression should look, nest and behave analogously to the language's built-in 'if' statement.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Extend your language step by step in the EchoLisp programming language

Source code in the echolisp programming language

(define-syntax-rule 
	(if2 cond1 cond2 both cond1-only cond2-only none) ;; new syntax
;; will expand to :
	(if cond1 
		(if cond2 both cond1-only)
		(if cond2 cond2-only none)))
   → #syntax:if2
   
(define (num-test n)
    (if2 (positive? n) (exact? n)
	  "positive and exact"
	  "positive and inexact"
	  "negative and exact"
	  "negative and inexact"))
	
(num-test 3/4)
   → "positive and exact"
(num-test -666)
   → "negative and exact"
(num-test -666.42)
   → "negative and inexact"
(num-test PI)
   → "positive and inexact"


  

You may also check:How to resolve the algorithm Substring/Top and tail step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Array length step by step in the SmallBASIC programming language
You may also check:How to resolve the algorithm Nim game step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the JavaScript programming language