Skip to content

Instantly share code, notes, and snippets.

View chomado's full-sized avatar
🎀
Working

ちょまど | Madoka Chiyoda chomado

🎀
Working
View GitHub Profile
// 末尾再帰についての説明
/*********************************************************/
/* 処理概要: nの階乗を返す (Int -> Int) */
/* 目的: 普通の再帰で書いたものと、末尾再帰で書いたものの比較 */
/*********************************************************/
// =====普通の再帰=====
int fact(int n)
{
(* 目的: init から始めてlstの要素を右から順にfを施し込む *)
(* fold_right: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b *)
let rec fold_right f lst init = match lst with
[] -> init
| first :: rest -> f first (fold_right f rest init)
@chomado
chomado / tailCall.hs
Created February 24, 2014 16:41
「なんで末尾呼び出しだと使うメモリの量が減るのか可視化してみた」by @naohaq さん
-- http://na.yuki.st/src/tailcall.txt より。
-- 末尾呼出でない定義
fact 0 = 1
fact n = n * fact (n - 1)
-- 末尾呼出な定義
fact' a 0 = a
fact' a n = fact' (a * n) (n - 1)
{- –- 普通の再帰-- -}
my_soap :: Integer -> Integer
my_soap 0 = 0
my_soap n = my_soap (n-1) + n
{- –- 末尾再帰-- -}
my_soap_iter :: Integer -> Integer
my_soap_iter x = my_soap_iter2 x 0
my_soap_iter2 :: Integer -> Integer -> Integer
{- –- 普通の再帰-- -}
my_mul :: Integer -> Integer -> Integer
my_mul m 1 = m
my_mul m n = my_mul m (n - 1) + m
{- –- 末尾再帰-- -}
my_mul_iter :: Integer -> Integer -> Integer
my_mul_iter x y = iter x y x
where
iter :: Integer -> Integer -> Integer -> Integer
;;; 普通の再帰
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
;;; 末尾再帰
(define (factorial n)
(fact-iter 1 1 n))
/* 現在は修正されています
前: scanf("%c %d", &mark, &num);
後: scanf(" %c %d", &mark, &num);
*/
#include <stdio.h>
void check(int);
void print(void);
int mark_to_id(char mark);
/************ メイン関数 ***********/
int main(void)
#include <stdio.h>
char grading(int, int, int);
/***** メイン関数 ********/
int main(void)
{
int m, f, r;
while (scanf("%d %d %d", &m, &f, &r), ~m||~f||~r) {
printf("%c\n", grading(m, f, r));
}
return 0;
#include <stdio.h>
int check(int, int, int, int);
int solve(int, int);
/***** メイン関数 *****/
int main(void)
{
int n, sum;
while (scanf("%d %d", &n, &sum), n||sum) {
printf("%d\n", solve(n, sum));
}
#include <stdio.h>
int check(char c)
{
if ('A'<= c && c <='Z') return 1;
else if ('a'<=c && c<='z') return 0;
else return 2;
}
/** メイン関数 **/
int main(void)
{