clojure
goodluckforlove
这个作者很懒,什么都没留下…
展开
-
用clojure解决 euler problem 1
问题描述:If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 1000.解决方案1原创 2012-07-05 22:10:43 · 1217 阅读 · 0 评论 -
4clojure第56个问题从序列中找出不同的项并保持次序
(ns for-clojure.problem56)(defn find-distinct-items "找出序列中不同的元素,并且要保持原来的次序不变" [v] (reduce #(if (some #{%2} %1) %1 (conj %1 %2)) [] v))(find-distinct-items [:a :a :b :b :c :c])这里需要注意的是为什么用so原创 2013-03-13 10:38:47 · 619 阅读 · 0 评论 -
用clojure实现《实用Common Lisp编程》中的单元测试框架
(ns simple-test-frame.core)(def ^:dynamic *test-name* nil)(defn report-result [result form] (if result (println "pass ... " *test-name* ":" form) (println "fail ... " *test-name* ":" f原创 2013-03-01 13:38:30 · 759 阅读 · 0 评论 -
4clojure第55个问题:实现自己的序列频率统计函数
(ns for-clojure.problem50)(defn my-frequencies-1 "尾递归序列统计频率" [v] (loop [v v r {}] (if (empty? v) r (let [ele (first v) rest-v (rest v)] (if (get r ele)原创 2013-03-13 10:11:44 · 670 阅读 · 0 评论 -
4clojure第97个问题:杨辉三角
(ns for-clojure.problem97)(defn pascals-triangle-1 "杨辉三角" [n] (if (= n 1) [1] (loop [m 2 r [1]] (let [next-r (into [] (map #(apply + %) (partition 2 1(concat [0] r [0]))))]原创 2013-03-13 13:02:04 · 619 阅读 · 0 评论 -
4clojure第43个问题逆向交错(Reverse Interleave)
Reverse Interleave Difficulty:MediumTopics:seqsWrite a function which reverses the interleave process into x number of subsequences.(= (__ [1 2 3 4原创 2013-03-12 21:04:08 · 1018 阅读 · 0 评论 -
4clojure第58个问题:组合函数
(ns for-clojure.problem58)(defn fn-comp-1 "创建一个函数的组合函数,此函数接受任何个数的函数,从右到左执行函数" [& fns] (fn [& init-paramters] (let [fns (reverse fns) result (apply (first fns) init-paramters)]原创 2013-03-13 11:57:33 · 840 阅读 · 0 评论 -
4clojure第59个问题:创建自己的juxt
(ns for-clojure.problem59)(defn juxtaposition [& fns] (fn [& xs] (into [] (map #(apply % xs) fns))))(= [21 6 1] ((juxtaposition + max min) 2 3 5 1 6 4))(= ["HELLO" 5] ((juxtaposition #(原创 2013-03-13 12:25:54 · 788 阅读 · 0 评论 -
4clojure第118个问题:自己的map函数
(fn m [f v] (cons (f (first v)) (lazy-seq (if (empty? (rest v)) [] (m f (rest v))))))这里在lazy-seq里面必须做(rest v)是否为空的判断,不原创 2013-03-13 21:58:47 · 699 阅读 · 0 评论 -
4clojure第135个问题:中缀计算器
(ns for-clojure.problem135)(defn infix-calculator "前缀计算机,从左到右执行,不论优先级" [& xs] (loop [r (first xs) xs (rest xs)] (if (empty? xs) r (recur ((first xs) r (second xs)) (nt原创 2013-03-13 19:31:32 · 652 阅读 · 0 评论 -
4clojure第54个问题:拆分序列
(ns for-clojure.problem54)(defn partition-a-sequence-1 "拆分序列,把一个序列拆分成每部分x个元素,最后不足x元素的丢弃" [x v] (loop [r [];最终结果 m [];中间结果保存 v v] (if (= (count m) x) (if (empty? v原创 2013-03-13 20:03:01 · 1118 阅读 · 0 评论 -
nrepl emacs ac-nrepl 绝配
用了Ideal的clojure插件以后,发现repl中没办法自动补全,这个问题很麻烦,因为在学习写clojure代码的过程中,很需要repl测试想法,函数等等。有emacs的nrepl之后,一切都变得美妙了。真的喜欢上nrepl了,一切都简单了。原创 2013-03-14 20:59:38 · 821 阅读 · 0 评论 -
4clojure第65个问题:黑盒测试序列类型
(ns for-clojure.problem65)(defn black-box-testing "黑盒测试:根据序列的特点判断序列的类型,不能使用类型判断等函数" [v] (cond (:a (conj v [:a 1])) :map (< (- (count (conj v 1 1)) (count v)) 2) :set原创 2013-03-14 20:53:31 · 825 阅读 · 0 评论 -
4clojure第50个问题根据类型拆分序列
;Split by Type ;Difficulty: Medium;Topics: seqs;Write a function which takes a sequence consisting of items with different types and splits them up into a set of homogeneous sub-sequences. The in原创 2013-03-13 00:04:44 · 1125 阅读 · 0 评论 -
用clojure实现《实用Common Lisp编程》中的简单数据库:CD数据库
(ns simple-cd-database.core)(defn make-cd [title artist rating ripped] {:title title :artist artist :rating rating :ripped ripped})(def db (atom []))(defn add-record [cd] (swap! db conj cd))原创 2013-02-27 17:38:30 · 1045 阅读 · 0 评论 -
用clojure解决 euler problem 2
问题描述: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...By con原创 2012-07-06 12:47:45 · 476 阅读 · 0 评论 -
用clojure解决 euler problem 4
问题描述:A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.Find the largest palindrome made from the product of two原创 2012-07-07 21:12:39 · 593 阅读 · 0 评论 -
用clojure解决euler problem 3
问题描述:The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 600851475143 ?(ns euler-problem-3.core)(defn largest-prime-factor [number] (loop [原创 2012-07-06 20:47:25 · 472 阅读 · 0 评论 -
用clojure解决euler problem 5
问题描述:2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.What is the smallest positive number that is evenly divisible by all of the numbe原创 2012-07-08 10:43:12 · 487 阅读 · 0 评论 -
用clojure解决 euler problem 6
问题描述:The sum of the squares of the first ten natural numbers is,12 + 22 + ... + 102 = 385The square of the sum of the first ten natural numbers is,(1 + 2 + ... + 10)2 = 552 = 3025Hence原创 2012-07-08 13:37:15 · 456 阅读 · 0 评论 -
用clojure解决euler problem 7
问题描述:By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.What is the 10 001st prime number?解决方案:(ns euler-problem-7.core (:use [clojure.cont原创 2012-07-08 20:39:47 · 412 阅读 · 0 评论 -
用clojure解决euler problem 8
问题描述:Find the greatest product of five consecutive digits in the 1000-digit number.73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156原创 2012-07-08 21:06:08 · 461 阅读 · 0 评论 -
用clojure解决euler problem 9
问题描述:A Pythagorean triplet is a set of three natural numbers, a b c, for which,a2 + b2 = c2For example, 32 + 42 = 9 + 16 = 25 = 52.There exists exactly one Pythagorean triplet for whic原创 2012-07-08 22:25:31 · 528 阅读 · 0 评论 -
用clojure解决euler problem 10
问题描述:The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.Find the sum of all the primes below two million.解决方案:(ns euler-problem-10.core (:use [clojure.contrib.math]))(defn prime? [原创 2012-07-09 21:01:00 · 526 阅读 · 0 评论 -
用clojure解决 euler problem 11
问题描述:In the 2020 grid below, four numbers along a diagonal line have been marked in red.08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 0849 49 99 40 17 81 18 57 60 87 17 40 98 43 6原创 2012-07-12 10:16:18 · 649 阅读 · 0 评论 -
用clojure解决euler problem 12
问题描述:The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:1, 3, 6, 10,原创 2012-07-13 21:00:01 · 554 阅读 · 0 评论 -
compojure ring 笔记
compojure.handler/site 要放在middleware的最外层 不然使用session验证时会出现 404错误(session middleware需要site导入)。compojure.handler/site包含了一般网站所需要的中间件site(site routes & [opts])Create a handler suitable for原创 2013-03-07 08:07:56 · 1503 阅读 · 0 评论 -
用 clojure实现【白话经典算法系列之十四】腾讯2012年实习生笔试加分题
昨天见 【白话经典算法系列之十四】腾讯2012年实习生笔试加分题, 作为一个clojure爱好者,实现了一个clojure版本的。题目:给定一数组a[N],我们希望构造数组b [N],其中b[j]=a[0]*a[1]…a[N-1] / a[j],在构造过程中,不允许使用除法:要求O(1)空间复杂度和O(n)的时间复杂度;除遍历计数器与a[N] b[N]外,不可使用新原创 2013-04-02 17:05:35 · 1069 阅读 · 0 评论