55
6
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 rangeThis is code-golf 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':[]
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. – FryAmTheEggman – 2017-06-14T19:05:15.457
@FryAmTheEggman You can assume they are in the ascii range – Post Rock Garf Hunter – 2017-06-14T19:06:42.527
Does the string come in surrounded by
"
? Or is that just to signify that it's a string? – nmjcman101 – 2017-06-14T19:08:44.557@nmjcman101 Just there to indicate its a string (hard to represent the empty string otherwise). – Post Rock Garf Hunter – 2017-06-14T19:09:33.520
Is
'a':'':[]
an acceptable output fora
and'':[]
an acceptable output for an empty string? – totallyhuman – 2017-06-14T20:40:53.2137@totallyhuman Those are not even valid Haskell. If they were maybe, but nice they are not, definitely no. – Post Rock Garf Hunter – 2017-06-14T20:43:26.590
printable ascii? – Titus – 2017-06-15T03:51:02.413
@Titus Yes, I'm pretty sure all of the unprintable ascii needs to be escaped. Anything that has to be escaped in Haskell doesn't need to be suported. – Post Rock Garf Hunter – 2017-06-15T03:54:07.823
You should probably specify (printable) ASCII explicitly in the question, as there are a lot of higher Unicode characters that don't need escaping. The rules are somewhat weird and based on Unicode character classes. (The exceptions are just enough spread around to make compression with string literals awkward.) – Ørjan Johansen – 2017-06-15T04:09:22.610
38This question can be alternatively titled "Diet Haskell". – March Ho – 2017-06-15T14:42:34.607
May we use
"
instead of'
(e.g."a" -> "a":[]
)? – caird coinheringaahing – 2018-04-09T17:31:52.3831@cairdcoinheringaahing No,
"
and'
are syntactically different. – Post Rock Garf Hunter – 2018-04-09T17:32:43.363