r/purescript • u/DeepDay6 • Aug 30 '23
Recursion over Arrays or Strings?
One of my favourite/most used pattern is recursing over lists, like
f :: List InputType -> List OutputType
f mylist = case mylist of
(x:xs) -> dothingsto x : f xs
I'm still learning PureScript, and I'm struggling to do that with Arrays or Strings. As far as I understand, I can't pattern match on arrays in a [x:xs] kind of way, as xs in this case will only match the complete rest of the array exactly.
For Strings, at least Data.String has no cons. Of course I could List.fromFoldable <<< String.split (Pattern "") to get a List or similarly create a listf of Chars, but being able to do that without would be much cleaner. I'm sure I just don't know enough yet :D
Also, Pursuit is quite a rabbit hole if one searches type signatures...
View all comments
Show parent comment
2
u/DeepDay6 Aug 31 '23
Thanks for that explanation. I was indeed doing codewars exercises when I found it a little unelegant to cast strings to lists just to match them. Mapping was not an option for the particular problem and I got stuck trying to find simpler ways to match instead of taking a step back and maybe use a fold (are strings foldable? I'll have to look that up...). It's not a rant, but was meant a serious question - I thought I had missed something.