# Draw some expanding arrows

This challenge is about printing a series of growing ASCII-art arrows. I'll describe the pattern in words, but it might be easier to look at what the start of this series looks like:

>
<
->
<-
-->
<--
--->
<---
---->
<----
----->
<-----
------>
<------
...


An arrow with length n contains an arrowhead (< or >) and n-1 dashes (-). A right-facing arrow has the dashes first, then a >. A left-facing arrow starts with <, and is followed by the dashes. The series consists of a length n right-facing arrow followed by a length n left-facing arrow, with n from 1 to infinity.

To complete the challenge, write a program or function that takes one input, an integer i >= 1, and outputs the first i arrows. Arrows are individual, not in right-left pairs, so for i=3 you should output:

>
<
->


You can return a list of strings, or print them one after the other. If printing, the arrows must be delimited by some consistent delimiter, which doesn't have to be a newline as in the example.

This is , so fewest bytes wins.

• – AdmBorkBork Dec 12 '18 at 16:23
• Can we have spaces before/after each line? – Olivier Grégoire Dec 12 '18 at 16:54
• @OlivierGrégoire Yes, trailing whitespace is ok. – Pavel Dec 12 '18 at 16:55
• And heading whitespace? – Olivier Grégoire Dec 12 '18 at 16:58
• @OlivierGrégoire Yeah, that's fine. – Pavel Dec 12 '18 at 17:00

# Canvas, 10 bytes

⇵-×<ｎ¹［↔｝］


Try it here!

• I don't know any Canvas, but is that an arrow-drawing builtin I see? ↔ kinda looks like it! – Pavel Dec 12 '18 at 16:03
• ↔ is the "reverse horizontally" built-in (also swapping > & <), sadly no arrow built-ins :p – dzaima Dec 12 '18 at 16:04

# R, 69 bytes

