This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns myreader.core | |
"Reader Literals Test" | |
(:require [clojure.string :as str])) | |
(defn debug-print | |
"Gauche debug print" | |
[x] | |
`(let [res# ~x] | |
(println "?=" res#) | |
res#)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def fib | |
(map second (iterate (fn [[a b]] [b (+ a b)]) [0 1]))) | |
; (1 1 2 3 5 8 13 21 34 55) | |
(println (take 10 fib)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(some #(->> % inc range rest (apply *) (str % "! = ") println) | |
(-> (Integer/parseInt (nth *command-line-args* 0 "1")) inc range rest)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn get-first | |
"条件にあう最初の要素を返す" | |
[pred [x & rst]] | |
(if (nil? x) nil | |
(if (pred x) x (recur pred rst)))) | |
(defn calc-body | |
"四則演算の優先順で1つの演算だけ処理する。処理結果を元の式に反映して式を返す" | |
[exp] | |
(let [indexed-exp (partition 2 (interleave exp (range)))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn correct-order | |
"中置記法を後置記法に並び替え。乗算、除算は優先するよう並び替える" | |
[ls] | |
(reduce | |
(fn [[op res] x] | |
(cond | |
(symbol? x) [x res] | |
(nil? op) [op (conj res x)] | |
(or (= 1 (count res)) (some #(= % op) '(+ -))) [nil (conj res x op)] | |
:else (let [[n last-op] (take-last 2 res)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn 一 ([] 1) ([f] (f 1))) | |
(defn 二十 ([] 20) ([f] (f 20))) | |
(defn から [numfn] | |
(fn [n] (list (numfn) (inc n)))) | |
(defn まで数えて [ls] | |
(list (apply range ls) ())) | |
(defn- make-pred [pred] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(n) | |
(1..n).map do |x| | |
res = "" | |
res += "Fizz" if x % 3 == 0 | |
res += "Buzz" if x % 5 == 0 | |
(res == "") ? x : res | |
end | |
end | |
puts fizzbuzz(15) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
sub fizzbuzz { | |
my $n = shift; | |
for(1 .. $n){ | |
my $res = ""; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; nがmで割り切れるかどうか | |
(defn divisible? [m n] | |
(zero? (mod n m))) | |
; 1〜無限のFizzBuzzリスト | |
(def all-fizzbuzz-list | |
(map #(condp divisible? % | |
15 "FizzBuzz" | |
5 "Buzz" | |
3 "Fizz" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(n:Int):IndexedSeq[String] = { | |
(1 to n).map(x => List(x % 15, x % 5, x % 3) match { | |
case List(0, _, _) => "FizzBuzz" | |
case List(_, 0, _) => "Buzz" | |
case List(_, _, 0) => "Fizz" | |
case _ => x + "" | |
}) | |
} | |
println(fizzbuzz(15)) |