Junilu Lacar wrote:Liu, this is the Racket solution, right? A little inscrutable for these Java/Kotlin eyes but it certainly is interesting, if only for its conciseness. Is cons on Line 5 like the Scala cons?
Yes, it is Racket. Indeed, it is significantly different. But you would find it fairly easy to grasp if you'd have touched Clojure to some good extent in the past. The latter is JVM language and undoubtedly much better known in industry. Those languages are similar in a way like
Java and Kotlin are.
I saw on some social media (it was Twitter actually) where Uncle Bob (Robert C. Martin) in some past AoC years was solving those in Clojure.
I don't recall myself using cons in Scala in the past, and can't find documentation in intuitive way at the moment (no one can beat javadocs here), so don't want to mislead, but Racket's cons really makes a pair, or in that line 5 case, adds a number to an existing list, which in racket is a pair.
So it may sound weird that list
'(1 2 3 4) is a pair, which you can access/slice it in any way you want. i.e.:
(car '(1 2 3 4)) => 1
(cdr '(1 2 3 4)) => '(2 3 4)
or
(first '(1 2 3 4)) => 1
(rest '(1 2 3 4)) => '(2 3 4)
more:
(cadr '(1 2 3 4)) => 2
(caddr '(1 2 3 4)) => 3
(cddr '(1 2 3 4)) => '(3 4)
(cdddr '(1 2 3 4)) => '(4)
And many other ways to achieve above, i.e. using
take,
drop.
No wonder why Lisp stands for List processing