[Question]
  [
I was messing around with infinite resistor networks (long story) when I came across the following interesting recursive pattern:
```
|-||
|---
```
Each instance of this pattern is twice as wide as it is tall. To go from one level of the pattern to the next, you break up this rectangle into two sub-blocks (each of which is a NxN square):
```
AB =
|-||
|---
so A = 
|-
|-
and B = 
||
--
```
These halves are then duplicated and rearranged according to the following pattern:
```
ABAA
ABBB
giving
|-|||-|-
|---|-|-
|-||||||
|-------
```
**Challenge**
Write a program/function which, given a number `N`, outputs the `N`th iteration of this recursive design. This is golf.
I/O format is relatively lenient: you may return a single string, a list of strings, a 2D array of characters, etc. Arbitrary trailing whitespace is allowed. You may also use either 0 or 1 indexing.
**Examples**
The first several iterations of the pattern are as follows:
```
N = 0
|-
N = 1
|-||
|---
N = 2
|-|||-|-
|---|-|-
|-||||||
|-------
N = 3
|-|||-|-|-|||-||
|---|-|-|---|---
|-|||||||-|||-||
|-------|---|---
|-|||-|-|-|-|-|-
|---|-|-|-|-|-|-
|-||||||||||||||
|---------------
N = 4
|-|||-|-|-|||-|||-|||-|-|-|||-|-
|---|-|-|---|---|---|-|-|---|-|-
|-|||||||-|||-|||-|||||||-||||||
|-------|---|---|-------|-------
|-|||-|-|-|-|-|-|-|||-|-|-|||-|-
|---|-|-|-|-|-|-|---|-|-|---|-|-
|-|||||||||||||||-|||||||-||||||
|---------------|-------|-------
|-|||-|-|-|||-|||-|||-|||-|||-||
|---|-|-|---|---|---|---|---|---
|-|||||||-|||-|||-|||-|||-|||-||
|-------|---|---|---|---|---|---
|-|||-|-|-|-|-|-|-|-|-|-|-|-|-|-
|---|-|-|-|-|-|-|-|-|-|-|-|-|-|-
|-||||||||||||||||||||||||||||||
|-------------------------------
```
I wonder if there is some short algebraic way to compute this structure.
  
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), ~~29~~ 25 bytes
```
'|-'[{a,‚äñ‚åΩ‚çâ~a‚Üê‚ç™‚ç®‚çµ}‚ç£‚éï‚çâ‚ç™‚ç≥2]
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob2pm/qO2CQZcjzra0/6r1@iqR1cn6jzqmvaoZ@@j3s66RKDko95Vj3pXPOrdWvuodzFQB1AcLLTZKPY/UNv/NC4TAA "APL (Dyalog Classic) ‚Äì Try It Online")
`‚ç≥2` is the vector `0 1`
`‚ç™` turns it into a 2x1 matrix
`‚çâ` transposes it, so it becomes 1x2
`‚éï` evaluated input
`{` `}‚ç£‚éï` apply a function that many times
`‚ç™‚ç®‚çµ` concatenate the argument on top of itself - a 2x2 matrix
`a‚Üê` remember as `a`
`~` negate
`‚çâ` transpose
`‚åΩ` reverse horizontally
`‚äñ` reverse vertically
`a,` concatenate with `a` on the left
`'|-'[` `]` use the matrix as indices in the string `'|-'`, i.e. turn 0 into `|` and 1 into `-`
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~130~~ ... ~~106~~ ~~94~~ 92 bytes
Golfed from my alternative method and fixing the characters, -14 bytes Thanks @Shaggy
```
f=n=>n?f(n-1).replace(/.+/g,x=>(g=i=>x.replace(/./g,p=>p<i?s[i]+s[i]:s))`0`+`
`+g`1`):s="|-"
```
[Try it online!](https://tio.run/##TcmxDsIgEIDh3cfodBeE2sSp8eiDNCZHEAiGACnGdOi7o24u//D9T/M2zW6xvmQuD9e7p0w6Lx6ynFBtriZjHYxKjOG8k4ZAkfT@N75eSddbXNoa7@KXuSHyhQWfWASeGOdGwyGHbktuJTmVSgAPV8T@AQ "JavaScript (Node.js) ‚Äì Try It Online")
**My original approach (~~106~~ 102 bytes)**
```
f=n=>n?[0,1].map(j=>f(n-1).split`
`.map(x=>x+x.substr((i=x.length/2)*j,i).repeat(2)).join`
`).join`
`:"|-"
```
-4 bytes Thanks @Shaggy
```
f=n=>n?[0,1].map(j=>f(n-1).split`
`.map(x=>x+(y=x.substr((i=x.length/2)*j,i))+y).join`
`).join`
`:"|-"
```
[Try it online!](https://tio.run/##PctBDsIgEEDRvcfoasYWtLozAQ9iTIoVKgQHUtDQxLtj48Ldy0@@U2@VxtnGzCjcda1GkJB0vuy7/sqfKoIT0gCxHnmK3uZhM/xyEbK0sIjC0@uW8gxgV3tNU37sDrh1nUVsF@QuWFqnP07NhzV1DJSC19yHCQwcEesX "JavaScript (Node.js) ‚Äì Try It Online")
Explanation & Ungolfed:
```
function f(n) {                     // Main Function
 if (n != 0) {                      //  If n != 0: (i.e. not the base case)
  return [0, 1].map(                //   Separate the pattern into 2 parts
  function(j) {                     //   For each part:
   return f(n - 1).split("\n")      //    Split the next depth into lines
    .map(function(x) {              //    For each line in the result:
    return x                        //     The common part: "AB"
     + x.substr(
      (i = x.length / 2) * j        //     Take A if j == 0, B if j == 1
      , i                           //     Take half the original length
     ).repeat(2);                   //     Double this part
   }).join("\n");                   //    Join all lines together
  }).join("\n");                    //   Join the two parts together
 }
 else return "|-";                  //  If not (base case): return "|-";
}
```
**My original alternative method, if `"|"->"2", "-"->"1"` is allowed, ~~105~~ 104 bytes:**
```
f=n=>n?f(n-1).replace(/[12]+/g,x=>(g=(y,i)=>y.replace(/1|2/g,p=>[,i?11:22,21][p]))(x,0)+`
`+g(x,1)):"21"
```
[Try it online!](https://tio.run/##RcnRCoMgFADQ931GT/ei1a7sqdA@JITEqThCpcYo2L@73vZ24LzMx@x2i@Xdpvx0tXqZpEqTh9QSdpsrq7EO@pmEZn3gh1QQJJw8olTn/@krri1SzTxORIMQXJCei0aEg9@RLbeFhYuEODSCmjranPa8um7NATw8EMf6Aw "JavaScript (Node.js) ‚Äì Try It Online")
**Just figured out some algebraic method towards this problem.**
```
x=>y=>"|-||--"[(f=(x,y,t=0,m=2**30,i=!(y&m)*2+!(x&m)<<1)=>m?f(x^m,y^m,([18,0,90][t]&3<<i)>>i,m>>1):t)(x>>1,y)*2+x%2]
```
[Try it online!](https://tio.run/##PYzhaoMwFIX/5ynSwMqNxhEVxjpzswdxDqStEmlMUSkJc8/uIoX9uHznng/O0D7a@TyZ@5KN7nLdetw86oCardm6ZhmroUPwIogFpbBYJEkphcEDhKPlSZEewMegVM5R288O/LcVIR7U@buQ4iSbemmOpVKGa22E1TrnHwsHH4MI@4J/KZrt0U50pkgZq0jnJgp7McRCVhGK5m@RacrpD6H/3jy9ib4sIndPaNxJkfZgOAy8Is@XfY1x@Zec3Ti72/X15nqYebX9AQ "JavaScript (Node.js) ‚Äì Try It Online")
(finally a function which length's comparable to my original answer)
`f(n, x, y)` calculates the block type at (x, y) block at `n`th iteration of the following substitution:
```
0 => 0 1      1 => 0 0      2 => 1 1
     0 2           0 0           2 2
