Created
March 2, 2018 08:24
-
-
Save germ13/6cbaeb7bd5cc9ca39539c081be9bf7c0 to your computer and use it in GitHub Desktop.
Codewars Kata: Shell Game (Clojure)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns the-shell-game) | |
;;https://www.codewars.com/kata/the-shell-game/clojure | |
(defn switch [location swap] | |
(cond (= location (first swap)) | |
(last swap) | |
(= location (last swap)) | |
(first swap) | |
:else | |
location)) | |
(defn find-the-ball [start swaps] | |
"Given the starting position and a list of swaps, find the final position" | |
(if (nil? (first swaps)) | |
start | |
(find-the-ball | |
(switch start (first swaps)) | |
(rest swaps)))) |
ETA: ohh you know what, I think I see it more clearly now... (switch start (first swaps)) on 18 returns another position, so you're passing in another position and the cdr of the vector. Got it.
Okay, I can put my confusion into words now that my brain is more awake. What is (rest swaps) on 19 doing? Does it have anything to do with how this is iterating over the vector? In other words, how is (first swaps) changing each time find-the-ball gets called?
Maybe I'm restating what you just said.
Line 18 and 19 are the new values (the new state) in the recursive call.
(switch start (first swaps)) ;; calculates the new location based on the first swap.
(rest swaps) ;; first swap was processed in previous line, only process remaining swaps.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what dont' you "like"? @winniecluk