for(i in 1:scan()-1)cat('<'[i%%2],rep('-',i/2),'>'[!i%%2],'
',sep='')


Try it online!

• -5 bytes thanks to @Giuseppe
• -3 bytes thanks to @Robert S.
• strrep coerces its second argument to integer so you can use / in place of %/% – Giuseppe Dec 12 '18 at 16:40
• you can also get rid of a entirely by indexing over 0...(n-1) instead: Try it online! – Giuseppe Dec 12 '18 at 16:44
• I'm an idiot... thanks ! :D – digEmAll Dec 12 '18 at 17:17
• @Giuseppe :also I just noticed the deleted question of Robert S. I can use rep instead of strrep and save 3 bytes...(facepalm) – digEmAll Dec 12 '18 at 17:21

# Java (JDK), 81 bytes

n->{for(int i=0;i<n;)System.out.printf(i%2<1?"<%s%n":"%s>%n","-".repeat(i++/2));}


Try it online!

## Explanations

n->{                  // int-accepting consumer
for(int i=0;i<n;)    //  for each i from 0 to n-1 included
System.out.printf(  //   output on stdout with a pattern
i%2<1              //    if i is even:
?"<%s%n"          //     use the left-arrow pattern
:"%s>%n",         //    else: use the right-arrow pattern
"-".repeat(i++/2)  //    fill the "%s" in the pattern with i/2 dashes, and increment i
);                  //
}                     //


## Haskell, 41 40 bytes

(takeg">")
g p=p:('<':init p):g('-':p)


Try it online!

Plain old recursion: start with string p = ">", collect p, a < in front of all but the last char of p and a recursive call with one - put in front of p. Take the first n items of this list.

Edit: -1 byte thanks to @xnor.

• A weird change to save a byte. – xnor Dec 13 '18 at 5:44

# Commodore BASIC V2 (C64), 94 bytes

0inputn:fOi=1ton:oniaN1gO1:?"<";
1on-(i<3)gO2:fOj=1.5toi/2:?"-";:nE
2on-nOiaN1gO3:?">";
3?:nE


Not entirely sure about the byte count, this is based on the text representation for typing the valid program. It's a bit shorter on disk (91 bytes) because BASIC V2 uses a "tokenized" representation of programs.

### Online Demo

Slightly "ungolfed":

0 inputn:fori=1ton:oniand1goto1:print"<";    :rem read n from user, loop to n, if odd skip "<"
1 on-(i<3)goto2:forj=1.5toi/2:print"-";:next :rem skip for i<3, print (i-1)/2 times "-"
2 on-notiand1goto3:print">";                 :rem if even skip ">"
3 print:next                                 :rem newline and next loop iteration


# Self-modifying Brainfuck, 55 bytes

Take input as character code.
Only support input up to 255.
Use null character to separate lines.

Coincidentally, all arrow-drawing characters are used as BF commands. Unfortunately, it doesn't save any bytes (currently).

>>,[<<[-<.>>+<]<<.>>.+>>-[<<<<<.>>>>[-<+<.>>].>-<]>]<>-


Try it online!

### Explanation

 Code  |              Memory         | Output | Comment
-------+-----------------------------+--------+--------------------------
| '<' '>' '-' [0]  0   0   0  |        |
>>,    | '<' '>' '-'  0   0  [x]  0  |        |
[      |                             |        |
| '<' '>' '-'  l   0  [x]  0  |        | l = arrow length
<<[-<  |                             |        | copy l to next cell
.>>+<] |                             |        | and print '-'
| '<' '>' '-' [0]  l   x   0  | -----  | there are l '-'s
<<.    | '<' [>] '-'  0   l   x   0  | >      |
>>.+   | '<' '>' '-' [1]  l   x   0  | <null> |
>>-    | '<' '>' '-'  1   l  [y]  0  |        | y=x-1
[      |                             |        | execute if y>0
<<<<<. | [<] '>' '-'  1   l   y   0  | <      |
>>>>   | '<' '>' '-'  1  [l]  y   0  |        |
[-<+<. |                             |        |
>>]    | '<' '>' '-'  L  [0]  y   0  | -----  | L=l+1
.      | '<' '>' '-'  L  [0]  y   0  | <null> |
>-<]>] |                             |        | decrement y
<>-    |                             |        | do nothing, used as data


# Python 2, 54 bytes

thanks to the and Jo King for fixing a bug.

k=0
exec"print k%2*'<'+k/2*'-'+~k%2*'>';k+=1;"*input()


Try it online!

• Your arrows have too many dashes; only every other one should lengthen by a dash. – xnor Dec 13 '18 at 2:35
• 54 bytes – tsh Dec 13 '18 at 5:23

# Pyth, 17 bytes

m_W%d2+*\-/d2@"><


Output is a list of strings. Try it online here.

m_W%d2+*\-/d2@"><"dQ   Implicit: Q=eval(input())
Trailing "dQ inferred
m                  Q   Map [0-Q), as d, using:
/d2            Floored division of d by 2
*\-               Repeat "-" the above number of times
+                  Append to the above...
@"><"d      Modular index d into "><" - yields ">" for even d, "<" for odd
- examples: d=4 gives "-->", d=7 gives "---<"
_W                      Reverse the above if...
%d2                   ... (d % 2) != 0
Implicit print result of the map


# PowerShell, 6256 50 bytes

param($n)(0..$n|%{($j='-'*$_)+'>';"<$j"})[0..--$n]


Try it online!

Loops from 0 up to input $n, each iteration creating two arrow strings. Those are then indexed with 0..--$n to pull out the correct number of elements.

Saved 6 bytes thanks to KGlasier.

• Messing around with my own solution I found a way to cut a few bytes on yours: Can save 4 bytes by wrapping the loop in brackets and indexing directly. ie param($n)(0..$n|%{($j='-'*$_++)+'>';"<$j"})[0..--$n]. So now you don't have to write $x twice. – KGlasier Dec 12 '18 at 17:00 • Also you can save two more bytes by not using ++ in ($j='-'*$_++) as you don't use $_ anywhere else. – KGlasier Dec 12 '18 at 17:03
• @KGlasier Awesome - thanks for the obvious golfs! :) – AdmBorkBork Dec 12 '18 at 17:16

# Python 3, 53 bytes

My first codegolf answer.

