From 4f66803ab709cbe56f4d2cd165e8a5d440936e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=A9v=C3=A9?= Date: Fri, 20 Nov 2020 14:12:49 +0000 Subject: [PATCH] Updates mutual recursion example (#983) so it can be executed without blowing the stack. --- examples/mutual_recursion.carp | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/mutual_recursion.carp b/examples/mutual_recursion.carp index b87b27f2..e9e0800f 100644 --- a/examples/mutual_recursion.carp +++ b/examples/mutual_recursion.carp @@ -1,18 +1,19 @@ -(use IO) +; This is dangerous because you've now told the Carp compiler that `f` +; _will_ exist. So you won't get a compile error from Carp if you don't +; implement it or change the signature. You should get a error in the C +; compiler though. +(register f (Fn [Int] Int)) -(register f a) +(defn-do g [i] + (println* "I'm g") + (f (inc i))) -(defn g [] - (do - (println "I'm g") - (f))) - -(defn f [] - (do - (println "I'm f") - (g))) +(defn-do f [i] + (println* "I'm f") + (if (> i 10) + i + (g (inc i)))) (defn main [] - (do - (f) - 0)) ;; Need a return value to get a concrete type. + (println* (fmt "Final result: %i" (g 0)))) +