可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Can somebody give an example how to use ScalaZ Free monad ? For example, if I have a simple State function and want to apply it 10,000 times, I'd get StackOverflowError: def setS(i: Int) :State[List[Int], Unit] = State { l => ( i::l, () ) } val state = (1 to 10000).foldLeft( put(Nil :List[Int]) ) { case (st, i) => st.flatMap(_ => setS(i)) } state(Nil) As I understand, Free monad can help avoid this. How would I re-write this piece of code using Free monad to not cause stack overflow ? 回答1: As I say in a comment above, lifting the State