This solution is probably not idiomatic at all. I just wanted to use a stream like in my previous Racket solution to see how different would that be in Clojure.
This was the Racket version:
As you see they are quite different.
letfn is somehow equivalent to Racket's letrec.
My biggest problem was with cons because they behave different.
Whereas in Racket you can do:
> (cons 1 2) '(1 . 2)in Clojure an exception is raised when you try the same:
user=> (cons 1 2) IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:505)You need to write this instead:
user=> (cons 1 (cons 2 nil)) (1 2)So in the end I used seq:
user=> (seq [1 2]) (1 2)
Another difference is that you have an integer overflow in Clojure unless you use BigInt, whereas in Racket number can be arbitrarily large.
Well, now that I've tried this. I'll try writing a more idiomatic version.
You can nitpick my solution here or see all the exercises I've done so far in this repository.
No comments:
Post a Comment