Reverse a sequence Difficulty: Easy Topics segs core-functions Write a function
ID: 3804537 • Letter: R
Question
Reverse a sequence Difficulty: Easy Topics segs core-functions Write a function which reverses a sequence. O [1 2 3 4 5]) [5 4 3 2 11) O sorted-set 5 7 2 7)) (7 5 2)Explanation / Answer
user=> (def my-number 5678) #'user/my-number user=> (def your-number 1045) #'user/your-number user=> (+ my-number your-number) 6723 user=> ; we can bind various things, for example.. user=> (+ "a" "b") ; + works only for numbers in Clojure ClassCastException java.lang.String cannot be cast to java.lang.Number clojure.lang.Numbers.add (Numbers.java:126) user=> (str "a" "b") ; str function concatenates strings, so ... "ab" user=> (def original-plus +) ; saves its original one to make it back later #'user/original-plus user=> (def + str) ; binds + to str function WARNING: + already refers to: #'clojure.core/+ in namespace: user, being replaced by: #'user/+ #'user/+ user=> (+ "a" "b") ; now string concatenation works by + "ab" user=> (+ 1 2) ; but this doesn't work as an arithmetic operation any more "12" user=> (def + original-plus) ; re-binds to original-plus #'user/+ user=> (+ 1 2) ; back to normal 3 user=> ; user defined functions can be bound too user=> (def ten-times (fn [x] (* 10 x))) #'user/ten-times user=> (ten-times 6) 60 user=> (type 'abc) clojure.lang.Symbol user=> (def 'abc 123) CompilerException java.lang.RuntimeException: First argument to def must be a Symbol, compiling:(NO_SOURCE_PATH:1:1) user=> ; Don't put quote before Var name. It is interpreted as (def (quote abc) 123).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.