h n= h is a function of type Int -> IO () mapM_ mapM_ executes a function returning monadic actions on all objects in a list, in order. Then it executes these actions, in order. For this code, it transforms each value in the list into a monadic action that prints the corresponding line ( the function consists of two components putStrLn the second part is printing the result of the first part to stdout . concatenating both components (\k-> the first parts first prints (n-k) spaces and then (n+k-1) asterisks ([k..n]>>" ") create the list of the integers from k to n (That is actually one more entry than necessary, but just results in a leading whitespace per line, while saving 2 bytes compared to [1..n-k]). Then create a new list where each element of that first list is replaced with the string " " and concatenate that result into one string ++ concatenate both lists ([2..n+k]>>"* ") create the list of the integers from 2 to n+k (of length n+k-1). Then create a new list where each element of that first list is replaced with the string "* " and concatenate that result into one big string ) ) ([1..n-1]++[n,n-1..1]) the list simply goes from 1 to n and back, supplying the k h n= h is a function of type Int -> IO () mapM_ mapM_ executes a function returning monadic actions on all objects in a list, in order. Then it executes these actions, in order. For this code, it transforms each value in the list into a monadic action that prints the corresponding line ( the function consists of two components putStrLn the second part is printing the result of the first part to stdout . concatenating both components (\k-> the first parts first prints (n-k) spaces and then (n+k-1) asterisks ([k..n]>>" ") create the list of the integers from k to n (That is actually one more entry than necessary, but just results in a leading whitespace per line, while saving 2 bytes). Then create a new list where each element of that first list is replaced with the string " " and concatenate that result into one string ++ concatenate both lists ([2..n+k]>>"* ") create the list of the integers from 2 to n+k (of length n+k-1). Then create a new list where each element of that first list is replaced with the string "* " and concatenate that result into one big string ) ) ([1..n-1]++[n,n-1..1]) the list simply goes from 1 to n and back, supplying the k h n= h is a function of type Int -> IO () mapM_ mapM_ executes a function returning monadic actions on all objects in a list, in order. Then it executes these actions, in order. For this code, it transforms each value in the list into a monadic action that prints the corresponding line ( the function consists of two components putStrLn the second part is printing the result of the first part to stdout . concatenating both components (\k-> the first parts first prints (n-k) spaces and then (n+k-1) asterisks ([k..n]>>" ") create the list of the integers from k to n (That is actually one more entry than necessary, but just results in a leading whitespace per line, while saving 2 bytes compared to [1..n-k]). Then create a new list where each element of that first list is replaced with the string " " and concatenate that result into one string ++ concatenate both lists ([2..n+k]>>"* ") create the list of the integers from 2 to n+k (of length n+k-1). Then create a new list where each element of that first list is replaced with the string "* " and concatenate that result into one big string ) ) ([1..n-1]++[n,n-1..1]) the list simply goes from 1 to n and back, supplying the k Haskell, 9999 9797 79 bytes
Haskell, 99 97 79 bytes
Haskell, 99 97 79 bytes
Haskell, 99 9797 79 bytes
h n=foldr(>>)(return())$mapn=mapM_(putStrLn.(\k->([k..n]>>" ")++([2..n+k]>>"* ")))([1..n-1]++[n,n-1..1]) h n= h is a function of type Int -> IO () foldrmapM_ this foldmapM_ receivesexecutes a list of monadic actions, each of which prints one line of the hexagon. foldr folds these actions into one resulting value, which will be thefunction printingreturning of the complete hexagon (>>) to combine twomonadic actions, weon simplyall executeobjects them after one another (return()) we initialize the fold with in a monadic action that does nothing $map list, in order. Then mapit executes a function on all objects in athese listactions, in order. For this code, it transforms each value in the list into a monadic action that prints the corresponding line ( the function consists of two components putStrLn the second part is printing the result of the first part to stdout . concatenating both components (\k-> the first parts first prints (n-k) spaces and then (n+k-1) asterisks ([k..n]>>" ") create the list of the integers from k to n (That is actually one more entry than necessary, but just results in a leading whitespace per line, while saving 2 bytes). Then create a new list where each element of that first list is replaced with the string " " and concatenate that result into one string ++ concatenate both lists ([2..n+k]>>"* ") create the list of the integers from 2 to n+k (of length n+k-1). Then create a new list where each element of that first list is replaced with the string "* " and concatenate that result into one big string ) ) ([1..n-1]++[n,n-1..1]) the list simply goes from 1 to n and back, supplying the k (\k-> ([1..n-k]>>" ") create the list of the integers from 1 to n-k. Then create a new list where each element of that first list is replaced with the string " " and concatenate that result into one string ++ concatenate both lists ([2..n+k]>>"* ") create the list of the integers from 2 to n+k (of length n+k-1). Then create a new list where each element of that first list is replaced with the string "* " and concatenate that result into one big string ) Edit: Switched to mapM_. I was not aware that was available without using import
Haskell, 99 97 bytes
h n=foldr(>>)(return())$map(putStrLn.(\k->([k..n]>>" ")++([2..n+k]>>"* ")))([1..n-1]++[n,n-1..1]) h n= h is a function of type Int -> IO () foldr this fold receives a list of monadic actions, each of which prints one line of the hexagon. foldr folds these actions into one resulting value, which will be the printing of the complete hexagon (>>) to combine two actions, we simply execute them after one another (return()) we initialize the fold with a monadic action that does nothing $map map executes a function on all objects in a list, in order. For this code, it transforms each value in the list into a monadic action that prints the corresponding line ( the function consists of two components putStrLn the second part is printing the result of the first part to stdout . concatenating both components (\k-> the first parts first prints (n-k) spaces and then (n+k-1) asterisks ([k..n]>>" ") create the list of the integers from k to n (That is actually one more entry than necessary, but just results in a leading whitespace per line, while saving 2 bytes). Then create a new list where each element of that first list is replaced with the string " " and concatenate that result into one string ++ concatenate both lists ([2..n+k]>>"* ") create the list of the integers from 2 to n+k (of length n+k-1). Then create a new list where each element of that first list is replaced with the string "* " and concatenate that result into one big string ) ) ([1..n-1]++[n,n-1..1]) the list simply goes from 1 to n and back, supplying the k (\k-> ([1..n-k]>>" ") create the list of the integers from 1 to n-k. Then create a new list where each element of that first list is replaced with the string " " and concatenate that result into one string ++ concatenate both lists ([2..n+k]>>"* ") create the list of the integers from 2 to n+k (of length n+k-1). Then create a new list where each element of that first list is replaced with the string "* " and concatenate that result into one big string ) Haskell, 99 97 79 bytes
h n=mapM_(putStrLn.(\k->([k..n]>>" ")++([2..n+k]>>"* ")))([1..n-1]++[n,n-1..1]) h n= h is a function of type Int -> IO () mapM_ mapM_ executes a function returning monadic actions on all objects in a list, in order. Then it executes these actions, in order. For this code, it transforms each value in the list into a monadic action that prints the corresponding line ( the function consists of two components putStrLn the second part is printing the result of the first part to stdout . concatenating both components (\k-> the first parts first prints (n-k) spaces and then (n+k-1) asterisks ([k..n]>>" ") create the list of the integers from k to n (That is actually one more entry than necessary, but just results in a leading whitespace per line, while saving 2 bytes). Then create a new list where each element of that first list is replaced with the string " " and concatenate that result into one string ++ concatenate both lists ([2..n+k]>>"* ") create the list of the integers from 2 to n+k (of length n+k-1). Then create a new list where each element of that first list is replaced with the string "* " and concatenate that result into one big string ) ) ([1..n-1]++[n,n-1..1]) the list simply goes from 1 to n and back, supplying the k Edit: Switched to mapM_. I was not aware that was available without using import