Loops/While: Difference between revisions

added Ursala
(Added R code)
(added Ursala)
Line 518:
test $a -gt 0 && (expr $a / 2 >> p.res ; echo $a) || exit 0
done</lang>
 
=={{header|Ursala}}==
Unbounded iteration is expressed with the -> operator. An expression
(p-> f) x, where p is a predicate and f is a function, evaluates to x,
f(x), or f(f(x)), etc. as far as necessary to falsify p.
 
Printing an intermediate result on each iteration is a bigger problem
because side effects are awkward. Instead, the function g in this
example iteratively constructs a list of results, which is displayed
on termination.
 
The argument to g is the unit list <1024>. The predicate p is ~&h,
the function that tests whether the head of a list is non-null
(equivalent to non-zero). The iterated function f is that which conses
the truncated half of the head of its argument with a copy of the
whole argument. The main program takes care of list reversal and
formatting.
<lang Ursala>
#import nat
 
g = ~&h-> ^C/half@h ~&
 
#show+
 
main = %nP*=x g <1024>
</lang>
output:
<pre>
1024
512
256
128
64
32
16
8
4
2
1
0
</pre>
 
=={{header|V}}==
Anonymous user