{
"cells": [
{
"cell_type": "markdown",
"id": "7fff49f7",
"metadata": {},
"source": [
"# `!?|⌈⌊⊥⊤⊣⊢`\n",
"## Factorial, binomial `!`\n",
"Monadic `!` is [factorial](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Factorial.htm). Note that it goes on the left (like all other monadic APL functions) as opposed to mathematics' $!$.\n",
"\n",
"Dyadic `A!B` is [binomial](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Binomial.htm). It is the number of ways to take `A` items from a bag of `B` items, generalised to be the binomial function. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "5e7fbbea",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"479001600\n",
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"28\n",
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"!12 ⍝ 12 factorial\n",
"2!8 ⍝ how many ways can we select 2 from 8?"
]
},
{
"cell_type": "markdown",
"id": "b98f072e",
"metadata": {},
"source": [
"## Roll, deal `?`\n",
"Monadic `?B` is [roll](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Roll.htm). It returns a random integer among the first `B` integers. `?0` returns a random float between (but not including) 0 and 1: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "59a91fb8",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"4 3 4\n",
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"0.320547\n",
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"?6 6 6 ⍝ roll three six-sided dice\n",
"?0 ⍝ random float between 0-1, excluding 0 and 1"
]
},
{
"cell_type": "markdown",
"id": "41a089e3",
"metadata": {},
"source": [
"Dyadic `A?B` is [deal](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Deal.htm). It returns a random one of the ways `A!B` counted. I.e. it returns `A` random numbers among the `B` first integers. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "919ea12a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"1 6 9 2 4 7 8 3 5 10\n",
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10?10 ⍝ 1-10 in random order"
]
},
{
"cell_type": "markdown",
"id": "74bcfb44",
"metadata": {},
"source": [
"Note that it deals from the set `⍳B`, so it's dependent on your `⎕IO` setting:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "754445eb",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"8 0 3 6 5 2 7 9 4 1\n",
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10?10 ⊣ ⎕IO←0 ⍝ Now we should get 0-9\n",
"⎕IO←1 ⍝ Reset ⎕IO to default"
]
},
{
"cell_type": "markdown",
"id": "ac53893c",
"metadata": {},
"source": [
"## Magnitude, residue `|`\n",
"Monadic `|` is [magnitude](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Magnitude.htm), also called the absolute value, $|x|$: "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3792814c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"97\n",
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"3 5 7 8 7 2\n",
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"|¯97\n",
"|3 5 ¯7 ¯8 7 ¯2"
]
},
{
"cell_type": "markdown",
"id": "e983bb39",
"metadata": {},
"source": [
"Dyadic `A|B` is [residue](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Residue.htm), also known as the _division remainder_ (\"mod\") when `B` is divided by `A`. Note the reversed order of arguments. \"normal\" mod is `|⍨`. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2a957e19",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"1 0 1 0 1 0 1 0 1 0\n",
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2|⍳10 ⍝ odd numbers in 1-10"
]
},
{
"cell_type": "markdown",
"id": "bebebeb1",
"metadata": {},
"source": [
"## Ceiling, maximum `⌈`\n",
"Monadic `⌈` is [ceiling](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Ceiling.htm), $⌈x⌉$, "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "d68e8f19",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"4\n",
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"⌈3.14159256"
]
},
{
"cell_type": "markdown",
"id": "6514f4e7",
"metadata": {},
"source": [
"Dyadic `A⌈B` is [maximum](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Maximum.htm): "
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "43df487d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"23\n",
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"15⌈23"
]
},
{
"cell_type": "markdown",
"id": "fc221dea",
"metadata": {},
"source": [
"## Floor, minimum `⌊`\n",
"Monadic `⌊` is [floor](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Floor.htm), and the dyadic is [minimum](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Minimum.htm),"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "210e5ea1",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"3\n",
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"15\n",
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"⌊3.14159256\n",
"15⌊23"
]
},
{
"cell_type": "markdown",
"id": "a0073428",
"metadata": {},
"source": [
"## Decode `⊥`\n",
"`A⊥B` is [decode](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Decode.htm). It evaluates digits `B` as (mixed) base `A`, e.g,"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "bd6bb720",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"42\n",
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2⊥1 0 1 0 1 0 ⍝ decode binary to decimal"
]
},
{
"cell_type": "markdown",
"id": "d6584d45",
"metadata": {},
"source": [
"## Encode `⊤`\n",
"`A⊤B`, or [encode](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Encode.htm), is the inverse of `⊥`, turning `B` into a list(s) of digits in (mixed) base `A`, "
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2b69d8bf",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"2 46 40\n",
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"24 60 60⊤10000 ⍝ seconds to hour, minutes, seconds"
]
},
{
"cell_type": "markdown",
"id": "e50f18bb",
"metadata": {},
"source": [
"Ten thousand seconds is the same as 2 hours, 46 minutes and 40 seconds. \n",
"## Left, right `⊣⊢`\n",
"Dyadic `⊣` is the [left](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Left.htm) argument unmodified. Monadically, it just returns its sole argument. Dyadic `⊢` is the [right](http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Right.htm) argument unmodified. Monadically, it just returns its sole argument. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "015bce93",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Dyalog APL",
"language": "apl",
"name": "dyalog-kernel"
},
"language_info": {
"file_extension": ".apl",
"mimetype": "text/apl",
"name": "APL"
}
},
"nbformat": 4,
"nbformat_minor": 5
}