62
\$\begingroup\$

In Haskell the list notation:

[a,b,c]

Is just syntactic sugar for:

a:b:c:[]

And the string notation:

"abc"

Is just syntactic sugar for:

['a','b','c']

This means that the string:

"abc"

Is the same as:

'a':'b':'c':[]

Task

Given a string you should output what the de-syntaxed version would look like in Haskell.

Rules

  • You will receive a string by any valid input method, you should output a string ending with :[] with every character from the input surrounded by ' and separated by :. The empty string should output [].

  • You can assume that you will not receive any characters that require escaping (e.g. ', newlines, tabs ...) and that input will be in the printable ascii range

  • This is you should aim to minimize the byte count of your answer

Test Cases

"" -> []
"a" -> 'a':[]
"Hello, World" -> 'H':'e':'l':'l':'o':',':' ':'W':'o':'r':'l':'d':[]   
\$\endgroup\$
12
  • \$\begingroup\$ Will the input ever have non-ascii values? Your restriction on characters that require escaping either requires we know which characters Haskell will escape or assumes your list is exhaustive. \$\endgroup\$ Commented Jun 14, 2017 at 19:05
  • 7
    \$\begingroup\$ @totallyhuman Those are not even valid Haskell. If they were maybe, but nice they are not, definitely no. \$\endgroup\$
    – Wheat Wizard
    Commented Jun 14, 2017 at 20:43
  • 43
    \$\begingroup\$ This question can be alternatively titled "Diet Haskell". \$\endgroup\$
    – March Ho
    Commented Jun 15, 2017 at 14:42
  • 1
    \$\begingroup\$ May we use " instead of ' (e.g. "a" -> "a":[])? \$\endgroup\$ Commented Apr 9, 2018 at 17:31
  • 2
    \$\begingroup\$ @cairdcoinheringaahing No, " and ' are syntactically different. \$\endgroup\$
    – Wheat Wizard
    Commented Apr 9, 2018 at 17:32

85 Answers 85

1 2
3
1
\$\begingroup\$

Rockstar, 94 69 bytes

listen to S
cut S
O's ""
while S
let O be+"'"+roll S+"':"

say O+"[]"

Try it here (Code will need to be pasted in)

\$\endgroup\$
1
\$\begingroup\$

Lua, 36 bytes

print((...):gsub(".","'%1':").."[]")

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Swift 5.9, 45 bytes

let f={($0+"").map{"'\($0)':"}.joined()+"[]"}
\$\endgroup\$
0
\$\begingroup\$

Jelly, 13 bytes

”';Ѐ;€⁾':⁾[]

Try it online!

Explanation:

”';Ѐ;€⁾':⁾[]

”';Ѐ            Concatenate ' to the beginning of each element
     ;€⁾':       Concatenate ': to the end of each element
          ⁾[]    Implicit concatenation of literal "[]"
\$\endgroup\$
2
  • \$\begingroup\$ This doesn't appear to add single quotes around each character. \$\endgroup\$ Commented Jun 14, 2017 at 19:15
  • \$\begingroup\$ @FryAmTheEggman Noticed as soon as I posted it. Fixing now. \$\endgroup\$ Commented Jun 14, 2017 at 19:16
0
\$\begingroup\$

CJam, 13 12 bytes

q{`':`}%"[]"

Explanation:

q    e# Input: "test"
{    e# For each character: ['t 'e 's 't]
  `  e#   Get the string representation: ["'t" "'e" "'s" "'t"]
  ': e#   Push the character ':': ["'t" ': "'e" ': "'s" ': "'t" ':]
  `  e#   Get the string representation: ["'t" "':" "'e" "':" "'s" "':" "'t" ':]
}%   e# End
"[]" e# Push the string "[]"
e# Implicit print: 't':'e':'s':'t':[]
\$\endgroup\$
0
\$\begingroup\$

braingasm, 18 bytes

,[39.."':".,]"[]".

Just a simple loop, reading one byte at a time and printing the necessary stuff before and after the input, then the final square brackets.

Test run:

$ echo -n Hello | braingasm haskell_strings.bg 
'H':'e':'l':'l':'o':[]
\$\endgroup\$
0
\$\begingroup\$

Octave, 37 bytes

@(s)(s&&printf("'%c':",s))+puts("[]")

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Braingolf, 29 bytes

VRl1-M[R>#'<"':">>>>v]R"[]"&@

Try it online!

Currently a bug in Braingolf foreach loops that breaks them when handling 2 adjacent duplicates (such as the ls in Hello), so I have to use this method that costs me an extra 13 bytes

If foreach loops worked properly, it would've been this, which is only 16 bytes:

{>#'<"':"}"[]"&@

Explanation

VRl1-M[R>#'<"':">>>>v]R"[]"&@  Implicit input from commandline args
VRl1-M                         Create stack2, push length of stack1 - 1 to stack2
      [..............]         Do-While loop, will run for each item on stack1
       R>                      Return to stack2, move ToS to bottom
         #'<                   Push single quote and move BoS back to top
            "':"               Push single quote and colon
                >>>>           Rotate stack to next item from input
                    v          Switch to stack2 for loop counting
                      R"[]"&@  Switch to stack1, push [] and print entire stack
\$\endgroup\$
0
\$\begingroup\$

Groovy, 37 bytes

{it.replaceAll(~/./,{"'$it':"})+"[]"}

(It's an anonymous closure, which can be called thus {...}("hello") or via a name thus f = {...};f("hello").)

\$\endgroup\$
0
\$\begingroup\$

QBIC, 35 bytes

[_l;||X=X+@'`+_sA,a,1|+B+@:`]?X+@[]

Explanation

[    |         FOR a = 1 TO
 _l |           the length of
   ;              input string
X=X+@'`        Add to Z$ a literal '
   +_sA,a,1|   and a 1-char substring at index a of the input A$
   +B          and another quote (defining it as literal created B$)
   +@:`        and a literal :
]              NEXT
?X+@[]         PRINT Z$ and a literal [] (no closing backtick because EOF)
\$\endgroup\$
0
\$\begingroup\$

Python 3, 36 bytes

lambda s:':'.join(map(repr,[*s,[]]))

A variation of Rod's one.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

setlx, 36 bytes

s|->join(["'"+x+"':":x in s],"")+"[]";

Explanation:

s |->                               // a function taking an argument s
    join(                           // join:
        [ "'" + x + "':" : x in s], // a list from each char wrapped in quotes
        ""                          // with the empty string
    ) 
    + []                            // add an empty list to the string, this will convert it to the string "[]"
;
\$\endgroup\$
0
\$\begingroup\$

Clojure, 42 bytes

#(str(apply str(for[c %](str"'"c"':")))[])
\$\endgroup\$
0
\$\begingroup\$

Javascript 33 bytes

f=s=>s.replace(/(.)/g,"'$1':")+'[]'
\$\endgroup\$
0
\$\begingroup\$

><>, 25 bytes

i:0(?v":''"o{ooo
o"[]"<;o

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can save a couple of bytes by taking input through the -s flag \$\endgroup\$
    – Jo King
    Commented Apr 11, 2018 at 6:41
0
\$\begingroup\$

Pari/GP, 52 bytes

s->c=concat;c(c([Str("'",x,"':")|x<-Vec(s)],["[]"]))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 81 bytes

	N =INPUT
S	N LEN(1) . X REM . N	:F(O)
	O =O "'" X "':"	:(S)
O	OUTPUT =O "[]"
END

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Actually, 9 bytes

♂è[]@q':j

Try it online!

Explanation:

♂è[]@q':j
           implicit input ("abc")
♂è         push a list containing the string representation of each character (['a', 'b', 'c'])
  []@q     add an empty list to the end of that list (['a', 'b', 'c', [])
      ':j  join with colons ("'a':'b':'c':[]")
           implicit print ('a':'b':'c':[])
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC 3, 53 48 bytes

-5 with the WHILE trick, thanks 12Me21!

LINPUT S$WHILE""<S$?"'"+SHIFT(S$)+"':";
WEND?"[]

Explainer:

LINPUT S$              'Read line from console
WHILE ""<S$            'WHILE S$ is not empty
 ?"'"+SHIFT(S$)+"':";  'Print 'x': where x is popped off the front of S$
WEND                   'End WHILE
?"[]"                  'Print []
\$\endgroup\$
1
  • \$\begingroup\$ snail remember \$\endgroup\$
    – 12Me21
    Commented Apr 10, 2018 at 0:22
0
\$\begingroup\$

Husk, 9 bytes

J':`:søms

Try it online!

Explanation

J':`:søms  Implicit input                                 "abc"
       ms  String representation of each character        ["'a'","'b'","'c'"]
   `:sø    Append the string of an empty list to the end  ["'a'","'b'","'c'","[]"]
J':        Join with a colon character                    "'a':'b':'c':[]"
\$\endgroup\$
0
\$\begingroup\$

F#, 55 bytes

My first F#, which means it probably isn't very good. My SmileBASIC answer is beating it, even...

fun s->List.reduce(+)[for c in s->sprintf"'%c':"c]+"[]"

Try it online. Golfing tips are appreciated

\$\endgroup\$
1
  • \$\begingroup\$ The space before the last c can be dropped. \$\endgroup\$ Commented Apr 11, 2018 at 6:25
0
\$\begingroup\$

Add++, 30 bytes

D,f,@*,39@39 58
L^~,€{f}"[]"BF

Try it online!

How it works

D,f,@*,		; Create a function, 'f', that
		; that takes one argument, and returns the full stack
		; Example argument:	['a']
	39@	; Prepend 39;	STACK = [39 'a']
	39 58	; Push 39, 58;	STACK = [39 'a' 39 58]
		; Return the full stack
		; 39 is the ordinal of ' and 58 of :

L^~,		; Create a lambda function
		; The ^ converts all integers to characters, then concatenates, when returning
		; The ~ unpacks the argument beforehand
		; Example argument:		['a' 'b' 'c']
	€{f}	; Run 'f' over each;	STACK = [[39 'a' 39 58] [39 'b' 39 58] [39 'c' 39 58]]
	"[]"	; Push '[]';		STACK = [[39 'a' 39 58] [39 'b' 39 58] [39 'c' 39 58] '[]']
	BF	; Flatten;		STACK = [39 'a' 39 58 39 'b' 39 58 39 'c' 39 58 '[]']
		; The ^ flag converts integers to characters:
		;			STACK = ["'" 'a' "'" ':' "'" 'b' "'" ':' "'" 'c' "'" ':' '[]']
		; Then concatenates them into a single string:
		;			STACK = ["'a':'b':'c':[]"]
		; And returns the top value:
		;			Return "'a':'b':'c':[]"
\$\endgroup\$
0
\$\begingroup\$

Pip, 15 bytes

aJ':JW''AL":[]"

Try it online!

Explanation

aJ':JW''AL":[]" a → input                     Example → "Hi"
aJ':            join characters with ':'              → 'H:i"
    JW''        join and wrap that with single quotes → "'H':'i'"
        AL":[]" Append list ":[]"                     → "'H':'i':[]"
                Concatenation has high precedence, so AL is used.
\$\endgroup\$
0
\$\begingroup\$

Vyxal P, 75 bitsv1, 9.375 bytes

\'vø.¾SJ\:j

Try it Online!

I'm not going to sugarcoat it - you can press c to golf your answers

Explained

\'vø.¾SJ\:j
\'vø.       # Surround each character with "'"
     ¾SJ    # append the string "[]" to that (the global array is initially empty, and the P flag stringify lists as square brackets instead of angle brackets)
        \:j # join on ":" and output
\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 13 bytes

ʋ':'j''ṛʋ:[]+

Attempt This Online!

Explanation

ʋ':'j''ṛʋ:[]+  # Implicit input
ʋ':'j          # Join the input by the string "':'"
     ''ṛ       # Surround this string by "'"
        ʋ:[]+  # Append the string ":[]"
               # Implicit output
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.