...

再帰法と帰納法 自然数の上の再帰法 x^(m+n) = (x^m)*(x^n) リストの上

by user

on
Category: Documents
21

views

Report

Comments

Transcript

再帰法と帰納法 自然数の上の再帰法 x^(m+n) = (x^m)*(x^n) リストの上
⮤↓ᩐ䛴୕䛴්ᖉἪ
• 䛿䛓஋䛴්ᖉⓏ䛰ᏽ⩇
x^0 = 1
x^(n+1) = x * x^n
• Fibonacci䛴්ᖉⓏ䛰ᏽ⩇
්ᖉἪ䛮ᖉ⣙Ἢ
fib 0 = 0
fib 1 = 1
fib (n+2) = fib n + fib (n+1)
⬄䚭ᣲỜ
⮤↓ᩐ䛴୕䛴ᖉ⣙Ἢ䛱䜎䜑ッ᪺
࿤㢗p(n)䛒௴ណ䛴⮤↓ᩐn䛱䛪䛊䛬ᠺ❟
• P(0)䛒ᠺ❟
• P(1)䛒ᠺ❟
• P(n),p(n-1)䛒ᠺ❟P(n+1)䛒ᠺ❟
䝮䜽䝌䛴୕䛴්ᖉἪ
• 䝮䜽䝌䛴㏻᥃
[] ++ ys = ys
(x:xs) ++ ys = x : (xs++ys)
Recursion
<fib.1>
<fib.2>
<fib.3>
x^(m+n) = (x^m)*(x^n)
• m䛱䛪䛊䛬ᖉ⣙Ἢ䛭ッ᪺䛟䜑䚯
– 0䛴ሔྙ
x^(0+n) = x^n
= 1 * x^n
= x^0 * x^m
<^.1>
<*䛴Ἢ์>
<^.1>
– m+1䛴ሔྙ
x^((m+1)+n)
= x^((m+n)+1)
= x * x^(m+n)
= x * x^m * x^n
䚭䚭䚭= x^(m+1) * x^n
<+䛴Ἢ์>
<^.2>
<௫ᏽ>
<^.2>
ᖉ⣙Ἢ䛱䜎䜑ッ᪺
௴ណ䛴᭯㝀䝮䜽䝌xs䛱䛪䛊䛬P(xs)䛒ᠺ❟
• 䝮䜽䝌䛴㛏䛛䜘ị䜇䜑㛭ᩐ
length [] = 0
length (x:xs) = 1 + length xs
Base
<^.1>
<^.2>
base
recursion
• P[] 䛒ᠺ❟
• P[x]䛒ᠺ❟
• P(xs)䛒ᠺ❟P(x:xs)䛒ᠺ❟
1
䝮䜽䝌ⁿ⟤
length (xs++ys) = length xs + length ys
xs䛱㛭䛟䜑ᖉ⣙Ἢ䛭ッ᪺䛟䜑
• Zip
– []䛴ሔྙ
length ([]++ys)
= length ys
= 0 + length ys
= length [] + length ys
<++.1>
<+>
<length.1>
2ᘤᩐ㛭ᩐ䠌䠅䛪䛴ሔྙ
zip [] ys = []
zip (x:xs) [] = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys
– x:xs䛴ሔྙ
length ((x:xs)++ys)
= length (x:(xs++ys))
= 1 + length (xs++ys)
= 1 + length xs + length ys
= length (x:xs) + length ys
• length (zip xs ys) = min (length xs) (length ys)
<++.2>
<length.2>
<௫ᏽ>
<length.2>
• Take/drop䛴්ᖉⓏ䛰ᏽ⩇
– ッ᪺䠌䚭
ሔྙ䠃䠌xs=[], ys
ሔྙ䠄䠌(x:xs), ys=[]
ሔྙ䠅䠌(x:xs), (y:ys)
• head/tail 䛴ᏽ⩇
take 0 xs = []
take (n+1) [] = []
take (n+1) (x:xs) = x : take n xs
head (x:xs) = x
tail (x:xs) = xs
head [] = ⊥
tail [] = ⊥
drop 0 xs = xs
drop (n+1) [] = []
drop (n+1) (x:xs) = drop n xs
✭䛭䛰䛊䝮䜽䝌xs䛱ᑊ䛝䛬
䚭䚭䚭䚭[head xs]++tail xs = xs
• ッ᪺䠌䚭take n xs ++ drop n xs = xs
• Map/filter
• Init/last
init [x] = []
init (x:x’:xs) = x : init (x’:xs)
last [x] = x
last (x:x’:xs) = last (x’:xs)
init xs = take (length xs –1 ) xs
xs䛱㛭䛟䜑ᖉ⣙Ἢ䛭ッ᪺䛟䜑䚯
㟸✭䝮䜽䝌䛴஦
䛪䛴ሔྙ
map f [] = []
map f (x:xs) = f x : map f xs
filter p [] = []
filter p (x:xs) | p x = x : filter p xs
| otherwise = filter p xs
filter p (map f xs) = map f (filter (p . f) xs)
xs䛱㛭䛟䜑ᖉ⣙Ἢ䛭ッ᪺䛟䜑䚯
2
⿭ຐ㛭ᩐ
• ⿭ຐ㛭ᩐ
⿭ຐᏽ⌦
• ⿭ຐᏽ⌦
xs \\ [] = xs
xs \\ (y:ys) = remove xs y \\ ys
remove [] y = []
remove (x:xs) y | x==y = xs
| otherwise = x : remove xs y
reverse [] = []
reverse (x:xs) = reverse xs ++ [x]
䛟䛿䛬䛴᭯㝀xs䛱ᑊ䛝䛬
䚭䚭䚭reverse (reverse xs) = xs
䛟䛿䛬䛴x䛮᭯㝀䝮䜽䝌ys䛱ᑊ䛝䛬
reverse (ys++[x]) = x : reverse ys
reverse (reverse xs) = xs
xs䛱㛭䛟䜑ᖉ⣙Ἢ䛭ッ᪺䛟䜑䚯
– ሔྙ[]:
reverse (reverse [])
= reverse []
<rev.1>
= []
<rev.1>
– ሔྙ(x:xs)
reverse (reverse (x:xs))
= reverse (reverse xs ++ [x])
<rev.2>
= x : reverse (reverse xs)
<䜁䛝䛊>
= x : xs
<௫ᏽ>
Init䛴ྙᠺ
௘ᵕ䠌init xs = take (length xs – 1) xs
ᑙฝ䠌
– init [x] = take (length [x] –1) [x]
= take 0 [x]
= []
– init (x:x’:xs) = take (length (x:x’:xs)-1) (x:x’:xs)
= take (2+length xs-1) (x:x’:xs)
= take (length xs + 1) (x:x’:xs)
= x : take (length xs) (x’:xs)
ッ᪺䛴ᡥ㡨䛮 = x : take (length (x’:xs)-1))(x’:xs)
జ䛬䛊䜑䚯
= x : init (x’:xs)
⿭ຐᏽ⌦
䝛䝱䜴䝭䝤䛴ྙᠺ
• 䝛䝱䜴䝭䝤䛴ッ᪺䠌
– 䝛䝱䜴䝭䝤
䚭䝛䝱䜴䝭䝤䛴ᛮ㈻䜘‫‮‬䛥䛟䛙䛮䜘♟䛟
• 䝛䝱䜴䝭䝤䛴ྙᠺ
– ௘ᵕ䟺䝛䝱䜴䝭䝤䛒‫‮‬䛥䛟䛿䛓ᛮ㈻䟻
䝛䝱䜴䝭䝤䜘⤄䜅❟䛬䜑
㧏㏷Fibonacciゝ⟤
䚭fib 0 = 0
fib 1 = 1
fib (n+2) = fib n + fib (n+1)
fib’ n = fst (twofib n)
twofib n = (fib n, fib (n+1))

Twofib
䛴ྙᠺ
3
twofib 0 = (fib 0, fib 1)
= (0,1)
twofib (n+1)
= (fib (n+1), fib (n+2))
= (fib (n+1), fib n + fib (n+1))
= (b,a+b)
where (a,b) = twofib n
• ຝ⋙䛴䜎䛊䝛䝱䜴䝭䝤
fib’ n = fst (twofib n)
twofib 0 = (0,1)
twofib (n+1) = (b,a+b)
where (a,b) = twofib n
4
Fly UP