lambda x:[i%2*"<"+i//2*"-"+~i%2*">"for i in range(x)]


-10 bytes thanks to Jo King

# Haskell, 51 44 bytes

-7 bytes thanks to xnor (using iterate over list-comprehension)!

(takedo b<-iterate('-':)"";[b++">",'<':b])


Try it online!

## Explanation / Ungolfed

Using do-notation saves us a concat, and using infix-notation allows a pointfree function with take, undoing these would give:

f n = take n $concat [ [b++">", '<':b] | b <- iterate ('-':) "" ]  # Japt -m, 161513 12 bytes Saved 1 byte thanks to Shaggy g<i>)iUUz ç-  Test it online ## Explanation: -m // Map the program through [0...Input); U becomes the iterative number g<i>)iUUz ç- <i> // ">" prepended with "<", creating "><" g // Get the char at index U, with index-wrapping i // Insert: U // At index U, with index-wrapping ç- // "-" repeated: Uz // U/2 times  • 12 bytes – Shaggy Jan 15 '19 at 23:59 • @Shaggy Ha! Very clever, thanks! – Oliver Jan 16 '19 at 0:15 # Jelly, 15 bytes ị⁾><;’H”-ẋƲṚ⁸¡)  Try it online! • -1 byte using tie: TIO. – HyperNeutrino Dec 14 '18 at 14:29 # MathGolf, 17 15 bytes Saved 2 bytes thanks to Jo King and Kevin Cruijssen {ï½'-*'>ï¥╛Å⌡\n  Try it online! ## Explanation The 15-byte approach is different compared to my original solution, I can't take any credit for the implementation. { start block or arbitrary length ï index of current loop, or length of last loop ½ pop a : push(a//2 if int else a/2) '- push single character "-" * pop a, b : push(a*b) '> push single character ">" ï index of current loop, or length of last loop ¥ modulo 2 ╛ if without else Å start block of length 2 ⌡ decrement twice \ swap top elements n newline char, or map array with newlines  • How does the if/else work in MathGolf? I know how the if-without-else and else-without-if statements work, but how to create an if{ ... } else{ ... } in MathGolf with ¿? (Maybe I should post this in the chat instead of here.. But I might perhaps have a save of 1 byte if I can fix the if-else.) – Kevin Cruijssen Dec 13 '18 at 10:20 • @KevinCruijssen I think it works with the next two commands/blocks. e.g. ¿12 will push 1 if true, else 2, ¿Å3*Å1+ will add one if true else triple the next element – Jo King Dec 13 '18 at 10:30 • @KevinCruijssen The if/else pops two operators or blocks from the code. Jo King is correct in his example, but you could also do ¿{"foo"}{"bar"} or ¿1{2}. – maxb Dec 13 '18 at 10:34 • @JoKing I'll add a TODO to fix the docs for the slicing operators. – maxb Dec 13 '18 at 10:35 • 15 bytes using @KevinCruijssen's solution – Jo King Dec 13 '18 at 10:46 # Japt-m, 14 bytes "<>"¬hUUz ç-)q  Try it online! Updated with a completely new method. Explanation:  #Implicitly map over the range [0..input) as U "<>" #The string "<>" ¬ #Split into the array ["<",">"] hU ) #Replace the element at index U with wrapping: - # The character '-' ç # Repeated a number of times equal to Uz # U integer divided by 2 q #Join the array to a string  • ç auto-casts its first parameter into a string, so you can drop the '. – Oliver Dec 13 '18 at 0:21 • You don't need the u method thanks to index wrapping so this can be 14 bytes. – Shaggy Dec 13 '18 at 8:13 # C (gcc), 80777674 71 bytes g(n,i,j){--n&&g(n);for(j=n%2,i=n/=2;putchar(~n?n---i*j?45:62-j*2:0););}  Try it online! -3 bytes with idea from ASCII-only. -1 with \0 instead of \n -5 rearranging parts Output includes a trailing \0. g(n,i,j){ --n&&g(n); //draw smaller arrows first (if n>1) for(j=n%2,i=n/=2; //j:!(input parity); i:arrow len-1=ceil(input)/2-1 putchar(~n //if n>=0, arrow is not yet completed ? n---i*j //if not first (j==1) or last (j==0) char of arrow: ? 45 // output '-' : 62-j*2 // otherwise, output the appropriate arrow head : 0);); //\0 after arrow complete. putchar returns 0; loop terminates }  • this might be clearer? idk – ASCII-only Jan 12 '19 at 7:12 • so close :/ – ASCII-only Jan 12 '19 at 7:52 • @ASCII-only Yeah, that should be clearer, even if it doesn't make a difference for bytecount. As for that second point.. thanks for the idea! Managed to trim down to 78 with that. – att Jan 12 '19 at 8:02 • sadly, combining ternaries doesn't help – ASCII-only Jan 12 '19 at 8:18 • XD you still have !n-- in the first codeblock – ASCII-only Jan 12 '19 at 8:22 # JavaScript (ES6), 58 bytes Returns a space-separated string. n=>(g=p=>n--?k++&1?<${p} +g(p+'-'):p+'> '+g(p):'')(k='')


Try it online!

# SNOBOL4 (CSNOBOL4), 123122 118 bytes

	N =INPUT - 1
P	H =X / 2
Y =DUPL('-',H)
OUTPUT =EQ(H,X - H) Y '>'	:S(I)
OUTPUT ='<' Y
I	X =LT(X,N) X + 1	:S(P)
END


Try it online!

# V, 22 bytes

i>
<Àñäkjjé-já-ñÀGjdG


Try it online!

• This looks like some weird scandinavian language – Pavel Dec 12 '18 at 18:02

# Charcoal, 16 bytes

ＮθＦθ«⊘ι↓>‖Ｔ»Ｆθ‖Ｔ


Try it online! Link is to verbose version of code. I had three 17-byte solutions before I eventually stumbled over this one. Explanation:

Ｎθ


Input n.

Ｆθ«


Repeat n times, 0-indexed.

⊘ι


Draw a line of -s of length half the index (truncated).

↓>


Draw the arrowhead and move to the next line.

‖Ｔ»


Reflect everything, flipping the arrowheads.

Ｆθ‖Ｔ


The above loop has n reflections, but we need an even number of reflections, so perform another n reflections.

# Clean, 76 73 bytes

import StdEnv,StdLib
$n=take n[s\\i<-inits['--'..],s<-[i++['>'],['<':i]]]  Try it online! Uses the neat fact that ['-','-'..] is the same as ['--'..] to save a bit. # JavaScript, 49 bytes f=n=>--n?f(n,l='')+(n%2? <+l:${l+='-'}>):'>'


Try it online!

• Wow, pretty cool – Limbo Dec 14 '18 at 10:41
• ...but it throws on 10000, meanwhile my ES6 solution is still works :D Anyway, your solution is very cool) – Limbo Dec 14 '18 at 10:53

