- Run the following code, both interpreted and compiled.
mycode: [prnt "Hello" halt]
do mycode
Explain the results.
- Rewrite the first do example in this chapter with does.
- What is the difference between the has1 and has2 functions in the following code?
has1: has [][
num: 108 - 42
print num
]
has2: has [num][
num: 108 - 42
print num
]
- What are the values of num1 and num2 after executing the following code? Explain.
ex-glob: function [
/extern num1 ]
[
num1: 13
num2: 42
]
num1: 100 ;== 100
num2: 200 ;== 200
ex-glob
- Define a sum function that sums two numbers, n and m. Use argument typing.
- Write a testref function that takes two number parameters. The function should have a /add refinement that returns their sum, and a /sub refinement that returns their subtraction.
- Write an average function that takes a series of numbers and returns its average.
- Red has a built-in sqrt function to calculate the square root of a number. For a negative argument, the mathematical result is a complex number. In Red, this function returns NaN (Not a Number): sqrt -4 ;== 1.#NaN. Write your own mysqrt function that uses sqrt, but guards against a negative input argument with unless. Also make sure that the input is a number! Test all kinds of input.
- Fully implement all guards for the fact function mentioned in the section on recursive functions.
- Make a car object that contains fields such as brand, selling price, VAT (or discount) percentage, and number of cars in stock. The object should also contain a function that calculates the total price with VAT included, and a function that calculates the total stock value. Make a more concrete object, such as, for example bmw or tesla, and test the object by calling the functions.
- Adapt the on-change function in person1 so that the name value cannot be changed.