400 lines
No EOL
13 KiB
HTML
400 lines
No EOL
13 KiB
HTML
|
||
|
||
<!DOCTYPE html>
|
||
<html class="writer-html5" lang="zh-tw" >
|
||
<head>
|
||
<meta charset="utf-8">
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<title>基本函數介紹 — Tshunhue Documents documentation</title>
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
|
||
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<!--[if lt IE 9]>
|
||
<script src="_static/js/html5shiv.min.js"></script>
|
||
<![endif]-->
|
||
|
||
|
||
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
|
||
<script src="_static/jquery.js"></script>
|
||
<script src="_static/underscore.js"></script>
|
||
<script src="_static/doctools.js"></script>
|
||
<script src="_static/language_data.js"></script>
|
||
|
||
<script type="text/javascript" src="_static/js/theme.js"></script>
|
||
|
||
|
||
<link rel="index" title="Index" href="genindex.html" />
|
||
<link rel="search" title="Search" href="search.html" />
|
||
<link rel="next" title="型別" href="%E5%9E%8B%E5%88%A5.html" />
|
||
<link rel="prev" title="Welcome to Tshunhue Documents’s documentation!" href="index.html" />
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav">
|
||
|
||
|
||
<div class="wy-grid-for-nav">
|
||
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search" >
|
||
|
||
|
||
|
||
<a href="index.html" class="icon icon-home" alt="Documentation Home"> Tshunhue Documents
|
||
|
||
|
||
|
||
</a>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
|
||
<input type="text" name="q" placeholder="Search docs" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
|
||
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<p class="caption"><span class="caption-text">Contents</span></p>
|
||
<ul class="current">
|
||
<li class="toctree-l1 current"><a class="current reference internal" href="#">基本函數介紹</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="#id2">四則運算</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="#id3">布爾值</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="#id4">流程控制</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="#id5">影印字元</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="#id6">定義變數 (=)</a><ul>
|
||
<li class="toctree-l3"><a class="reference internal" href="#id7">非函數</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="#id8">函數(閉包)</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2"><a class="reference internal" href="#lambda-function">匿名函數 (lambda function)</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1"><a class="reference internal" href="%E5%9E%8B%E5%88%A5.html">型別</a></li>
|
||
</ul>
|
||
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
||
|
||
|
||
<nav class="wy-nav-top" aria-label="top navigation">
|
||
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="index.html">Tshunhue Documents</a>
|
||
|
||
</nav>
|
||
|
||
|
||
<div class="wy-nav-content">
|
||
|
||
<div class="rst-content">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div role="navigation" aria-label="breadcrumbs navigation">
|
||
|
||
<ul class="wy-breadcrumbs">
|
||
|
||
<li><a href="index.html" class="icon icon-home"></a> »</li>
|
||
|
||
<li>基本函數介紹</li>
|
||
|
||
|
||
<li class="wy-breadcrumbs-aside">
|
||
|
||
|
||
<a href="_sources/基本函數介紹.rst.txt" rel="nofollow"> View page source</a>
|
||
|
||
|
||
</li>
|
||
|
||
</ul>
|
||
|
||
|
||
<hr/>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<div class="section" id="id1">
|
||
<h1>基本函數介紹<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h1>
|
||
<div class="section" id="id2">
|
||
<h2>四則運算<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">+ : (-> int int int)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>整數相加。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">- : (-> int int int)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>整數相減。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">* : (-> int int int)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>整數相乘。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">/ : (-> int int int)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>整數相除,得商數。除以 0 的時候跳出特例,中止程式。如果用 C 語言表示的話:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">static</span> <span class="n">jmp_buf</span> <span class="n">buf</span><span class="p">;</span>
|
||
<span class="o">...</span>
|
||
<span class="n">void</span> <span class="n">int_div_closure_thunk</span><span class="p">(</span><span class="n">Object</span><span class="o">*</span> <span class="n">closure</span><span class="p">,</span> <span class="n">Object</span><span class="o">*</span> <span class="n">lhs</span><span class="p">,</span> <span class="n">Object</span><span class="o">*</span> <span class="n">rhs</span><span class="p">,</span> <span class="n">Object</span><span class="o">*</span> <span class="n">result</span><span class="p">){</span>
|
||
<span class="k">if</span> <span class="p">(</span><span class="n">lhs</span><span class="o">-></span><span class="n">value</span><span class="o">==</span><span class="mi">0</span><span class="p">){</span>
|
||
<span class="n">printf</span><span class="p">(</span><span class="s2">"Exception: Div 0 Error."</span><span class="p">);</span>
|
||
<span class="n">longjmp</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
|
||
<span class="p">}</span>
|
||
<span class="o">...</span>
|
||
<span class="nb">int</span> <span class="n">main</span><span class="p">(</span><span class="n">void</span><span class="p">){</span>
|
||
<span class="nb">int</span> <span class="n">jmpVal</span> <span class="o">=</span> <span class="n">setjmp</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>
|
||
<span class="k">if</span> <span class="p">(</span><span class="n">jmpVal</span> <span class="o">==</span> <span class="mi">0</span><span class="p">){</span>
|
||
<span class="o">//</span> <span class="n">main_body</span>
|
||
<span class="p">}</span><span class="k">else</span><span class="p">{</span>
|
||
<span class="o">//</span> <span class="n">exit</span> <span class="n">the</span> <span class="n">program</span> <span class="k">with</span> <span class="n">the</span> <span class="n">exception</span>
|
||
<span class="n">printf</span><span class="p">(</span><span class="s2">"Exit the program with the exception"</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">+. : (-> dou dou dou)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>浮點數相加。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">-. : (-> dou dou dou)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>浮點數相減。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">*. : (-> dou dou dou)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>浮點數相乘。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">/. : (-> dou dou dou)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>浮點數相除。</p>
|
||
</div>
|
||
<div class="section" id="id3">
|
||
<h2>布爾值<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h2>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">and : (-> bool bool bool)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>AND。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">or : (-> bool bool bool)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>OR。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">not : (-> bool bool)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>NOT。</p>
|
||
</div>
|
||
<div class="section" id="id4">
|
||
<h2>流程控制<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h2>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">(if cond then alt)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>若 cond 為真則返回 then,否則返回 alt。</p>
|
||
</div>
|
||
<div class="section" id="id5">
|
||
<h2>影印字元<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h2>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">print-str : (-> str void)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>列印字串。<code class="docutils literal notranslate"><span class="pre">void</span></code> 表示不返回任何東西。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">int-to-str : (-> int str)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>整數轉成十進位字串(10 為 base)。</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">dou-to-str : (-> int str)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>浮點數轉成十進位字串(10 為 base)。</p>
|
||
</div>
|
||
<div class="section" id="id6">
|
||
<h2>定義變數 (=)<a class="headerlink" href="#id6" title="Permalink to this headline">¶</a></h2>
|
||
<div class="section" id="id7">
|
||
<h3>非函數<a class="headerlink" href="#id7" title="Permalink to this headline">¶</a></h3>
|
||
<p>語法如下:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="k">def</span> <span class="nf">Type</span> <span class="n">var</span> <span class="n">expression</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>例如:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="k">def</span> <span class="nf">int</span> <span class="n">a</span> <span class="mi">10</span><span class="p">)</span>
|
||
<span class="p">(</span><span class="k">def</span> <span class="nf">dou</span> <span class="n">b</span> <span class="p">(</span><span class="o">+</span> <span class="mf">3.1</span> <span class="mf">7.0</span><span class="p">))</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>..note</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>因為 tshunhue 裡面,所有變數都是不可變物件, ``define`` 不可以拿來重新設定值。定義閉包也一樣。
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="id8">
|
||
<h3>函數(閉包)<a class="headerlink" href="#id8" title="Permalink to this headline">¶</a></h3>
|
||
<p id="defining-lambda">閉包的定義比較複雜。但大致如下:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="k">def</span> <span class="nf">Type</span> <span class="n">var</span> <span class="k">lambda</span><span class="o">-</span><span class="n">function</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>例如:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>(def (-> int int) int-add1 (\ ((int x)) (+ x 1)))
|
||
(def (-> dou dou dou) sqrt-sum (\ ((dou x)(dou y)) (+. (*. x x) (*. y y))))
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="lambda-function">
|
||
<h2>匿名函數 (lambda function)<a class="headerlink" href="#lambda-function" title="Permalink to this headline">¶</a></h2>
|
||
<p>定義:</p>
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">(\ () body+)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">(\ ((Type var)) body+)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<dl class="py function">
|
||
<dt>
|
||
<code class="sig-name descname">(\ ((Type1 var1)(Type2 var2)[...]*) body+)</code></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>所有函數都是第一類物件與閉包,用 lexical scope 保存前文變數。</p>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
<footer>
|
||
|
||
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
||
|
||
<a href="%E5%9E%8B%E5%88%A5.html" class="btn btn-neutral float-right" title="型別" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
|
||
|
||
|
||
<a href="index.html" class="btn btn-neutral float-left" title="Welcome to Tshunhue Documents’s documentation!" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
|
||
|
||
</div>
|
||
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>
|
||
|
||
© Copyright 2020, Yoxem Chen
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
|
||
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
|
||
|
||
<a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
|
||
|
||
provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
||
|
||
</footer>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
</section>
|
||
|
||
</div>
|
||
|
||
|
||
<script type="text/javascript">
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(true);
|
||
});
|
||
</script>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</body>
|
||
</html> |