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
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 |
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以下の整数一覧を並べたリスト *) | |
(* 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 = |
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
/* 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++) { |
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
#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; |
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
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番目の要素のところで分割する |
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
-- 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. パターンマッチ |
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
-- 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)) |
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
-- 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 |
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
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) = |
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
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] |