How do I convert a list of chars to a string in purescript

♀尐吖头ヾ 提交于 2019-12-12 11:25:00

问题


I'm looking for an idiomatic way to write a function List Char -> String in Purescript.

This seems like a simple thing to do, but I'm new to Purescript and have been browsing documentation for a while now with no progress!

Background information: I am porting a simple function from Haskell to Purescript

generateId :: Int -> [Char]

This generates a String of specified length. It was quite easy to convert the code to use List Char operations (where List is from Data.List in Purescript). In Haskell [Char] is the same as String so no other processing is needed, however, I can't find a function to convert from List Char to a native String in Purescript!

My search lead me to fromCharArray :: Array Char -> String in Data.String, however I could not find a way to convert from List Char to an Array Char!

I could manually convert between them by folding over List Char and building an Array Char using snoc, but surely I must be missing an inbuilt solution for what seems like basic String manipulation in Purescript!

Edit: fromList works to convert from any Unfoldable (such as Arrays) to a List. Still leaving this question open in case there is a more idiomatic way of achieving this.


回答1:


I agree with your edit. Data.String.fromCharArray <<< Data.List.fromList sounds pretty decent to me. fromCharArray is implemented in native JS with array.join("").

Update: fromList is deprecated now - use toUnfoldable instead



来源:https://stackoverflow.com/questions/34565039/how-do-i-convert-a-list-of-chars-to-a-string-in-purescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!