60
\$\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\$
  • \$\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\$ – FryAmTheEggman Jun 14 '17 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 Jun 14 '17 at 20:43
  • 41
    \$\begingroup\$ This question can be alternatively titled "Diet Haskell". \$\endgroup\$ – March Ho Jun 15 '17 at 14:42
  • 1
    \$\begingroup\$ May we use " instead of ' (e.g. "a" -> "a":[])? \$\endgroup\$ – caird coinheringaahing Apr 9 '18 at 17:31
  • 2
    \$\begingroup\$ @cairdcoinheringaahing No, " and ' are syntactically different. \$\endgroup\$ – Wheat Wizard Apr 9 '18 at 17:32

79 Answers 79

1 2
3
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':[]
| improve this answer | |
\$\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':[]
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Octave, 37 bytes

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

Try it online!

| improve this answer | |
\$\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
| improve this answer | |
\$\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").)

| improve this answer | |
\$\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)
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 3, 36 bytes

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

A variation of Rod's one.

Try it online!

| improve this answer | |
\$\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 "[]"
;
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Clojure, 42 bytes

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

Javascript 33 bytes

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

><>, 25 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can save a couple of bytes by taking input through the -s flag \$\endgroup\$ – Jo King Apr 11 '18 at 6:41
0
\$\begingroup\$

Pari/GP, 52 bytes

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

Try it online!

| improve this answer | |
\$\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!

| improve this answer | |
\$\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':[])
| improve this answer | |
\$\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 []
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ snail remember \$\endgroup\$ – 12Me21 Apr 10 '18 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':[]"
| improve this answer | |
\$\endgroup\$
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':[]"
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Rockstar, 94 bytes

listen to S
X's0
O's""
while S at X
let C be S at X
let O be+"'"+C+"':"
let X be+1

say O+"[]"

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

| improve this answer | |
\$\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.
| improve this answer | |
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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