Pad a jagged array to be square

Haskell, 67 bytes

m%v|let l?x=take(maximum$length<$>(1<$m):m)$l++repeat x=(?v)<$>m?[]

Try it online!


R, 80 90 93 88 bytes

Edit: -5 bytes thanks to Kirill L., but only after +10 bytes to fix bug, and +3 more bytes after belatedly realising that the fill-value should be given as input...

function(l,f,m=matrix(f,d<-max(0,seq(l),lengths(l)),d)){for(i in l)m[F<-F+1,seq(i)]=i;m}

Try it online!


K (ngn/k), 21 19 bytes

{y^x .!'2#|/#'x,,x}

Try it online!

  • |/#'x,,x get the size of the output (i.e. the maximum of the count of each row, and of the number of rows)
  • !'2# build a list of two copies of 0..size (e.g. (0 1 2 3;0 1 2 3))
  • x . dot-apply into the input (out of bound accesses result in 0Ns)
  • y^ replace nulls with the fill number