본문 바로가기

함수형

(7)
[Clojure 연습] 5. 4Clojure Easy 1 문제 # 21 답 (fn [li idx] (last (take (+ idx 1) li)) ) 해설 배웠던 take 로 자르고 그중에 마지막 문제 # 22 count a Sequence 답 (fn [li] ((fn [l c] (if (empty? l) c (recur (rest l) (inc c)))) li 0)) ;; best answer #(reduce (fn [x y] (inc x)) 0 %) 해설 Best answer 미춋네.. Reduce 에서 Fn Var Seq 가 가능 Var를 넣어주면 첫번째 인자는 이놈이 넘어가게 가능 문제 # 24 답 apply + 해설 너무 쉽고~ 문제 # 25 답 filter odd? 해설 문제 # 23 Reverse a Sequence 답 (fn [raw] (let ..
[Clojure] 좋은 사이트들 1. repl https://repl.it/languages/clojure클로저 뿐만 아니라 다른 repl도 많이 제공하는듯클로저 web repl중에서 가장 좋은듯하다 2. 문제 http://www.4clojure.com/빈칸 체우기 문제 3. dochttps://clojuredocs.org/클로저.org 사이트에서 api나 ref보는것보다 여기가 제일 좋은듯.설명도 좋고무엇보다 다른 사람들이 올린 예시를 참고가능 영어가 어려워도 대충 보고 파악 가능하다.
[Clojure 연습] 4. 4Clojure Elementary 2 문제 #20 penultimate element 답 (fn [para-list] (last (drop-last para-list))) 다른 누구의 답에는 comp를 사용하였는데 (comp second reverse ) Comp는 중요해보인다. 풀이 Comp compare 가 아니라 composition이다. 일종의 함수조합을 만들어냄 ((Comp f g) x) 라면 f(g(x)) 하는 함수 가 되는듯 문제 #35 답 7 풀이 문제 #36 답 [x 7 y 3 z 1] 풀이 문제 #37 답 "ABC" 풀이 문제 # 57 답 [5 4 3 2 1] 풀이 문제 #71 Rearranging code -> 답 last 풀이 ->연산자는 진행순서를 고대로 나타내게 할수있음. -> 연산자는 첫 x 및 form을 계속하여..
[Clojure 연습] 3. 4Clojure Elementary 클로저에 관한 빈칸 채우기 문제 사이트 4Clojure 차근 차근 풀어봐야겠다 ㅜ 문제 # 4 답 :a :b :c 문제 # 6 vector 답 :a :b :c 해설 why: = 연사자는 뒤의 파라미터들이 모두 같은지 판단한다, 또한 list 와 vector는 같은 요소라면 같다고판별 문제 # 8 set 답 #{:a :b :c :d} 문제 # 10 답 20 해설 (맵 key) 혹은 (key 맵) 은 해당 value 문제 #12 답 3 문제 #14 function 답 8 해설 #(함수) 에서 %는 처음 파라미터를 의미함 2개이상이면 %1 %2 .. Partial 은 모르것다. 문제 #16 hello world 답 #(str "Hello, " % "!") 문제 # 18 filter function 답 '(6 ..
[Clojure 연습] 2. 기초 연습문제 (ns hello.core) (def user-data {"id" 10, "name" "jack", "age" 0, "role" :developer}) (def users [user-data (assoc user-data "role" :tester "age" 20) (assoc user-data "age" 10)]) ; 재귀(defn sum-developer-age [init-user] ((fn [v sum] (if (empty? v) sum (recur (rest v) (+ sum (get (first v) "age"))))) init-user 0)) ;시퀀스 함수(defn sum-developer-age-sq [init-user] (reduce + (map (fn [i] (get i "age")) i..
[Clojure] read-line을 통하여 값을 읽을때 print보다 먼저 불리는 현상 해결방안: https://stackoverflow.com/questions/388057/clojure-side-effects-happening-out-of-order (flush 를 이용) (defn input [] (print "Type! :") (let [x (read-line)] (println (str "InputValue is:" x)) )) (input)이런상황에서보통의 C++ C# java ...기타등등 언어라면 당연히 Type: 이 출력되고그다음 x를 입력받고그다음InputValue is : 10" 이런식으로 출력되어야하는데 실행시키면 read-line이 먼저실행되고 그다음 Type!: InputValue is 10 이런식으로 출력이된다. aaaaa Type! :InputValue is:a..
[Clojure 연습] 1. 기초 연습문제 (ns hello.core) (defn Hello-Name [args] (println (str "hello " (first args))) ) (defn My-Calc-Program [] (let [x (Long/parseLong (read-line)) y (Long/parseLong (read-line)) z (read-line)] (case z "+" (println (+ x y)) "-" (println (- x y)) "*" (println (* x y)) "/" (println (/ x y)) ) )) (defn Sum-All [args] (println (apply + (map #(Long/parseLong %) args )) ) ) (defn -main [& args] (Sum-All arg..