===========
RosettaCode
===========
.. Modified: 2018-10-23/05:38-0400 btiffin
.. Copyright 2016 Brian Tiffin
.. GPL 3.0+ :ref:`license`
.. This file is part of the Unicon Programming documentation.
.. image:: images/unicon.png
:align: center
.. only:: html
:ref:`genindex`
:floatright:`Unicon`
.. index:: rosettacode
.. _rosettacode:
Unicon on rosettacode.org
=========================
Along with the :ref:`ipl`, rosettacode.org contains another treasure trove of
Unicon programming examples, tips, and learning materials.
http://rosettacode.org
http://rosettacode.org/wiki/Category:Unicon
Everything from simple Hello, world examples to complex graphing and other
meaty topics.
--------
Some samples
------------
.. index:: permutations, combinations
Combinations and Permutations
.............................
A nice example of concise Unicon, and the benefits of large integer support.
http://rosettacode.org/wiki/Combinations_and_permutations#Icon_and_Unicon
.. sourcecode:: unicon
procedure C(n,k)
every (d:=1) *:= 2 to k
return P(n,k)/d
end
procedure P(n,k)
every (p:=1) *:= (n-k+1) to n
return p
end
Where::
write("P(1000,10) = ",P(1000,10))
write("P(1000,15) = ",P(1000,15))
Gives::
P(1000,10) = 955860613004397508326213120000
P(1000,15) = 899864387800469514043972248293019354931200000
--------
.. index:: twosum
.. _twosum:
Two Sum
.......
Not yet a Unicon guru, here is a possibly less than stellar RosettaCode
entry by this author.
http://rosettacode.org/wiki/Two_Sum
.. literalinclude:: examples/onesum.icn
:language: unicon
.. only:: html
.. rst-class:: rightalign
:download:`examples/onesum.icn`
Sample run:
.. command-output:: unicon -s onesum.icn -x
:cwd: examples
The above sample does not quite highlight the expressive power of Unicon, the
task requires zero or one solution. Unicon can do much better than that.
.. literalinclude:: examples/twosum.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/twosum.icn`
Sample runs:
.. command-output:: unicon -s twosum.icn -x
:cwd: examples
.. command-output:: unicon -s twosum.icn -x 10 0 1 2 3 4 5 6 7 8 9 10
:cwd: examples
--------
.. index:: floating-point, fixed-point
Pathological floating point problems
....................................
http://rosettacode.org/wiki/Pathological_floating_point_problems
Found this one interesting, and was initially disappointed (but not for long),
as the Unicon `real ` data type falls into the trap of not having enough
precision to produce correct results for this task.
The initial quick trial for the sequence convergence shows the problem:
.. literalinclude:: examples/patho-real.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/patho-real.icn`
The sample run starts miscalculating the proper convergence pretty much after
the first iteration, and then shortly fails catastrophically converging on
100, instead of 6.
.. command-output:: unicon -s patho-real.icn -x
:cwd: examples
I mentioned it on the SourceForge forum, and how a REXX solution with inherent
decimal arithmetic (and explicit control on how many digits are used in
calculations) made for a very easy to read and correct solution. Bruce Rennie
came to the rescue almost immediately.
Unicon supports infinite length integers, which can be scaled out to allow
very precise control over the number of digits used in computations. A little
bit of fixed point decimal arithmetic, and large integers make this problem a
cake walk. Runs fast and accurate, and the source code is a very clean read.
Bruce posted a small example, which was modified a bit to suit a RosettaCode
entry (and to fill out other parts of the task, to further demonstrate the
ease of using highly precise fixed point decimal math in Unicon programs).
.. literalinclude:: examples/patho.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/patho.icn`
A much more satisfying run:
.. command-output:: unicon -s patho.icn -x
:cwd: examples
With only a small bit of fixed point management, Unicon is more than capable
of very precise fractional mathematics when `real ` native floating
point data may not be up to the task. Some care needs to be taken with the
order of scaling within the large integer operations, but other than that,
this style of programming is fairly straight forward. It shines another
bright light on Unicon for use in general purpose programming.
.. note::
The COBOL 2014 specification calls for 1,000 digits of internal precision
for financially sound computations. Unicon can easily meet and exceed that
requirement when using large integers in fixed point decimal calculations.
At speed.
.. only:: html
..
--------
:ref:`genindex` | Previous: :doc:`theory` | Next: :doc:`notes`
|