[Question]
  [
# Challenge
Create a function or program that, when given an integer `size`, does the following:
If `size` is equal to 1, output
```
H H
HHH
H H
```
If `size` is greater than 1, output
```
X X
XXX
X X
```
where `X` is the output of the program/function for `size - 1`
(If you prefer, you may have the base case correspond to `0`, so long as you specify in your answer)
Any of the following output formats are acceptable, whichever is more convenient for you:
* A string of the required structure with any two distinct characters corresponding to `H` and `space`
* A two-dimensional array with the required structure, with any two distinct values corresponding to `H` and `space`
* An array/list of strings, with one line of the output in each string, with any two distinct values corresponding to `H` and `space`
Leading spaces are allowed, as long as there is a constant amount of leading spaces on each line. The two distinct output characters can be dependent on anything you choose, as long as they are different.
Specify what output format your code is returning.
# Test Cases
`1`
```
H H
HHH
H H
```
`2`
```
H H   H H
HHH   HHH
H H   H H
H HH HH H
HHHHHHHHH
H HH HH H
H H   H H
HHH   HHH
H H   H H
```
`3`
```
H H   H H         H H   H H
HHH   HHH         HHH   HHH
H H   H H         H H   H H
H HH HH H         H HH HH H
HHHHHHHHH         HHHHHHHHH
H HH HH H         H HH HH H
H H   H H         H H   H H
HHH   HHH         HHH   HHH
H H   H H         H H   H H
H H   H HH H   H HH H   H H
HHH   HHHHHH   HHHHHH   HHH
H H   H HH H   H HH H   H H
H HH HH HH HH HH HH HH HH H
HHHHHHHHHHHHHHHHHHHHHHHHHHH
H HH HH HH HH HH HH HH HH H
H H   H HH H   H HH H   H H
HHH   HHHHHH   HHHHHH   HHH
H H   H HH H   H HH H   H H
H H   H H         H H   H H
HHH   HHH         HHH   HHH
H H   H H         H H   H H
H HH HH H         H HH HH H
HHHHHHHHH         HHHHHHHHH
H HH HH H         H HH HH H
H H   H H         H H   H H
HHH   HHH         HHH   HHH
H H   H H         H H   H H
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the lowest byte count for each language wins!
  
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 46 bytes
```
Nest[ArrayFlatten@{r={#,0,#},{#,#,#},r}&,1,#]&
```
Returns a 2d array of `0`s and `1`s.
[Try it online!](https://tio.run/##FcixCoAgEADQXzkUmi4omouCaIx2cThCKSiD65YQv91qevBOks2dJPtK2UMLeXa3mIGZnukgERf6yG3UWKFO@Kl/ORVYo7ZFHi@z8B7EBAQFZQcKwZtgLUL8qkk2vw "Wolfram Language (Mathematica) – Try It Online")
[![Nest[ArrayFlatten@{r={#,0,#},{#,#,#},r}&,1,#]&[3]//MatrixForm](https://i.stack.imgur.com/Sg0GI.jpg)](https://i.stack.imgur.com/Sg0GI.jpg)
[![Nest[ArrayFlatten@{r={#,0,#},{#,#,#},r}&,1,#]&[5]//Image](https://i.stack.imgur.com/KMZDi.jpg)](https://i.stack.imgur.com/KMZDi.jpg)
[Answer]
## [Canvas](https://github.com/dzaima/Canvas), ~~14~~ 12 [bytes](https://github.com/dzaima/Canvas/blob/master/files/chartable.md)
```
H；［⌐⌐∔＊×∔；３＊＋
```
[Try it here!](https://dzaima.github.io/Canvas/?u=SCV1RkYxQiV1RkYzQiV1RkYxQSV1RkYxQSV1RkYxQSV1RkYxMiV1RkYwQSVENyV1MjIxNCV1RkYxQiV1RkYxMyV1RkYwQSV1RkYwQg__,i=Mg__)
Explanation:
```
Code    |Instruction                                                         |Stack
--------+--------------------------------------------------------------------+-------------------------
        |Push input to stack (implicit)                                      |I
H       |Push "H" to stack                                                   |I,"H"
；      |Swap the top two stack items                                        |"H",I
［      |The following ToS (input) times:                                    |X
    ⌐⌐  |Duplicate ToS (result from last loop ("H" if first loop)) four times|X,X,X,X,X
    ∔   |Join vertically                                                     |X,X,X,X\nX
    ×   |Prepend                                                             |X,X,XX\nX
    ∔   |Join vertically                                                     |X,X\nXX\nX
    ；  |Swap top two stack items                                            |X\nXX\nX,X
    ３＊|Repeat three times vertically                                       |X\nXX\nX,X\nX\nX
    ＋  |Join horizontally                                                   |X<space>X\nXXX\nX<space>X
        |End loop (implicit)                                                 |X
        |Print ToS (implicit)                                                |
```
Where `I` is the input, `X` is the pattern generated by the previous loop ("H" for the first loop), and `<space>` is the empty space on the first and third row of the pattern, added implicitly by `＋`.
*-2 bytes thanks to [dzaima](https://codegolf.stackexchange.com/users/59183/dzaima)!*
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~12~~ 11 bytes
```
t:"[ACA]BX*
```
Given input `n`, this outputs a matrix containing `0` and `n`.
[Try it online!](https://tio.run/##y00syfn/v8RKKdrR2THWKULr/39jAA)
To convert this into a character matrix of `H`and space add `g72*c` in the header. [Try it online too!](https://tio.run/##y00syfn/v8RKKdrR2THWKULrf7q5kVbyf2MA)
Or add `]1YC` to see the matrix displayed graphically. Try it at [MATL Online!](https://matl.io/?code=t%3A%22%5BACA%5DBX%2a%0A%5D1YG&inputs=3&version=20.8.0)
### Explanation
```
t          % Input (implicit): n. Duplicate
:          % Range. Gives the array [ 1 2 ... n]
"          % For each (that is, do n times)
  [ACA]    %   Push the array [5 7 5]
  B        %   Convert to binary. Gives the 3×3 matrix [1 0 1; 1 1 1; 1 0 1]
  X*       %   Kronecker product
           % End (implicit). Display (implicit)
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 16 15 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
╛c_mê║6{│◙ÖmπV"
```
[Run and debug it](https://staxlang.xyz/#p=be635f6d88ba367bb30a996de35622&i=2&a=1)
This is the ascii representation of the program with comments. This program builds up the H sideways, and then transposes once at the end.
```
'H]                 ["H"]
   {         },*    repeat block specified number of times
    c               copy the matrix
     {3*m           triplicate each row
         |S         surround; prepend and append like b + a + b
           |C       horizontally center rows with spaces
                M   transpose back to original orientation
                 m  output each row
```
[Bonus 14 byte program](https://staxlang.xyz/#p=ce104d969c6288969557af02b941&i=3&a=1) - uses its input as the output character. Theoretically, this would not produce the right shape at 10, since it has 2 digits, but attempting to run that crashes my browser.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 72 bytes
Output is a list of strings, one string per line.
```
f=->n{n<1?[?H]:[*a=(x=f[n-1]).map{|i|i+' '*i.size+i},*x.map{|i|i*3},*a]}
```
[Try it online!](https://tio.run/##KypNqvz/P81W1y6vOs/G0D7a3iPWKlor0VajwjYtOk/XMFZTLzexoLomsyZTW11BXStTrzizKlU7s1ZHqwIuo2UM5CbG1v4vKC0pVkiLTk8tKdYryY/PjP1vCgA "Ruby – Try It Online")
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), 14 bytes
```
×/¨∘.≥⍨2|,⍳⎕⍴3
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob2pm/qO2CQZcjzra0/4fnq5/aMWjjhl6jzqXPupdYVSj86h3M1DNo94txv@BKv6ncRlzqatzpXEZQShDCGUAAA "APL (Dyalog Classic) – Try It Online")
`⎕` evaluated input n
`,⍳⎕⍴3` all n-tuples with elements from 0 1 2
`2|` mod 2
`×/¨∘.≥⍨` form a matrix by comparing every pair of tuples a and b - if all elements of a are ≥ the corresponding elements of b, it's a 1, otherwise 0
[Answer]
# [Haskell](https://www.haskell.org/), 50 bytes
```
f 0=[[1]]
f n=[x++map(*c)x++x|c<-[0,1,0],x<-f$n-1]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P03BwDY62jA2litNIc82ukJbOzexQEMrWRPIqqhJttGNNtAx1DGI1amw0U1TydM1jP2fm5iZp2CrAFTnq1BQlJlXoqCikKZg/B8A "Haskell – Try It Online")
Makes a grid of 0's and 1's. One character longer for spaces and H's.
# [Haskell](https://www.haskell.org/), 51 bytes
```
f 0=["H"]
f n=[x++map(min c)x++x|c<-" H ",x<-f$n-1]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P03BwDZayUMplitNIc82ukJbOzexQCM3M08hWRPIqahJttFVUvBQUNKpsNFNU8nTNYz9n5sIlLZVACr0VSgoLQkuKfLJU1BRSFMw/g8A "Haskell – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~17~~ ~~16~~ 15 bytes
```
381B«€s3Z€ẎF€µ¡
```
This is a full program that prints a 2D array of **1**'s and **0**'s.
[Try it online!](https://tio.run/##y0rNyan8/9/YwtDp0OpHTWuKjaOA5MNdfW5A6tDWQwv/AyUB "Jelly – Try It Online") or [see the output with **H**'s and spaces.](https://tio.run/##y0rNyan8/9/YwtDp0OpHTWuKjaOA5MNdfW5A6tDWQwv/H25/uLv7UeM@D4VIoDoA "Jelly – Try It Online")
[Answer]
# [SOGL V0.12](https://github.com/dzaima/SOGLOnline), 13 [bytes](https://github.com/dzaima/SOGL/blob/master/chartable.md)
```
┌.{³2∙⁴┼+;3∙┼
```
[Try it Here!](https://dzaima.github.io/SOGLOnline/?code=JXUyNTBDLiU3QiVCMzIldTIyMTkldTIwNzQldTI1M0MrJTNCMyV1MjIxOSV1MjUzQw__,inputs=Mw__,v=0.12)
[Answer]
# [R](https://www.r-project.org/), 64 bytes
```
function(n)Reduce(`%x%`,rep(list(matrix(c(1,1,1,0,1,0),3,3)),n))
```
[Try it online!](https://tio.run/##JY5BC8IwDIXv/opQERqsoOyoP2Bnr6JsdCkrzHRkGZu/vnZKyCHvhfc@yQFuJ8hhZq8xsWW8Uzd7ss1hPTROaLRDnNS@W5W4Wm8vbpvztugqVyE6Rsy8xVSwh4kUGHoSuoL2BELvNnJHAin8hJCSlmuJwwCjRFZIs/6cfwf4JEJeh0/50R5qaLmDaWw97RaJSgXCgHGmNvhQGwry8YLPAlS92DljMH8B "R – Try It Online")
Reduces by Kronecker product, as a shameless port of [Luis Mendo's answer](https://codegolf.stackexchange.com/a/157697/67312).
The footer prints the result nicely, but this is an anonymous function which returns a `matrix` of `1` for `H` and `0` for space.
[Answer]
# [Java (JDK)](http://jdk.java.net/), 126 bytes
```
n->{int s=1,H[][]=new int[n+=Math.pow(3,n)-n][n],x;for(;s<n;s*=3)for(x=n*n;x-->0;)H[x/n][x%n]|=~(x/n/s%3)&x%n/s%3&1;return H;}
```
[Try it online!](https://tio.run/##pVA9b8IwFNz5FU9IIOcT0nTCmLFKBybGKEMaEjANL1HsQChN/3pqBwpVu1TqYD3fvbPuzrv4EDu79WvH92VRSdgp7NaS525WYyJ5ga5JB0keCwHLmCOcBwDPKJ@u2zlHGUZhtIAMWIfO4qwIEMyzA00zTI@gJWixZSy3blkciW@j4WAUYmQ3NCsqQsUcqTCZb2jUMDSRNo6zmFIjCJuJkjYjjN7ZB1FgIka@MVaEvow9WqWyrhAC2nZUZSvrl5wnIGQs1TgUfA17nZusZMVxE0YQVxth9D0AtF8fmL@lzKP9hDkD/3K1rC8hwLUoBMBAt9K/QQw3c@OyzE9Eyw161ZbKSpLghlcnIdO9W9TS7Ve5evm/XTu4nO89L663nD86Kmo7u7P37s0Mtnf2t2lGhqNkaAPxnMYwH6eW/3AL2P4xaNt9Ag "Java (JDK) – Try It Online")
Returns an `int[][]` with `0` for `H` and `1` for `space`. This actually "carves" a wall of `H`'s instead of "piling" `H`'s.
## Explanations
```
n->{                        // An int to int[][] lambda function
  int s=1,                  //  size of the carvings.
      H[][]=new int[n+=Math.pow(3,n)-n][n],  
                            //  change n to 3^n, through +=...-n to avoid an explicit cast
                            //  and create the 2D array to return, filled with 0s
      x;                    //  counter for the 2D array
  for(;s<n;s*=3)            //  for each size
    for(x=n*n;x-->0;)       //   for each cell
      H[x/n][x%n] |=        //     assign 1 to a cell of the array if...
        ~(x/n/s%3)          //      it is located in the "holes" of the H
        &x%n/s%3            //
        &1;                 //      
  return H;                 //  return the array
}                           // end the lambda
```
## Credits
* -9 bytes thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat).
[Answer]
# [V](https://github.com/DJMcMayhem/V), 22 bytes
```
éHÀñäLgvr PGï3PkyHGpH
```
[Try it online!](https://tio.run/##K/v///BKj8MNhzceXiLmk15WpBDgfni9cUB2pYd7gcf///9NAA "V – Try It Online")
Hexdump:
```
00000000: e948 c0f1 e416 4c67 7672 2050 47ef 3350  .H....Lgvr PG.3P
00000010: 6b79 4847 7048                           kyHGpH
```
This is basically the exact same approach as [the Sierpinski carpet](https://codegolf.stackexchange.com/a/126218/31716) and [The Fractal Plus](http://golf.shinh.org/p.rb?Fractal+Plus) on Anarchy Golf.
[Answer]
# [Python 2](https://docs.python.org/2/), 70 bytes
```
f=lambda r:-r*'H'or[x+[x,' '*3**r][b]+x for b in 1,0,1for x in f(r-1)]
```
[Try it online!](https://tio.run/##JcgxCoAgGAbQq/ybaQZZW9DeHcQhCUkolQ8DO70hje@lN58xTLW69dpve@yEZYBgG4vQpddFMmJiFgJGW9MXchFkyQdScpSqqTS5DoPiprbAHz6kJ3ecLwk@ZEKdPw "Python 2 – Try It Online")
Function outputs a list of strings.
---
# [Python 2](https://docs.python.org/2/), 84 bytes
```
r=input()
for i in range(3**r):x,s=' H';exec"s+=[x,s][i%3%2]+s;x*=3;i/=3;"*r;print s
```
[Try it online!](https://tio.run/##DcixCoAgEADQva84gqg0CHJLbu8fwiHC6pZLTgP7emt5wwtvum6eShEkDk/q@uq4BQiIQTY@fWeUkn7OQ8QWltb67Pc6alz/cSs1ppmcjjYrNJbGn1qJDUKcIJZiPg "Python 2 – Try It Online")
Uses the same template as other 3\*3 fractal patterns: 
* [Sierpinski
carpet](https://codegolf.stackexchange.com/a/143780/20260)
* [Fractal
X](http://golf.shinh.org/reveal.rb?Fractal+X/xnor_1471571069&py)
* [Fractal plus](http://golf.shinh.org/p.rb?Fractal%20Plus).
[Answer]
# [J](http://jsoftware.com/), ~~25~~ 22 bytes
```
,./^:2@(*/#:@5 7 5)^:]
```
[Try it online!](https://tio.run/##y/r/P81WT0FHTz/OyshBQ0tf2crBVMFcwVQzzir2f2pyRr5CmoIRF4ihoa7goV5dp@WQpqmmZ6dgrGCkYPgfAA)
```
        */               multiply by
          #:@5 7 5       the binary matrix shaped like H
,./^:2                   assemble the 4-dimensional result into a matrix
                   ^:]   do it input times
```
[Answer]
## Haskell, ~~73~~ ~~67~~ ~~64~~ 55 bytes
```
g#f=g<>f<>g
w=map.(id#)
(iterate(w(>>" ")#w id)["H"]!!)
```
This works only with the latest version of `Prelude`, because it exports `<>` from `Data.Semigroup`. To run it on TIO, add an import as done here: [Try it online!](https://tio.run/##DcuxDoMgEADQ3a84oQMsLq7C5ODQTo6mMZcI9FJRgmf4fNq3vw9eX7fvlWI6M8OIjN3sIoV83qkG6U0YrB9saIqJmDpFm9SNN4rYZWSnirJWgNCyAG16EZN4t62uEekAA//yWiHdPHN@HvAAD339AQ "Haskell – Try It Online")
```
g#f=              -- function # takes two functions g and f and a list s
                  -- and returns
   g <> f <> g    -- g(s), followed by f(s) and another g(s)
w=                -- w takes a function and a list of lists
                  -- (both as unnamed parameters, because of pointfree style,
                  -- so let's call them f and l)
  map.(id#)       -- return map(id#f)l, i.e. apply (id#f) to every element of l
  w(>>" ")#w id   -- this partial application of # is a function that
                  -- takes the missing list (here a list of lists)
                  -- remember: (>>" ") is the function that replaces every element
                  -- of a list with a single space
iterate(   )["H"] -- starting with a singleton list of the string "H"
                  -- which itself is a singleton list of the char 'H'
                  -- repeatedly apply the above function
              !!  -- and pick the nth iteration
Example for ["H H", "HHH", "H H"], i.e.
   H H
   HHH
   H H
call the iterated function:
                    ( w(>>" ")         # w id       ) ["H H","HHH","H H"]
expand w:           ( map(id#(>>" "))  # map(id#id) ) ["H H","HHH","H H"]
expand outermost #: map(id#(>>" "))["H H","HHH","H H"] ++
                    map(id#id)     ["H H","HHH","H H"] ++
                    map(id#(>>" "))["H H","HHH","H H"]
expand map:         [(id#(>>" "))"H H",   (id#(>>" "))"HHH",   (id#(>>" "))"H H"] ++
                    [(id#id)     "H H",   (id#id)     "HHH",   (id#id)     "H H"] ++
                    [(id#(>>" "))"H H",   (id#(>>" "))"HHH",   (id#(>>" "))"H H"]
expand other #:     ["H H"++"   "++"H H", "HHH"++"   "++"HHH", "H H"++"   "++"H H"] ++
                    ["H H"++"H H"++"H H", "HHH"++"HHH"++"HHH", "H H"++"H H"++"H H"] ++
                    ["H H"++"   "++"H H", "HHH"++"   "++"HHH", "H H"++"   "++"H H"]
collaps ++:         ["H H   H H", "HHH   HHH", "H H   H H",
                     "H HH HH H", "HHHHHHHHH", "H HH HH H",
                     "H H   H H", "HHH   HHH", "H H   H H"]
which is printed line by line: 
  H H   H H
  HHH   HHH
  H H   H H
  H HH HH H
  HHHHHHHHH
  H HH HH H
  H H   H H
  HHH   HHH
  H H   H H
```
Edit: -9 bytes thanks to @Potato44.
[Answer]
# Vim - 66 56 54 bytes
`A`
`@`
`c`
`H`
`esc`
`"`
`r`
`d`
`^`
`q`
`c`
`{`
`ctrl-v`
`}`
`"`
`a`
`y`
`g`
`v`
`r`
`space`
`g`
`v`
`d`
`"`
`a`
`P`
`P`
`"`
`a`
`P`
`V`
`G`
`"`
`b`
`y`
`P`
`g`
`v`
`ctrl-v`
`$`
`d`
`"`
`a`
`P`
`.`
`.`
`G`
`"`
`b`
`p`
`q`
`@`
`r`
The input is taken as a number in the buffer.
[Answer]
# [Perl 5](https://www.perl.org/), ~~46~~ ~~44~~ ~~43~~ ~~41~~ 40 bytes
1 based counting. Uses `0` and `1` for `H` and space, has a leading `1` (space)
```
say//,map/$'/^1,@;for@;=glob"{A,.,A}"x<>
```
Based on a classic idea by mtve.
[Try it online!](https://tio.run/##K0gtyjH9/784sVJfXyc3sUBfRV0/zlDHwTotv8jB2jY9Jz9JqdpRR0/HsVapwsbu/3/jf/kFJZn5ecX/dX1N9QwN9AwA "Perl 5 – Try It Online")
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 38 34 bytes[SBCS](https://github.com/abrudz/SBCS)
```
({(⍵,(0×⍵),⍵){⍺⍪⍵⍪⍺}⍵,⍵,⍵}⍣⎕)1 1⍴1
```
Output is a 2-dimensional array with `1` representing H and `0` representing space.
[Try it online!](http://tryapl.org/?a=f%u2190%7B%28%7B%28%u2375%2C%280%D7%u2375%29%2C%u2375%29%7B%u237A%u236A%u2375%u236A%u237A%7D%u2375%2C%u2375%2C%u2375%7D%u2363%u2375%291%201%u23741%7D&run)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~30~~ 29 bytes
```
HＦＥＮＸ³ι«Ｊ⁰¦⁰Ｃ⁰ιＣιιＴ⊗ι⊗ι‖ＯＯ→↓ι
```
[Try it online!](https://tio.run/##RYxBCoMwFETXeorg6gdSENzZZV20hbYiXiDaWD9EfwiJUkrPniqVdjXvMcy0vbQtSR1CaXF0kBwTvo87sgwu0sBpNN5d/dAoC1ywkuYFMsGQc85ecXT2g6kJUsHSZRYdyDxXwZ/gJrXFAQryjVZ3wOXqz2tdqU6r1t0mZbU0W0Be4aN3guUFzeP36B1CFnaT/gA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
H
```
Print the original `H`.
```
ＦＥＮＸ³ι«
```
Loop over the first `size` powers of 3.
```
Ｊ⁰¦⁰
```
Move the cursor back to the origin. `Trim` needs this, as both the original printing of the `H` and the reflection below move the cursor.
```
Ｃ⁰ι
```
Copy the previous iteration downwards, creating a domino.
```
Ｃιι
```
Copy the result down and right, creating a tetromino.
```
Ｔ⊗ι⊗ι
```
Trim the canvas down to an `L` shape triomino.
```
‖ＯＯ→↓ι
```
Reflect the canvas horizontally and vertically with overlap, completing the iteration.
Charcoal is better at some fractals than others. Here's a similar idea, but in almost half the size:
```
HＦＮ«⟲Ｃ²⁶‖ＯＯLＸ³ι
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvREPJQ0nTmistv0hBwzOvoLTErzQ3KbVIQ1NToZqLMyi/JLEk1Tm/oFLDyAyojDMoNS0nNbnEvyy1KCexAEppWPnoKATklwO1GesoZGoCFdb@/2/8X7csBwA "Charcoal – Try It Online") Link is to verbose version of code.
[Answer]
# [Python 2](https://docs.python.org/2/), 143 bytes
```
def g(a,x,y,s):
	if s:s/=3;[g(a,x+k/3*s,y+k%3*s,s)for k in 0,2,3,4,5,6,8]
	else:a[x][y]=1
def f(s):s=3**s;a=eval("s*[0],"*s);g(a,0,0,s);print a
```
[Try it online!](https://tio.run/##ZU/daoMwFL72PEUIDKKG1tVuDMWL3u0dRCRY02Y6I550rU/vTnQw2MhFPs75/s44u6sdDov5HO3kGM4I0PQKkZ0yCM6tZnVtBuPqWmDb65CGgQc7dJMZLqxgnG@8@2Rcu5IkQ3fT/7lxsS0AFOlOIgSw/ZkgpRLnbG8O4BfTQi3e@SKUfMhZonc0mmGG@yLNy3Ued/s0QjnH3ZP/MdR2Yh0zA0vkQabyKF/kq3yrIGh7bDNVPqpyropn8M5akCcWaRRhror2S/WCY1QmleQRhrkPSOgRHOkCR320OIZ/StINAD51snefu9qon6N95U3L@e7DmkFw9s7LpmJe0Xg@ycLlGw "Python 2 – Try It Online")
-30 bytes thanks to recursive
wrapper code is for nice formatting. it works fine if you remove it
[Answer]
# PHP 7, ~~125~~ 109 bytes
a different approach: Instead of nesting and flattening the result recursively, this just loops through the rows and columns and uses a 3rd loop to find out if to print `H` or `_`.
Edit: Saved a lot by combining the row/column loops to one, though it took a bit to get the decrease for the inner loop correct. Requires PHP 7 for the power operator.
[Try them online](http://sandbox.onlinephpfunctions.com/code/447783049bb371196e0235947b591123f9fe0e9a)!
---
```
for($z=3**$argn;$z*$z>$q=$p;print$c."
"[++$p%$z])for($c=H;$q;$q-=$q/$z%3*$z,$q/=3)if($q%3==1&&$q/$z%3-1)$c=_;
```
prints the result. Run as pipe with `-nR`.
## qualified function, ~~147~~ 130 bytes
```
function r($n){for($z=3**$n;$z*$z>$q=$p;$r.=$c."
"[++$p%$z])for($c=H;$q;$q-=$q/$z%3*$z,$q/=3)if($q%3==1&&$q/$z%3-1)$c=_;return$r;}
```
returns a single string. Run with default config (no `php.ini`).
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 25 bytes
```
,’b3U×"3S_4A1e
3*çþ`ị⁾ HY
```
[Try it online!](https://tio.run/##AS0A0v9qZWxsef//LOKAmWIzVcOXIjNTXzRBMWUKMyrDp8O@YOG7i@KBviBIWf///zQ "Jelly – Try It Online")
---
Although this is longer than the [existing Jelly submission](https://codegolf.stackexchange.com/a/157670/), it tries to generate each character independently just from the coordinate.
In particular, if the coordinate is `(x,y)` (1-indexing), the first link returns `0` and `1` corresponds to `H` and  respectively.
```
,                Pair. Get (x,y)
 ’               Decrement. Get (x,y) (0-indexing)
  b3             Convert to base 3 digits.
    U            Upend. So next operations can pair the corresponding digits.
     ×"3         Multiply the first element (list) by 3.
        S        Sum (corresponding digit together). Let the sum be **s**.
         _4A1e   Check if any of **abs(s-4)** is 1. Equivalently, check
                 if there is any **3** or **5** in the list of **s**.
```
Also, the 5 bytes `ị⁾ HY` are used for formatting, so this program (20 bytes) is also valid (but the output doesn't look as nice):
```
,’b3U×"3S_4A1e
3*çþ`
```
[Answer]
# [T-SQL](https://www.microsoft.com/sql-server), ~~267~~ 261 bytes
```
DECLARE @N INT=3DECLARE @ TABLE(I INT,H VARCHAR(MAX))INSERT @ VALUES(1,'H H'),(2,'HHH'),(3,'H H');WITH
T AS(SELECT 1 A,3 P,I J,H S FROM @ UNION ALL SELECT A+1,P*3,J*P+I,REPLACE(REPLACE(S,' ','   '),'H',H)FROM @,T
WHERE A<@N)SELECT S FROM T WHERE A=@N ORDER BY J
```
[Answer]
# PHP 7, 153 bytes
```
    function p($n){$r=["H H",HHH,"H H"];if(--$n)foreach(p($n)as$s){$r[+$i]=$r[$i+6*$p=3**$n]=str_pad($s,2*$p).$s;$r[3*$p+$i++]=$s.$s.$s;}ksort($r);return$r;}
```
Run with default config (no `php.ini`) or [try it online](http://sandbox.onlinephpfunctions.com/code/58d2dc212341fc0beb36b00d8ce44ced12e23116).
[Answer]
# Perl, 64 bytes
```
//;$_ x=3,$.=s|.+|$&@{[$$_++/$.&1?$&:$"x$.]}$&|g for($_=H.$/)x$'
```
Requires `-p`, input is taken from stdin. Output is an H of `H`s.
[Try it online!](https://tio.run/##BcHRCkAwFAbgV5H@hsZZkhtaXHoHaVdIyRYuVubVHd/n5nOvmZVqYSKvqxykr0AyQPTPCBgpFUiUHUSD2IOmFyKs0WLPFEYPBJV5JMzVZ9292ePiwv0 "Perl 5 – Try It Online")
[Answer]
# PHP (5.6+), 94 bytes
```
<?for(;$H>$e*=3or$e=($i+=$e&&print"$s
")<${$s=H}=3**$argn;)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;
```
Used with `-F` command line option. Assumes interpreter defaults (`-n`). Will not work on versions previous to 5.6, due to the power operator.
**Sample usage**
```
$ echo 3|php -nF h-carpet.php
H H   H H         H H   H H
HHH   HHH         HHH   HHH
H H   H H         H H   H H
H HH HH H         H HH HH H
HHHHHHHHH         HHHHHHHHH
H HH HH H         H HH HH H
H H   H H         H H   H H
HHH   HHH         HHH   HHH
H H   H H         H H   H H
H H   H HH H   H HH H   H H
HHH   HHHHHH   HHHHHH   HHH
H H   H HH H   H HH H   H H
H HH HH HH HH HH HH HH HH H
HHHHHHHHHHHHHHHHHHHHHHHHHHH
H HH HH HH HH HH HH HH HH H
H H   H HH H   H HH H   H H
HHH   HHHHHH   HHHHHH   HHH
H H   H HH H   H HH H   H H
H H   H H         H H   H H
HHH   HHH         HHH   HHH
H H   H H         H H   H H
H HH HH H         H HH HH H
HHHHHHHHH         HHHHHHHHH
H HH HH H         H HH HH H
H H   H H         H H   H H
HHH   HHH         HHH   HHH
H H   H H         H H   H H
```
[Try it online!](https://tio.run/##DctBDsIgEAXQvcdovrSg1hh20rE701uYJo7KBiYMO@PVRd/@yUtam@ZHLkPAcgE78rmAaUDcEdgYKTHVDrrp7IQ3lJYPeeewlmcKFjqS1nKT9f4fR/DWm9MMPff9HmxHaGjNf7PUmJO2Q7r@AA "PHP – Try It Online")
[Answer]
# CJam - 103 97 87 76 bytes
```
{H{ae_,S*}%}:Il~a:A];{A_W={)(a9*+:A;[[HIH][HHH][HIH]]{z~}%}{);:A;"H"}?}:H~N*
```
This program does a quite verbose "handcoded" recursion. No smart matrix multiplications. Throughout the recursion, on top of the stack there is an array gathering the output gained from the parent calls. Right after each set of recursive calls the output of the recursive calls needs to be zipped together, to make sure the output is correct when the stack is printed linearly at the end of the program. The stack of arguments being passed down the recursion is kept in the variable `A`.
[Try online](http://cjam.aditsu.net/#code=%7BH%7Bae_%2CS*%7D%25%7D%3AIl~a%3AA%5D%3B%7BA_W%3D%7B)(a9*%2B%3AA%3B%5B%5BHIH%5D%5BHHH%5D%5BHIH%5D%5D%7Bz~%7D%25%7D%7B)%3B%3AA%3B%22H%22%7D%3F%7D%3AH~N*&input=4)
[Answer]
# [K (ngn/k)](https://github.com/ngn/k), 18 bytes
```
{~|/a<\:'a:1=!x#3}
```
[Try it online!](https://tio.run/##y9bNS8/7/z/NqrquRj/RJsZKPdHK0FaxQtm49n@agiFXmoIREBv/BwA "K (ngn/k) – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 23 bytes
```
_·£[X³XX³]Ãy c ·û}gQq)y
```
[Try it online!](https://ethproductions.github.io/japt/?v=1.4.5&code=I1tvX2QjJCBrIltvX2QkazEhIjE=&input=)
### Unpacked & How it works
```
Z{ZqR mXYZ{[Xp3 XXp3]} y c qR û}gQq)y
Z{    Declare a function that accepts a string...
  ZqR   Split by newline...
  mXYZ{   and map each row into...
    [Xp3 XXp3]  an array of [X.repeat(3), X, X.repeat(3)]
  }
  y   Transpose the resulting 2D array
  c   Flatten
  qR  Join with newline
  û   Center-pad each row to the longest
}
gQq)  Apply the above function to '"' recursively
y     Transpose the resulting 2D string
```
Using the transposed pattern
```
III
 I 
III
```
is far easier to handle than the original `H` pattern, at least in Japt where the `I` can be done with string repeat and center-padding.
[Answer]
# C++11 - 138 bytes
Not sure if this answer has a valid syntax here however.
```
#define A a?1:0
template<int N>struct H{H<N-1>h[9];H(int a):h{A,0,A,A,A,A,A,0,A}{}};template<>struct H<0>{char h;H(int a):h{a?'H':' '}{}};
```
Ungolfed with working code
```
#include <iostream>
#define A a?1:0
template<int N>
struct H
{
  H<N-1> h[9];
  H(int a) : h{A,0,A,A,A,A,A,0,A}
  {}
};
template<>
struct H<0>
{
  char h;
  H(int a) : h{a?'H':' '}
  {}
};
int pow(int a, int b)
{
  int res=1;
  for (int i=1; i<=b; ++i)
    res *= a;
  return res;
}
template<int N>
char getHvalue(int i, int j, H<N> &hn)
{
  int n3=pow(3, N-1);
//std::cout << N << " " << i << " " << j << std::endl;
  return getHvalue(i%n3, j%n3, hn.h[i/n3*3+j/n3]);
}
template<>
char getHvalue<0>(int, int, H<0> &hn)
{
  return hn.h;
}
int main()
{
  H<0> h0(1);
  std::cout << getHvalue(0, 0, h0) << std::endl;
  std::cout << "\n====================\n" << std::endl;
  H<1> h1(1);
  for (int i=0; i<3; ++i) {
    for (int j=0; j<3; ++j)
      std::cout << getHvalue(i, j, h1);
    std::cout << std::endl;
  }
  std::cout << "\n====================\n" << std::endl;
  H<2> h2(1);
  for (int i=0; i<9; ++i) {
    for (int j=0; j<9; ++j)
      std::cout << getHvalue(i, j, h2);
    std::cout << std::endl;
  }
  std::cout << "\n====================\n" << std::endl;
  H<3> h3(1);
  for (int i=0; i<27; ++i) {
    for (int j=0; j<27; ++j)
      std::cout << getHvalue(i, j, h3);
    std::cout << std::endl;
  }
  return 0;
}
```
]