# Powershell, 51 bytes

param($n)0..$n|%{'-'*$_+'>';'<'+'-'*$_}|?{$n---gt0}  # 6502 machine code (C64), 49 bytes 00 C0 20 9B B7 A2 00 8A 4A A8 90 05 A9 3C 20 D2 FF A9 2D C0 00 F0 06 20 D2 FF 88 D0 FA 8A 4A B0 05 A9 3E 20 D2 FF A9 0D 20 D2 FF E8 E4 65 D0 D7 60  Still quite a bit shorter than BASIC ;) This one has a number range only up to 255 because the natural integer size of the machine has only 8 bits. ### Online demo Usage: SYS49152,[n] (e.g. SYS49152,3 for the example from the challenge) Commented disassembly:  00 C0 .WORD$C000        ; load address
.C:c000  20 9B B7    JSR $B79B ; get unsigned byte from commandline .C:c003 A2 00 LDX #$00           ; main loop counter
.C:c005   .loop:
.C:c005  8A          TXA                ; loop counter to accumulator
.C:c006  4A          LSR A              ; divide by 2, shift lowest bit to C
.C:c007  A8          TAY                ; result to Y
.C:c008  90 05       BCC .toright       ; C clear -> counter even, skip '<'
.C:c00a  A9 3C       LDA #$3C ; load character '<' .C:c00c 20 D2 FF JSR$FFD2          ; output character
.C:c00f   .toright:
.C:c00f  A9 2D       LDA #$2D ; load character '-' .C:c011 C0 00 CPY #$00           ; counter/2 == 0 ? then no dashes
.C:c013  F0 06       BEQ .skipdashes
.C:c015   .printdashes:
.C:c015  20 D2 FF    JSR $FFD2 ; output character .C:c018 88 DEY ; decrement Y .C:c019 D0 FA BNE .printdashes ; not 0 yet -> repeat .C:c01b .skipdashes: .C:c01b 8A TXA ; loop counter to accumulator .C:c01c 4A LSR A ; shift lowest bit to C .C:c01d B0 05 BCS .toleft ; C set -> counter odd, skip '>' .C:c01f A9 3E LDA #$3E           ; load character '>'
.C:c021  20 D2 FF    JSR $FFD2 ; output character .C:c024 .toleft: .C:c024 A9 0D LDA #$0D           ; load newline character
.C:c026  20 D2 FF    JSR $FFD2 ; output character .C:c029 E8 INX ; next loop iteration .C:c02a E4 65 CPX$65            ; compare to command line argument
.C:c02c  D0 D7       BNE .loop          ; not reached yet -> repeat main loop
.C:c02e  60          RTS                ; exit


# Perl 6, 39 bytes

{map {'<'x$_%2~'-'x$_/2~'>'x$_%%2},^$_}


