# newLISP Challenge!

Before we continue with Tutorial 3 on **newLISP**, let’s do now some exercises in newLISP programming. You’ve to solve some little problems. To do so successfully, recall the contents of the first two tutorials on newLISP of mine. In addition, you can use http://www.newlisp.org/downloads/manual_frame.html for info on newLISP functions.

*[Notice that for some problems one could find better solutions when invoking other functions than discussed in the first two tutorials.]*

**1. Determine the returned value of the following expressions:**

a. *(< 0 1 1 2 3 5 8 13 21 34 55 89 144)*

→ nil

b. *‘(+ 1 2 (- 1 2))*

→ (+ 1 2 (- 1 2))

Note: a quoted expression, i.e. an expression preceded by an apostrophe, means to newLISP: “do not evaluate this expression as a function, but literally, i.e. as data and return simply the expression without apostrophe.”

c. *(add 3E4 (* 2 (pow 10 1)) 7E-2)*

→ 30020.07

d. *(and (> (/ 1 5) (* 5 (/ 1 30))) “Math can be very challenging!”)*

→ nil

Note: do you see why *(and (> (div 1 5) (mul 5 (div 1 30))) “Math can be very challenging!”)* returns “Math can be very challenging!”?

e. *(string? ‘(“newLISP puts the fun back in LISP”))*

→ nil

Note: *(string? “newLISP puts the fun back in LISP”) *would return the value true.

f. *(or (number? ‘(100)) (float? 3.1415))*

→ true

**2. Write a single expression that tests if the absolute value of the substraction 2 – 5 is greater than 0, but returns the value nil**

*(not (> (abs (- 2 5)) 0))*

→ nil

Note: using the if conditional (that we did not discussed in the previous tutorials), you could write:

*(if (> (abs (- 2 5)) 0) nil true)*

→ nil

Read: if (test-is-successful) then (expression-test-is-successful) else (expression-test-is-not-successful)

Without the optional else part: *(if (> (abs (- 2 5)) 0) nil)* evaluates to nil also.

**3. Write a single expression that returns a float value, which is the difference between the highest and the lowest value of the following series of four numbers: 2 1.5 3 2.7**

*(sub (max 2 1.5 4 2.7) (min 2 1.5 4 2.7))*

→ 2.5

Note: change sub into – (minus) and the result will be 3: *(- 4 1.5)* will be interpreted as *(- 4 1)*

**4. Write a single expression that returns a random number between 1 and 10 (integer) and multiply the result with 2.5**

*(mul (+ 1 (rand 10)) 2.5)*

→ the answer could be 22.5

Note: The expression (rand 10) generates a number between 0 and 9. So we have to add 1 to the result in order to get a random number between 1 and 10: *(+ 1 (rand 10))*

**5. Use in any case the functions ‘zero?’ and ‘mod’ for the next problem. You may not use the function ‘even?’. Problem: test in a single expression if the number 10 is even.**

*(zero? (mod 10 2))*

→ true

**6. Write a single expression that concatenates the list of string elements (“2” “3” “4”) into the string “2-3-4”**

*(join ‘(“2” “3” “4”) “-“)*

→ “2-3-4”

Note: the first parameter of the function join is a list of strings, in this case *‘(“2” “3” “4”)*

**7. Write a single expression that calculates the sum of squares of the list (3 9 27). This list should be the result of the built-in function ‘series’.**

*(ssq (series 3 3 3))*

→ 819

**8. Use in any case the functions ‘zero?’ and ‘pow’ for the next problem. You may not use the function ‘sqrt’. Problem: test if the number 9 is unequal to 0 and prove that the difference between the square root of 100 and the square root of 81 equals to 1. Of course, in a single expression.**

*(and (not (zero? 9)) (= (- (pow 100 0.5) (pow 81 0.5)) 1))*

→ true

Note: the solution with the if conditional:

*(if (zero? 9) nil (= (sub (pow 100 0.5) (pow 81 0.5)) 1))*

→ true

or

*(if (not (zero? 9)) (= (sub (pow 100 0.5) (pow 81 0.5)) 1))
*→ true

**9. Write a single expression that validates the string “AbcD XyZ” and shows that the length of “AbcD XyZ” equals to 8**

*(regex “[a-z ]+” “AbcD XyZ” 1)*

→ (“AbcD XyZ” 0 8)

**
**Note: this expression with the case-insensitive search option 1 only tests if the string AbcD XyZ consists of alphabetic and space characters.

*(regex “[a-d]{4}[ ]{1}[x-z]{3}” “AbcD XyZ” 1)*is more restrictive.

**10. Write a single expression that transforms the list (2 3 4) into the string “3-4-5”
**

*(join (map string (map inc ‘(2 3 4))) “-“)*

→ “3-4-5”

Note: study the following steps

*(map inc ‘(2 3 4))*

→ (3 4 5)

i.e. “map the function inc to each element of the list (2 3 4)”

(map string (map inc ‘(2 3 4)))

→ (“3” “4” “5”)

i.e. “map the function string to each element of the list (3 4 5)”

(join ‘(“3” “4” “5”) “-“)

→ “3-4-5”

So, not enough questions? Ok, a last tricky one:

**11. Write a single expression that**

– and tests if the substring has indeed three words, which are separated by one space;

– the substring has no spaces at the beginning and at the end.

*(regex “^\\w+[ ]{1}\\w+[ ]{1}\\w+$” (member “not” “to be or not to be”))*

→ (“not to be” 0 9)

To avoid double backslashes, use curly braces:

*(regex {^\w+[ ]{1}\w+[ ]{1}\w+$} (member “not” “to be or not to be”))*

→ (“not to be” 0 9)

Thank you for reading. Till next time!

*Reinier Maliepaard *

*(last update: 24-06-2019)*

## Comments

## Vadim

Reinier, thank you very much for this work. Without a detailed description, it is impossible to use a powerful and useful plugin. You’ve done an important job!