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の階乗を返す (Int -> Int) */ | |
/* 目的: 普通の再帰で書いたものと、末尾再帰で書いたものの比較 */ | |
/*********************************************************/ | |
// =====普通の再帰===== | |
int fact(int 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
(* 目的: 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) |
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
-- 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) |
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
{- –- 普通の再帰-- -} | |
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 |
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
{- –- 普通の再帰-- -} | |
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 |
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
;;; 普通の再帰 | |
(define (factorial n) | |
(if (= n 1) | |
1 | |
(* n (factorial (- n 1))))) | |
;;; 末尾再帰 | |
(define (factorial n) | |
(fact-iter 1 1 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
/* 現在は修正されています | |
前: 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) |
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> | |
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; |
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 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)); | |
} |
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 check(char c) | |
{ | |
if ('A'<= c && c <='Z') return 1; | |
else if ('a'<=c && c<='z') return 0; | |
else return 2; | |
} | |
/** メイン関数 **/ | |
int main(void) | |
{ |
OlderNewer