[Question]
  [
(we won't be finding a `Find` or catching a `tryCatch`, though)
This is part two of a multi-part series of implementing some interesting R functions. Part one can be found [here](https://codegolf.stackexchange.com/q/188078/67312).
# The task:
You are to implement [R's `match` function](https://stat.ethz.ch/R-manual/R-devel/library/base/html/match.html) in as few bytes as possible.
## Input:
* `x`, a possibly empty list/array of integers
* `table`, a possibly empty list/array of integers
* `nomatch`, a single integer value
* `incomparables`, a possibly empty list/array of integers
## Output:
* a single array/list of integers `O` of equal length to `x`, where each value `O[i]` represents either:
	+ The index `j` of the **first** value in `table` where `table[j]==x[i]`
	+ `nomatch`, indicating that no value in `table` is equal to `x[i]` **OR** that `x[i]` is in the list of `incomparables`.
# Test Cases
```
All in the form x, table, nomatch, incomparables -> output
outputs 
[], [1,2,3], 0, [5] -> []
[1, 2, 3], [], 0, [5] -> [0, 0, 0]
[9, 4, 3, 6, 3], [9, 8, 7, 6, 5, 4, 3, 2, 1], -1, [4] -> [1, -1, 7, 4, 7]
[8, 6, 7, 5, 3, 0, 9], [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6], 1000, [1] -> [12, 8, 14, 5, 1, 1000, 6]
```
More test cases can be generated as needed.
## Additional rules:
* R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can use indices that start at 3 or 17 or whatever, but this must be consistent, and you must indicate this in your answer.
* If you chosen language has a builtin that does this, please also implement your own solution.
* Explanations are appreciated.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest solution in bytes wins!
  
[Answer]
# [R](https://www.r-project.org/), 83 bytes
```
function(x,t,n,i)sapply(x,function(a)c(which(a==t/!rowSums(outer(t,i,`==`))),n)[1])
```
[Try it online!](https://tio.run/##PYrLCsIwEEV/ZdzNwBWNj1oX@QqXIrQEQwOalDSh@vUxFnR37zknFquLzd4kFzy/kODhZOrH8fGu9296MTwPzgzca502qxjmS35OHHK6R05w6LTuRAReruomxbLhM@gA2oMa0LYOAS2wBZ0WePwFO5Cqdq2@RUWNSPkA "R – Try It Online")
Avoids `match`, `%in%` and `setdiff`.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~10~~ 8 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
-2 thanks to [Erik the Outgolfer](https://codegolf.stackexchange.com/users/41024)
```
,⁷y⁵iⱮ⁶o
```
A full program accepting four command line arguments, `incomparables nomatch table x` which prints a Jelly representation\* of the list of R's `match` function results.
**[Try it online!](https://tio.run/##NYy7DYAwDERX8QAuEgwkzBKlpABFok6ZhZAogH28iBGHaJ7s@61zKdWMtd1V27nosWu7NjNLPpt3zlkSJs/UgwPTxNQxjbgFjBADKHAFYo/YF36nsqWIJ/xlhw68Bw "Jelly – Try It Online")**
### How?
e.g. with `incomparables nomatch table x` = `[1,4], 2, [2,4], [4,3,2,1,0]`:
```
,⁷y⁵iⱮ⁶o - Main Link: list, incomparables; list, nomatch
 ⁷       - newline character                                '\n'
,        - pair (incompararables) with (right)              [[1,4],'\n']
   ⁵     - 5th argument (3rd input = table)                 [2,4]
  y      - translate (right) with lookup (left)             [2,'\n']             
      ⁶  - 6th argument (4th input = x)                     [4,3,2,1,0]
     Ɱ   - map with:
    i    -   first index of (right) in (left)               [0,0,1,0,0]
       o - logical OR                                       [2,2,1,2,2]
```
---
\* An empty list is represented as nothing, a list of lenth one is represented as just the item, while other lists are enclosed in `[]` and delimited by `,` 
[Answer]
# [R](https://www.r-project.org/), 55 bytes
In this case, the code doesn't use `match` with its full functionality, it is just used as an `index` function. First R answer, so probably incredibly inefficient byte-wise! 
**Note** (thanks to [Giuseppe](https://codegolf.stackexchange.com/users/67312/giuseppe) for the info)**:** `%in%` and `setdiff` are also both *internally* implemented using `match`, so completely getting rid of this surprisingly useful function will result in a *mess*. Therefore, there is a [150-rep bounty](https://codegolf.meta.stackexchange.com/questions/5243/list-of-bounties-with-no-deadline/17875#17875) with no deadline for this! (note that `setdiff` is allowed, though)
```
function(x,t,n,i)ifelse(x%in%setdiff(t,i),match(x,t),n)
```
[Try it online!](https://tio.run/##dZBdCsMgEITfewqhBBQ2EGP@hOYwxUYiNLYklub2dl2QUkre1tlvZ2TWuLBLyaJ9eRPcw/MdAnhwwtnpvk18L5wvtincnLU8oA7LNZg5YQK8iM/V@cAXbrgAZriEGhROVXq0QojTF5DAamCKuANEA2sQAdZlEJUBWE9Km7doI3FbykQ0vxYDoT3RilI0GeEs6V7SSpNLl7GWYjQd6pyhSGwISzD6yKqif8sUev4rjR@29p4dthbGcU@1ifgB "R – Try It Online")
or...
# [R](https://www.r-project.org/), 5 bytes
```
match
```
[Try it online!](https://tio.run/##dY7dCsIwDIXvfYqCDBLIYF33V3API3VjBVdlq7i3r22giBfeJed8OSdbWMWlFGG9erOE52adhxUMIAkDkmpScarS0iLi6QtIEjUJxdwfRJNoIkKiy2BUBhI9K212Y4yMbikT0fxGDIz2TCtu0RwUZ8n3ki3NKV3GWq7RfKhzh2KxYSzBMUdWFf8tU@l5fjnj7cPBQZ4cWQQ7T/d9gqOwrtgnf7PzDD4a9F6sWcCP44HkEMMH "R – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~9~~ 8 bytes
```
ṣK¥ƒiⱮo⁶
```
[Try it online!](https://tio.run/##y0rNyan8///hzsXeh5Yem5T5aOO6/EeN2/7//x9trKNgqKNgAiZNdRQsdRSMdBTMwGxjMGkBFjQHk8ZgWWOwoAlYGUhxLFDz/2gLMN8cptMArMEw9r@hgYEBAA "Jelly – Try It Online")
A full program that takes three arguments: `[[table], incomparables]`, `x`, `nomatch` in that order.
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), 60 bytes
```
(a,b,c,d)=>a.Select(x=>d.Contains(x)|(x=b.IndexOf(x))<0?c:x)
```
[Try it online!](https://tio.run/##pU9Na4NAEL37K/aoMMquq1lt1R5KC0KhhR7Fg58gpFuIhgqtv92OEyUJ6a2X4c2bN@/NVL1d9d38fNRVlL50/RB1ekjgAmK5bNMnffxoDkW5b4hI2ng2CyihgtqKk8J5b/ZNNZhjnNTO46ceik735mj9IFM6qa6b8bXF3or4Q3U3WvO98XZAI7M1dfPFEGU8B4RZ/i3ABTkBh/PEsq70Z9HFMl/X/elWHYIHEnbrBhEBKCR8GrggJrDFOvP@MAhQq1AtMSbcTCQIYB4wrD6wEJgLbEdYUg2IVFQlTSWRHskW8QSC8@1wseTOWQ4so@8QcMR@zuyEZblhIE0mi@J6yKnjiyYkf0kRpAwpU22nedstAof4M8u8k4c4tYokarEKaEdtH2FCuBhK@vhff6MNPY6p@S8 "C# (Visual C# Interactive Compiler) – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 44 bytes
Zero-indexed.
```
->x,t,n,i{x.map{|e|i-[e]==i&&t.index(e)||n}}
```
[Try it online!](https://tio.run/##XY5BDoIwEEX3nGIWxqgZCBVQusCLNMRUbLQJEIOoGODsOJ3Axt3kvf9/27wu36nSbXGHbPJPHbZYo@27oNKPfjCD9ZXJs8yu121g66vpNmY7DPU4Tp@7LQ3cTPv0AB660RVkYN66hNXZEeDVoNBludmx33qmvk4KVI6gBO4xoiOkO8kh95RjsEdwVP0ZiRCTQTjMnkCKcGSQLJLKgqRPOyqemylHjpyKeFS6Pp2Ca4KN5PJhSSW8Lrknl@mIYcwxF6YZEYbul4Le@gE "Ruby – Try It Online")
[Answer]
# [R](https://www.r-project.org/), 79 bytes
```
function(x,t,n,i)sapply(x,function(y)`if`(any(z<-y==t)&all(y-i),which(z)[1],n))
```
[Try it online!](https://tio.run/##dY7NCoNADITvfYo9lQQiGP/qQn2SUlAEcUG2Yi3t@vI2Bmzpobdk5stMprWr1u7h29ndPLxoJk8O7804DkHWjxOwdl0NjQ@wnKNQVTMem2GAEDmkZ@/aHha88JU84jpOzs/QQQtIpgWmhFKZ4m3JEfHwBZhMQiZV7g9iyWSCkCl2UJSSzEmVfHclhsWNeCOy34hS0ZPSqbZYDZKZ9Z7VsppS7FiuNVYP7d6RqpgptsGSw3Gsf7OUrm8 "R – Try It Online")
# [R](https://www.r-project.org/), 66 bytes
```
function(x,t,n,i)sapply(x,function(y)c(which(y==t&all(y-i)),n)[1])
```
Combining my solution with parts of [Nick's solution](https://codegolf.stackexchange.com/a/188182/59530).
[Try it online!](https://tio.run/##dY7NCsJADITvPsWeZAMpNN3@LdgnEQ9lQbrQrqVWdJ@@poFVPHhLZr7MZNkmdcrUdn0Et/pb0C9cMaCHez/PY@T140Rw@jl4N@jYdeuxH0cdMw@AAc50gW1efFj1pJ0GVE4TFmh4yvelAoDDFyBUBSoj3B/EoioZQVUnkJUWVSNKlVyOIXYz2onyN6IVtBHaSIuVIJ5J7kksKyl1wiqpsXJoU4cRsRRshzmH8lz@Ji7d3g "R – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 14 bytes
```
Ë!XøD ©ÒVbD ªW
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=yyFY%2bEQgqdJWYkQgqlc&input=WzgsIDYsIDcsIDUsIDMsIDAsIDldClszLCAxLCA0LCAxLCA1LCA5LCAyLCA2LCA1LCAzLCA1LCA4LCA5LCA3LCA5LCAzLCAyLCAzLCA4LCA0LCA2LCAyLCA2XQoxMDAwClsxXQ)
[Answer]
# [Python 3](https://docs.python.org/3.8/), 60 bytes
```
lambda x,t,n,i:[v in{*t}-{*i}and-~t.index(v)or n for v in x]
```
[Try it online!](https://tio.run/##bY7dCoJAEEbve4q5VBnD9SfdoCfZvDAsWshVZBFD7NW32SGDoJtlmHPm@3Z42ntvsmoYXQcnOLtH013aBma0aFAf1QTaLJFd4yXSa2Pa@GX32rTXOZjCfgQDN3q9BHPthlEbG3SBqhGUwBQzGhKaizoMd18qEFIEz9RfLhFy4giHj0WLCqHkRbFBihAEY0pT@c99xWLJbsYF0qfQKPhYMJEccdisgjsk38mtIONlzpqXKUYkif@xoEb3Bg "Python 3.8 (pre-release) – Try It Online")
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 63 bytes
```
param($x,$t,$n,$o)$x|%{($n,$t.indexof($_))[$_-in$t*!($_-in$o)]}
```
[Try it online!](https://tio.run/##TY/hCsIwDIT/7ykiRm0kk9Vtzv5QfA8REdxQ0E104EB99plGh0ILae@7u/ZS3fPr7ZCfTi0Wi0d72V13Z4MNY81YMlaEzXPwMH6uJ8dynzdVYXBLtMZteCyxHvfMZ6po82pfQTDEAlaGZFueckwQyZhSMBp9JcswZYhJqY8I/XAJ64jBr80PdQyJoAwzNfjznCHTc9ppEmZFC61PTL5ZUhJaRQXK/iLnas7UH2uhE7NMVlGrgtPUWQelWuvU5rrOWC8TxTxMYKPIf0fe0b4B "PowerShell – Try It Online")
0-indexed
[Answer]
# JavaScript (ES6), 53 bytes
```
(x,t,n,i)=>x.map(v=>!~i.indexOf(v)*-~t.indexOf(v)||n)
```
[Try it online!](https://tio.run/##bY9NboNADIX3PYW7m6kMGfObWcAVcoARC5RARZQOUYMQiyhXJ8YtEmq68zx/7z3PuR7r2/G7uw6B70/N3BazmnBAj50uyin8qq9qLMr3Rxd2/tRMh1aN@iN4DJvn/e71fOz9rb804aX/VK1yFYIjjDDmwfCcVlrDbgdBCa56@wMTQoSwoO4f3IhkXlwWIWEXQvbrZWGPkIuQrksOJl4G3OGSTSr9aLlw@Uv4XlJyCYrlALtU8EjiINlYyc9WKpUDrPjs2h6LmAi2wBxDxiyfpO05kWCUSAqtTFbNTw "JavaScript (Node.js) – Try It Online")
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 45 bytes
```
->\b,\c,\d{*>>.&{$_∉d&&~b.first($_,:k)||c}}
```
[Try it online!](https://tio.run/##VY67boNAEEV7f8XIQngXLetdniZRiFKkS@0GVpZfSCg4QeDGwqROfjM/QoaxKUI1e@@ZM9THpoqG0wXsAp4GN813It@L/NA5aSrtztr8fv8cbPtrJ4uyac/M2oiHd3697vt@KD4bYFX5cWy5PG1r5si2rsozW4CbLnimDIduBvgtmXSec8OlxIEvHylstxdg84JZmjNL8TmXr@uXt1k/ZEZApoUnfBwUzqFBIWRmhil4AsY8@98peilEEgEBIgKiO4jBSkBMQTiVaNFYuijMgptC354xITGaVrQS05ZP/mT04aiJ0dQkJIsmKqRrCe0l0ymfwoCwEUaNVmr8fX2/7RGhAxLoqY7MHw "Perl 6 – Try It Online")
Anonymous code block that takes input curried, like `f(table, nomatch, incomparables)(x)` and returns the matches 0 indexed.
### Explanation:
```
->\b,\c,\d{                               }    # Anonymous code block taking 3 inputs
           *           # Return an anonymous Whatever lambda
            >>.&{                        }  # Mapping input to
                 $_∉d                       # If the element is not an uncomparable
                     && b.first($_,:k)      # Return the first index in the table
                       ~                    # Stringified so Nils are false
                                      ||c   # Else the nomatch element
~~
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 14 bytes
```
ＩＥθ∨∧¬№ει⊕⌕ηιζ
```
[Try it online!](https://tio.run/##Nco9C4MwEMbxr3LjBc7Bqn2hUxEKHfqyS4YQAwZsomnaoV/@vApdnuH/e@xgko1mZH4kHzK25pXxaiacCe4JT6HHW5Qc34KOwCtFcAk2uacL2fV49nIZVhD5yqojc9cdCGqCimArqwl@YU@wW0Pzxw1BKViUcqi15uIzLg "Charcoal – Try It Online") Link is to verbose version of code. 1-indexed. Explanation:
```
  θ             First input (x)
 Ｅ              Map over elements
       ε        Fourth input (incomparables)
      №         Count occurrences of
        ι       Current element
     ¬          Is zero
    ∧           Logical And
           η    Second input (table)
          ⌕     Find 0-based index of
            ι   Current element
         ⊕      Convert to 1-indexed
   ∨            Logical Or
             ζ  Third input (nomatch)
Ｉ               Cast to string
                Implicitly print on separate lines
```
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 125 bytes
1-indexed.
Given that I can't use a sentinel value in the passed arrays, I need to give the array bounds for each array.
```
f(x,c,t,d,n,i,e,j,f)int*x,*t,*i;{for(;f=0,c-->0;x[c]=--f?n:j){for(j=e;!f&j;x[c]-i[--j]||--f);for(;!f&j<d;x[c]-t[j++]||++f);}}
```
[Try it online!](https://tio.run/##pVLJbtswEL3rKxgXCbQMC8pbotJyfyDorSdBB4eSEhK2FDh0IcDRt7tDanFdK7kEBgz6vXkLhxb0WYjTqXBrEKAhgxIk5KCg8GSp/Rp8Db7kx6Lau7yIGQhK14zXiUhjSouf5Q/lWVLFOb8p7pSlqEwoVen7O4543GoNt8paVicqCJANAmSb5vRNlmJ7yHKyetOZrL6/rJ0LaCufDOZgI7LbyNL9U8nMI0eHEITwmxC/TtL4aI@E/Pr9@Ajd2cWJJPWOIUxh1vyPRjCHGSxHmAdE72GBLIOosWQ34@vN0zb/J248YqxEBA9oukRbEzuF8Cp2BiFyIU5EyC9tgQWqItRFVjPDX3NkkL2oVVa7jRYvphfDzjSEkDHWd8Z1VrvXkdKLqwrXyPwKCS@iRd2mmqr3mNzDw6LsEmHaL0ec22BL82m4g4z5o5g3ljHjxlSmaxpyIoOgfWxCXvfIF@6kjpOJxy3Ui5QRqZWV4cFouum7Cdxmk@RGpUAMm6i00/Z2SNiuH7u2V/nMuZ8Yde8eJ77NgLSXx6gBRtUHsd2iPssdRkaCCV0Te6XW3DW3B7sh6OvCcDU4t4HBFM4VBpuvLPyg37BXu@XG@O1zfdiXhHGnOf0F "C (gcc) – Try It Online")
[Answer]
# [Attache](https://github.com/ConorOBrien-Foxx/Attache), 39 bytes
```
${{[_,y][nil=_or x@_in z]}=>x&Index@_4}
```
[Try it online!](https://tio.run/##hY9Ba8JAEIXv@RVzEE8jZE2ijaAWeuqlFNrbMoRtXGtAN2L2EBV/ezo7NaCUtrlk8ua9702M96bc2G65hLej86adwVp787G1CK7eGV9uECpX1ru9OQS5QYAWKOLA7dNe38cb7dQPRRqtYTbvBuezLvBI2lXbeVEfoH0sKgcnuswX7fDZrSwL6aV7sXbV6MF@X35SFK139Uq3CH9eRcyPQte/17eMfD1Uzr/bxj@ZxjZ6GCoQtAC0Jh4VjjHhIeY5I0JZ8T@PFqBJPq5mhTBGCFb9iz0WOb5L5QgppxAm1ywLDwhTEbJ@yWDFyxF36PQnNeiTb8od/EHkqYASKc9DBY9KyEo2ufAnvSuTA3LJ5X17ImIqtmBmjIrj8JOK6PYapSSoEnHHvS2jiKj7Ag "Attache – Try It Online")
Pretty simple verification. Note that the argument order differs from that of match; specifically, `x` is the last argument rather than the first, and corresponds to `_4` in the above snippet.
## Explanation
```
${{[_,y][nil=_or x@_in z]}=>x&Index@_4}
${                                    }   named lambda, taking parameters x, y, z, and _4
                            x&Index@_4    short for Index[x, _4];
                                              calculates where each element in _4 occurs in x
                                              returns `nil` for no match
  {                      }=>              over each index:
   [_,y][               ]                     choose y (`nomatch`) if
         nil=_                                    the index is nil
              or x@_in z                          or the element is in `incomparables`
    _                                         otherwise, choose the index
```
[Answer]
## Haskell, ~~57~~ 56 bytes
```
(t#n)i=map$maybe n id.($zip i[n,n..]++zip t[1..]).lookup
```
Argument order is: `table`, `nomatch`, `incomparables`, `x`. 
[Try it online!](https://tio.run/##ZY7NDsIgEITvPsUm7YGmKwHp76FPQjhg2kTSlhKtB315XDTx0sMmzH4zs9zsY56WJUa2Z75ww2pDvtrXdQIPbuQsf7sATnv0nJuyTGrXkt4FX7Ztfoa4WudhgHE7AUC4O79DDiwrQEu8oDIgQNcGtDnwP/oZD7zHDltssMYKFVmkAXaWBCqTYFo26cAhqFASlRTsKZYKFE1HqqVJVYpURYSoASkEfYTadUer9msX2Jv4AQ "Haskell – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 7 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
õ:Ik®I:
```
0-indexed. Inputs are in the order: `incomparables, table, x, nomatch`.
[Try it online.](https://tio.run/##yy9OTMpM/f//8FYrz@xD6zyt/v@PNonlirbUUbDQUTDXUTDTUTDVUTDRUTDWUTDSUTCEyEH4QDnjWC5dS0sA)
**Explanation:**
```
õ:       # Replace all values of the (implicit) first incomparables-list in
         # the (implicit) second table-list with an empty string
         #  i.e. incomparables=[4] and table=[9,8,7,6,5,4,3,2,1] → [9,8,7,6,5,"",3,2,1]
  Ik     # Get the index of each value in the third x-list in this list (-1 if not found)
         #  i.e. x=[9,4,3,6,3] → [0,-1,6,3,6]
    ®I:  # Replace all -1 with the fourth input-integer
         #  i.e. nomatch=-99 → [0,-99,6,3,6]
         # (and output the mapped list implicitly as result)
```
]