```
where `0 = "|-", 1 = "||", 2 = "--"`, starting from `f(0, 0, 0) = 0`.
Then, `g(x)(y)` calculates the symbol at (x, y) of the original pattern.
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 24 ~~17~~ 15 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
‚ïõ√§‚îú¬º√†z[{‚ïß‚Üë;Œµ‚ïñ>‚ï†
```
[Run and debug it](https://staxlang.xyz/#p=be84c3ac857a5b7bcf183beeb73ecc&i=3%0A5&a=1&m=2)
Here's the ascii representation of the same program.
```
'|'-{b\2*aa+c\}N\m
```
The basic idea is start with the 0-generation grid, and then repeat a block that expands the grid.
```
'|'-                    Push "|" and "-"
     {         }N       Get input and repeat block that many times.
      b                 Copy two top stack values
       \2*              Zip two parts, and double the height
          aa            Roll the top of the stack down to 3rd position.
            +           Concatenate two grids vertically
             c\         Copy result and zip horizontally
                  \     Zip the two parts horizontally
                   m    Output each row
```
[Answer]
# [Canvas](https://github.com/dzaima/Canvas), ~~17~~ 16 [bytes](https://github.com/dzaima/Canvas/blob/master/files/chartable.md)
```
|‚àô-‚ï∂Ôºª‚àîŒ±œâÔºãÔºö‚àîÔºõÔºöÔºãÔΩùÔºã
```
[Try it here!](https://dzaima.github.io/Canvas/?u=JTdDJXUyMjE5LSV1MjU3NiV1RkYzQiV1MjIxNCV1MDNCMSV1MDNDOSV1RkYwQiV1RkYxQSV1MjIxNCV1RkYxQiV1RkYxQSV1RkYwQiV1RkY1RCV1RkYwQg__,i=Mg__,v=1)
Explanation, showing the stack for the input of 1:
```
|‚àô-               push "|" and "-" - the initial halves  "|", "-"
   ‚ï∂[         }   repeat input times                     
     ‚àî              add the two parts vertically         "|¬∂-"
      Œ±œâ            get the original arguments to that   "|¬∂-", "|", "-"
        +           and add those horizontally           "|¬∂-", "|-"
         :‚àî         and add to itself vertically         "|¬∂-", "|-¬∂|-"
           ;        get the vertically added parts       "|-¬∂|-", "|¬∂-"
            :+      and add to itself horizontally       "|-¬∂|-", "||¬∂--"
               +  finally, add the halves together       "|-||¬∂|---"
```
Updated to 16 bytes by fixing a bug where the values set for `Œ±`/`œâ` to work weren't copied properly (Canvas is supposed to be fully immutable, but, alas, it wasn't).
[Answer]
# [Python 2](https://docs.python.org/2/), ~~88~~ 77 bytes
-11 bytes thansk to Lynn
```
f=lambda x:x<1and['|-']or[n+2*n[i:i+2**x/2]for i in(0,2**x/2)for n in f(x-1)]
```
[Try it online!](https://tio.run/##NcxBDsIgEAXQq8xuoNJqSdwQewUvgCwwFR1jh4Z0QRPvjlTjZn7@y8/M6/KIrEsJw8tP19FDNvnUex4tvlt0MVne6YYtGarZ5L12ISYgIBYH9RO5CVeBIHLbS1f@E0ie7zdxlGZOxAvgGQZUpPDC@D3dM9ZHQZCUW69SPg "Python 2 ‚Äì Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/), 72 bytes
```
@1='|-';$l=@1,map{/.{$l}/;push@1,$_.$' x2;$_.=$&x2}@1for 1..<>;say for@1
```
[Try it online!](https://tio.run/##K0gtyjH9/9/B0Fa9RlfdWiXH1sFQJzexoFpfr1olp1bfuqC0OAMopBKvp6KuUGFkDWTYqqhVGNU6GKblFykY6unZ2FkXJ1YqAHkOhv//m/zLLyjJzM8r/q/ra6pnYGgAAA "Perl 5 ‚Äì Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 17 bytes
```
!¬°¬ßz+D»Ø·πÅmDTm¬Ω;"|-
```
1-indexed.
[Try it online!](https://tio.run/##ASEA3v9odXNr//8hwqHCp3orRMiv4bmBbURUbcK9OyJ8Lf///zU "Husk ‚Äì Try It Online")
## Explanation
```
!¬°¬ßz+D»Ø·πÅmDTm¬Ω;"|-  Implicit input: a number n.
              "|-  The string "|-".
             ;     Wrap in a list: ["|-"]
 ¬°                 Iterate this function on it:
                    Argument is a list of lines, e.g. L = ["|-||","|---"]
           m¬Ω       Break each line into two: [["|-","||"],["|-","--"]]
          T         Transpose: [["|-","|-"],["||","--"]]
      »Ø·πÅ            Map and concatenate:
        mD           Map self-concatenation.
                    Result: ["|-|-","|-|-","||||","----"]
   z+               Zip using concatenation
  ¬ß  D              with L concatenated to itself: ["|-|||-|-","|---|-|-","|-||||||","|-------"]
                   Result is the infinite list [["|-"],["|-||","|---"],["|-|||-|-","|---|-|-","|-||||||","|-------"],...
!                  Take n'th element, implicitly display separated by newlines.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~21~~ 19 bytes
```
;"/;`,·∫é;`‚Ç¨$
‚Åæ|-√á¬°ZY
```
[Try it online!](https://tio.run/##y0rNyan8/99aSd86Qefhrj7rhEdNa1S4HjXuq9E93H5oYVTk///GAA "Jelly ‚Äì Try It Online")
---
Explanation:
Initially, the value is `‚Åæ|-`, that is `["|","-"]`.
The last link (`√á`), given `[A, B]`, will return
```
   AB     AA
[  AB  ,  BB  ]
```
. The `¬°` repeatedly apply the last link (input) number of times, and `ZY` formats it.
Explanation for last link:
```
-----------------
;"/;`,·∫é;`‚Ç¨$  Monadic link. Value = [A, B]
;"/          Accumulate vectorized concatenate. Calculates (A ;" B).
             Represented as a matrix, it's |AB| (concatenated horizontally)
   ;`        Concatenate with self.      |AB|
                                Value =  |AB|  (concatenate vertically)
     ,    $  Pair with ...
      ·∫é        Tighten.  |A|    (concatenate vertically)
                 Value = |B|
       ;`‚Ç¨     Concatenate each with self.    |AA|
                                      Value = |BB|  (duplicate horizontally)
```
[Answer]
# [Clean](https://clean.cs.ru.nl), ~~121~~ 106 bytes
```
import StdEnv
$0=[['|-']]
$n#z=map(splitAt(2^n/2))($(n-1))
=[u++v++u++u\\(u,v)<-z]++[u++v++v++v\\(u,v)<-z]
```
[Try it online!](https://tio.run/##TY6xCsIwFEX3fkXAQBNiUIujGQQdhG4d2wpprRJIXotNCha/3WdEB@Fy4J7l3tZ2GtD1l2A74rQBNG7o754U/nKEKaFrVZbpU6Z1nVBYzMrpgY2DNX7vWXaGVcY5owzkhvNElUGISYjIUFUsLCe@k3MtxM9/8uex8DouKXK12lsD3cgIJVvC8dVGdRtRnnI8PEA7035LfJWbBmXzBg "Clean ‚Äì Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 86 bytes
```
(%)=zipWith(++)
f 0=["|-"]
f n|(a,b)<-unzip$splitAt(2^(n-1))<$>f(n-1)=a%b%a%a++a%b%b%b
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/X0NV07YqsyA8syRDQ1tbkytNwcA2WqlGVykWyMyr0UjUSdK00S3NA6pRKS7IySxxLNEwitPI0zXU1LRRsUsDs2wTVZNUE1UTtbVBDCD8n5uYmadgq5CbWOAbr1BQWhJcUuSTp6CikKZg8h8A "Haskell ‚Äì Try It Online")
Pretty simple. Output is a list of strings. We take the previous version and split each line in half then collect those into two new lists using `unzip`. Then it's simply a matter of combining the arrays together the right way
[Answer]
# [J](http://jsoftware.com/), 49 bytes
```
f=.3 :'''|-''{~((,.[:|.[:|."1[:|:-.)@,~)^:y,:0 1'
```
A clumsy translation of ngn's APL solution. I had problems making it tacit - I'll apreciate any advice.
[Try it online!](https://tio.run/##y/r/P81Wz1jBSl1dvUZXXb26TkNDRy/aqgaMlQyBpJWunqaDTp1mnFWljpWBgqH6/9TkjHyFNAUDLjBDXV2BCypiCBOBCRihCxijC5igC5j@BwA "J ‚Äì Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~47~~ 46 bytes
```
Ôº≠¬≤‚Üñ|-¬∂¬∂Ôº¶Ôº•ÔºÆÔº∏¬≤Œπ¬´Ôº¶¬≤Ôº£¬±Œπ‚Å∞Ôº£‚Å∞ŒπÔº£‚äóŒπ¬±ŒπÔº£√ó¬≥Œπ¬±Œπ‚â¶‚äóŒπÔº≠¬±Œπ¬±ŒπÔº¥‚äóŒπŒπ
```
[Try it online!](https://tio.run/##XY69DoIwFIVneIqGqU0gIRgXnYwuGiEMuLEAFmxCf1JajFGfvRYiRNzuvec759zqVsiKF60xMe8xjHywuYgzrhXauqkkTEHvFeQsZ5491FwCGBcCHpnQKtG0xBIiH6T8bgfrJQgh8HSdEYwQ2HPxgAluCoUhsWBoQ5zxGA7wtBy4Llt8HZGZntWMUNzB1WD4k@0ru64jDZsSvqEnTcVv7cKUSUIXjYPlbczaBH37AQ "Charcoal ‚Äì Try It Online") Link is to verbose version of code. Explanation:
```
Ôº≠¬≤‚Üñ|-¬∂¬∂
```
In order to get a consistent cursor position for the following loop, I have to print step 0 at position (-2, -2) and leave the cursor at (-2, 0). (This might be due to a bug in Charcoal.)
```
Ôº¶Ôº•ÔºÆÔº∏¬≤Œπ¬´
```
Loop over the first `N` powers of 2.
```
Ôº¶¬≤Ôº£¬±Œπ‚Å∞Ôº£‚Å∞ŒπÔº£‚äóŒπ¬±ŒπÔº£√ó¬≥Œπ¬±Œπ
```
Make copies of the previous output with various offsets resulting in a canvas that contains the desired next step in a rectangle within it.
```
‚â¶‚äóŒπÔº≠¬±Œπ¬±ŒπÔº¥‚äóŒπŒπ
```
Move to the position of that rectangle and trim the canvas.
Alternative solution, also 46 bytes:
```
Ôº≠¬≤‚Üí|-Ôº¶Ôº•ÔºÆÔº∏¬≤Œπ¬´Ôº¶432Ôº£√óÔº©Œ∫Œπ√ó¬≥ŒπÔº¶245Ôº£√óÔº©Œ∫Œπ‚äóŒπ‚â¶‚äóŒπÔº™‚äóŒπŒπÔº¥‚äóŒπŒπ
```
[Try it online!](https://tio.run/##fY69DoIwFEZneYqG6TaBhZ9FJoOLJhhieIGCVRopJS3FGPXZa/kxDiZu9zs5J7lVTWQlSGNMJgYKgYfWR3ape5w4uWRtD@7Td@04C4kgIx3s2k73B81LKgF7KBc3e9iMYYzRw1lNohuFgYtRKro7FIxTBSlRPVzx6HloRuEUJZ8kiOI/yVbosqEnmAv7yEYpdmlh4aNk@V7zrhDwlRdeSMZ/6MuY2PhD8wY "Charcoal ‚Äì Try It Online") Link is to verbose version of code. Explanation:
```
Ôº≠¬≤‚Üí|-
```
This time step 0 has to be printed at position (2, 0), but at least the cursor position doesn't matter.
```
Ôº¶Ôº•ÔºÆÔº∏¬≤Œπ¬´
```
Loop over the first `N` powers of 2.
```
Ôº¶432Ôº£√óÔº©Œ∫Œπ√ó¬≥ŒπÔº¶245Ôº£√óÔº©Œ∫Œπ‚äóŒπ
```
Make copies of the previous output with various offsets resulting in a canvas that contains the desired next step in a rectangle within it.
```
‚â¶‚äóŒπÔº™‚äóŒπŒπÔº¥‚äóŒπŒπ
```
Move to the position of that rectangle and trim the canvas.
[Answer]
# [R](https://www.r-project.org/), 126 bytes
```
function(n,k=cbind){o=matrix(c("|","-"),1,2)
if(n>0)for(i in 1:n)o=rbind(k(a<-o[,x<-1:(2^(i-1))],b<-o[,-x],a,a),k(a,b,b,b))
o}
```
[Try it online!](https://tio.run/##HY7BCsMgEETv@QrxtFtWqB5D0h8pCWhaYQldQSwV2n67NWUuw/BmmNyimkyLT9kKJwGhfd4Cyw3faX74krnCBvqjSRuNZMnhwBHkcsaYMrBiUXYUTHM@WrCDn0y6Up2MHcGtwMYiLhT@qakLefJIHaNwCHFI3yb9g3LDK3O5Q4G@j0hakzu5Vag7bD8 "R ‚Äì Try It Online")
Returns a `matrix`. There's a bit of code in the TIO link to get it to print nicely for ease of verification.
[Answer]
# [K (ngn/k)](https://gitlab.com/n9n/k), 25 bytes
```
{"|-"x{x,'|+|~x:x,x}/,!2}
```
[Try it online!](https://tio.run/##y9bNS8/7/z/NqlqpRleporpCR71Gu6auwqpCp6JWX0fRqPa/gZ@iepq6oqn1fwA "K (ngn/k) ‚Äì Try It Online")
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), ~~67~~ 65 bytes
```
SubstitutionSystem[{"|"->{i={"|","-"},i},"-"->{i,i}Ôèá},{i},{#}]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7P7g0qbgks6S0JDM/L7iyuCQ1N7paqUZJ16460xbE0FHSVarVyawF0SBBIPN9f3utTjVQqFq5Nlbtf0BRZl6Jg4ODspq@Q5qDa3JGPpgZlJiXnhptoGMS@x8A "Wolfram Language (Mathematica) ‚Äì Try It Online")
Straightforward implementation of the recursion. Returns an array of characters wrapped in a list.
]