Try it online!

Anonymous code block that returns a list of lines.

# K (ngn/k), 31 29 bytes

{"<->"x#2,x{(1=*x)_1,2-|x}\0}


Try it online!

first we generate lists with 0 instead of "<", 1 instead of "-", and 2 instead of ">":

{ } function with argument x

x{...}\0 apply the inner function x times, starting with an initial value of 0 and preserving intermediate results

|x reverse

2- replace 0 with 2 and vice versa, keep the 1s as they are

1, prepend a 1

(1=*x)_ is the first of x equal to 1? if yes, drop one element, otherwise drop 0 elements (do nothing)

2, prepend a 2 for the initial ">" arrow

x# we have a little too many lists, so take only the first x of them

"<->" use the lists' elements (0/1/2) as indices in this string

• I would like to ask for an explanation (I haven't started learning K yet, I don't know which version to start with...) – Galen Ivanov Dec 13 '18 at 12:14
• @GalenIvanov i tried to write an explanation, i hope it makes sense. thanks for your interest in my favourite language :) there are multiple implementations with different advantages and disadvantages (kx's original, kona, oK and i'm working on my own). would you like to join the apl chat room so i can give you more details? – ngn Dec 13 '18 at 12:44
• Thank you, I'm already there – Galen Ivanov Dec 13 '18 at 12:49

# 05AB1E, 23 20 bytes

FNÉD„><è'-N;∍«s_iR},


Try it online!

First time using 05AB1E or any other golfing language for that matter. Any ideas welcome.

-3 from Kevin Cruijssen

• Welcome to the world of 05AB1E, and nice first answer. +1 from me. :) "><" can be „>< to save a byte. There are builtins for 1, 2, and 3 char strings, being ', „, and … respectively. Here is a 18 bytes alternative I came up with, but perhaps it could be golfed a bit more. If you haven't seen it yet, we have a tips for golfing in 05AB1E page, and also feel free to ask anything in the chat. – Kevin Cruijssen Dec 13 '18 at 8:54
• @KevinCruijssen Thanks so much for your ideas. I don't feel right just using your code, as it feels fairly different from mine, but I did use the idea of modulo 2 as checking for if a number is odd. I also use the two char string idea. I would not mind at all if you posted the 18 byte version on your own. – nedla2004 Dec 13 '18 at 23:09
• I've posted my answer in that case. :) – Kevin Cruijssen Dec 14 '18 at 8:31

# C# (.NET Core), 90 bytes

a=>{for(int i=0;i<a;i++){var s=new String('-',i/2);Console.WriteLine(i%2<1?s+">":"<"+s);}}


Try it online!

Uses an Action delegate to pull in the input and not require a return.

Ungolfed:

a => {
for(int i = 0; i < a; i++)          // from [0, a)
{
var s = new String('-', i / 2);     // create string of dashes of length (a / 2)
Console.WriteLine(i % 2 < 1 ?       // write arrow
s + ">" :       // if i is even: dashes plus ">"
"<" + s);       // if i is odd: "<" plus dashes
}
}


# ES6, 968279 70 bytes

Try it online! (Thanks to @Oliver)

n=>[...Array(n)].map((_,i)=>(i%2?"<":"")+"-".repeat(i/2)+(i%2?"":">"))

• Welcome to PPCG! By default, taking input as a variable is disallowed; you have to either make it a function (just stick a i=> in front of your code!) or from a command-line argument or STDIN or something. – HyperNeutrino Dec 14 '18 at 14:40
• @HyperNeutrino okay, edited answer. However, the most voted answer contains only the body of the function, but ok. Anyway I'm outsider) – Limbo Dec 14 '18 at 15:09
• Can you link it? I don't think any of them are invalid, at least not the top few. – HyperNeutrino Dec 14 '18 at 15:16
• A few more bytes: Try it online – Oliver Dec 14 '18 at 16:15
• A few more bytes if you re-arrange that last ternary operator and remove the center parenthesis: Try it online – Oliver Dec 14 '18 at 16:59

# Red, 109 108 bytes

-1 byte thanks to NK1406

func[n][repeat i n[print reduce[pick[pad/with pad/left/with]k: i% 2 + 1
pick copy"<>"k i / 2 + k - 1 #"-"]]]


Try it online!

• -1 for removing a space – NK1406 Dec 15 '18 at 18:28
• @NK1406 Thank you! I didn't know this is valid. – Galen Ivanov Dec 15 '18 at 19:01