Skip to content

Instantly share code, notes, and snippets.

View chomado's full-sized avatar
🎀
Working

ちょまど | Madoka Chiyoda chomado

🎀
Working
View GitHub Profile
@chomado
chomado / sort2.hs
Last active December 17, 2015 11:19
さっき書いたsortのやつ、case of使わないで書きなおしてみました。case ofとかOCamlのmatch withとか使わなくても普通にパターンマッチできるのスゴイですHaskell感動しました
mysort2 :: [Int] -> [Int]
mysort2 [] = []
mysort2 (first : rest) = insert (mysort2 rest) first
where
insert [] n = [n]
insert (first : rest) n
| first < n = first : insert rest n
| otherwise = n : first : rest
@chomado
chomado / perfect.ml
Created May 26, 2013 18:23
n 以下の完全数をリストで返す
(* 完全数: 自分自身を除く約数の和がその数自身になる *)
(* n以下の整数一覧を並べたリスト *)
(* enumerate: int -> int list *)
let rec enumerate n = if n <= 0 then [] else n :: enumerate (n-1)
(* n 以下の完全数をリストで返す *)
(* perfect : int -> int list *)
let perfect n =
let perfectp n =
@chomado
chomado / determinant.c
Created June 7, 2013 06:10
行列の積の求め方を知ったので、ユーザーの入力した2行3列の行列と、3行2列の行列の積を求めるものを書いてみました
/* 2行3列の行列と、3行2列の行列の積を求める */
#include <stdio.h>
int main(void) {
int i, j, k;
int mx[2][3], my[3][2], mz[2][2];
/* ---- 入力 -----*/
puts("行列mx, myの各要素を入力してください。");
puts("行列mx: 2行3列");
for (i=0; i<2; i++) {
@chomado
chomado / static_and_automatic _storage_duration.c
Created June 8, 2013 11:02
自動記憶域期間と静的記憶域期間の変数の挙動を確認するプログラム。難しいです…orz
#include <stdio.h>
// 自動記憶域期間・静的記憶域期間の変数の挙動を確認する
int fx = 0; // 静的記憶域期間 + ファイル有効範囲
void func(void) {
static int sx = 0; // 静的記憶域期間 + ブロック有効範囲
int ax = 0; // 自動記憶域期間 + ブロック有効範囲
printf("%3d%3d%3d\n", ax++, sx++, fx++);
}
int main(void) {
int i;
halve :: [a] -> ([a], [a])
halve xs = splitAt n xs
where n = length xs `div` 2
-- 実行例
--halve [1, 2, 3, 4, 5, 6]
--([1, 2, 3], [4, 5, 6])
-- drop n xs ... リストからn個の要素を取り除く
-- splitAt n xs = (take n xs, drop n xs) ... リストをn番目の要素のところで分割する
@chomado
chomado / programing_in_Haskell_4_2.hs
Created June 17, 2013 11:10
--safetail :: [a] -> [a] -- tail(cdr)のように振る舞うが、違いは、空リストを与えた時に空リストを返すようにすること。 -- 実装方法は、それぞれ、a. 条件式、 b. ガード付きの等式、 c. パターンマッチ でやること。
-- a. 条件式
safetail_A :: [a] -> [a]
safetail_A xs = if null xs then [] else tail xs
-- b. ガード付きの等式
safetail_B :: [a] -> [a]
safetail_B xs | null xs = []
| otherwise = tail xs
-- c. パターンマッチ
-- double x = x + x
double = \x -> x + x
--add x y = x + y
add = \x -> (\y -> x + y)
-- カリー化された関数 mult :: Num a => a -> a -> a -> a
-- mult x y z = x * y * z は、λ式を用いるとどのように表現できるか
mult :: Int -> ( Int -> ( Int -> Int ))
mult = \x -> (\y -> (\z -> x * y * z))
-- 5-1
sum [x^2 | x<-[1..100]]
-- 5-2
replicate' :: Int -> a -> [a]
replicate' n a = [a | _ <- [1..n]]
-- 5-3
-- n以下の要素のピタゴラス数のリストを返す
pyths :: Int -> [(Int, Int, Int)]
pyths n = [(x, y, z) | x <- [1..n], y <- [1..n], z <- [1..n], x^2 + y^2 == z^2]
-- 5-4
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x : xs) =
let smallerOrEqual = [a | a <- xs, a <= x]
larger = [a | a <- xs, a > x]
in quicksort smallerOrEqual ++ [x] ++ quicksort larger
quicksort' :: (Ord a) => [a] -> [a]
quicksort' [] = []
quicksort' (x : xs) =
@chomado
chomado / collatzProblem.hs
Last active December 18, 2015 17:09
コラッツの数列
collatz :: Int -> [Int]
collatz 1 = [1]
collatz n
| n `mod` 2 == 0 = n : collatz ( n `div` 2 )
| otherwise = n : collatz ( 3 * n + 1 )
-- って書いたら、even, odd っていう便利なものがありました。書き直します
collatz':: Int -> [Int]
collatz' 1 = [1]