4 ms·
Hmm, some questions from someone who isn't familiar with Swift or much functional programming: - What does this have to do with Swift? Other commenters seem to
by n4r9 8y ago
Hmm, some questions from someone who isn't familiar with Swift or much functional programming:
- What does this have to do with Swift? Other commenters seem to agree that Haskell would be more appropriate.
- What does this have to do with functional programming? As far as I can tell, an algorithm about bitstrings has been wrapped up and reframed in terms of types. Could you not do this in C without too much headache?
- The recursion aspect confuses me a bit; is it possible to "find" a predicate like "all indices of the bitstring are 1"? Or is that not an allowed type of predicate?
- Again with the recursion, how does the "find" procedure know to terminate if no viable bit string exists?
EDIT: I should have read kccqzy's post more carefully. I believe it answers the third and fourth questions.
- saagarjha 8y agoFor the first two: not much, and somewhat. The commenters are correct that this logic is much more easily expressed in Haskell, since it has better support for this kind of programming. Swift is able to do this because it has lazy evaluation as a opt-in feature as well as the parts of the type system that matter; I don’t think you would be able to port this to C easily.
- kccqzy 8y agoFor your third question, that predicate doesn't work. For your fourth question, I didn't quite explain that but it's due to laziness. All of this recursion happens inside a lazy function argument. So if p chooses to return True without inspecting more values from the bit sequence, the recursion is then stopped.