By choosing among the operations lfold, rfold, map and select, write a definition for each of the following that does not use recursion or loops.
Write a function called doubleAll that takes a list x of numbers as its parameter and produces a list of the doubles numbers in list x as its result. For example, doubleAll([5,2,19,3]) = [10,4,38,6].
double(x) = x + x
doubleAll = map double
Remark. You can always use helper functions. This
definition still does not have any recursion, either direct
or mutual.
Write a function called firstPos that returns the first positive number from a list of numbers. For example, firstPos([-4, -2, 0, 6, 12, -9]) = 6. Presume that the list has at least one positive number in it.
positive(x) = x > 0
firstPos = select positive
Consider the following definition written in Astarte.
Define f(?n) = (:n*n :: f(n+1):).What value does expression f(2) compute? Give the full value, as it would be if it were examined. (It suffices to give a clear description of the full value.)
f(2) = [4,9,16,25,...]
(an infinite list of the squares of the numbers 2,3,4,...)
What is the purpose of the static link in a frame in the run-time stack?
The static link in a frame for function f points to the frame of the function that created f. It is used to find nonlocal bindings.
What is the value of Scheme expression (car (cdr (cons 'horse (cons 'zebra ()))))?
zebra
Write a Scheme function called prefix so that (prefix x y) returns true if list x is a prefix of list y. Be careful to use Scheme syntax correctly.
(define (prefix x y)
(cond
((null? x) #t)
((null? y) #f)
((equal? (car x) (car y)) (prefix (cdr x) (cdr y)))
(else #f)
)
)
What is one important motivation for including exception handling in a programming language?
Some acceptable answers include the following.
Using backtracking, write an Astarte program fragment that will print all solutions (x,y) to equation xy - 2x2 + y = 10, where x and y are both integers in the range 0,...,100. Do not use a loop or recursion. Your program fragment can fail when it is done.
Let x = each(0 _upto_ 100).
Let y = each(0 _upto_ 100).
{x*y - 2*x^2 + y == 10}
Writeln[$(x,y)].
{false}