Showing
3 changed files
with
62 additions
and
2 deletions
@@ -68,7 +68,7 @@ fn sqrt() { | @@ -68,7 +68,7 @@ fn sqrt() { | ||
68 | let f = Fractional(-9, 4); | 68 | let f = Fractional(-9, 4); |
69 | let fr :f64 = f.try_into().unwrap(); | 69 | let fr :f64 = f.try_into().unwrap(); |
70 | let sq = f.sqrt(); | 70 | let sq = f.sqrt(); |
71 | - let _sq = f64::sqrt(fr); | 71 | + let _sq = fr.sqrt(); |
72 | 72 | ||
73 | println!("{:>14} : {:?} / {}", format!("sqrt {}", f), sq, _sq); | 73 | println!("{:>14} : {:?} / {}", format!("sqrt {}", f), sq, _sq); |
74 | 74 | ||
@@ -79,7 +79,7 @@ fn sqrt() { | @@ -79,7 +79,7 @@ fn sqrt() { | ||
79 | let fr :f64 = (*f).try_into().unwrap(); | 79 | let fr :f64 = (*f).try_into().unwrap(); |
80 | let sq = f.sqrt().unwrap(); | 80 | let sq = f.sqrt().unwrap(); |
81 | let sqr :f64 = sq.try_into().unwrap(); | 81 | let sqr :f64 = sq.try_into().unwrap(); |
82 | - let _sqr = f64::sqrt(fr); | 82 | + let _sqr = fr.sqrt(); |
83 | 83 | ||
84 | println!("{:>14} : {} {} / {}", format!("sqrt {}", f), sq, sqr, _sqr); | 84 | println!("{:>14} : {} {} / {}", format!("sqrt {}", f), sq, sqr, _sqr); |
85 | } | 85 | } |
recursion/Cargo.toml
0 → 100644
recursion/src/main.rs
0 → 100644
1 | +// | ||
2 | +// Try some recursion with pattern matching... especially in | ||
3 | +// conjunction with match to get a similar behavious as in e.g. | ||
4 | +// haskell... | ||
5 | +// | ||
6 | +// Georg Hopp <georg@steffers.org> | ||
7 | +// | ||
8 | +// Copyright © 2019 Georg Hopp | ||
9 | +// | ||
10 | +// This program is free software: you can redistribute it and/or modify | ||
11 | +// it under the terms of the GNU General Public License as published by | ||
12 | +// the Free Software Foundation, either version 3 of the License, or | ||
13 | +// (at your option) any later version. | ||
14 | +// | ||
15 | +// This program is distributed in the hope that it will be useful, | ||
16 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | +// GNU General Public License for more details. | ||
19 | +// | ||
20 | +// You should have received a copy of the GNU General Public License | ||
21 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
22 | +// | ||
23 | + | ||
24 | +// Borrowing rules do not apply here because everything lives on the stack. | ||
25 | +fn faculty(x: u32) -> u32 { | ||
26 | + match x { | ||
27 | + 0 => 1, | ||
28 | + _ => x * faculty(x - 1), | ||
29 | + } | ||
30 | +} | ||
31 | + | ||
32 | +// for a tail recursive version we need to pass the currently calculated | ||
33 | +// faculty to an inner tail recursive function. | ||
34 | +// This will probably be optimized better, because the compiler would be | ||
35 | +// able to unroll the complete recursion. | ||
36 | +fn tail_faculty(x: u32) -> u32 { | ||
37 | + fn faculty(x: u32, f: u32) -> u32 { | ||
38 | + match x { | ||
39 | + 0 => f, | ||
40 | + _ => faculty(x - 1, x * f), | ||
41 | + } | ||
42 | + }; | ||
43 | + faculty(x, 1) | ||
44 | +} | ||
45 | + | ||
46 | +fn main() { | ||
47 | + for i in 0..10 { | ||
48 | + println!("Fakultät {} = {}", i, faculty(i)); | ||
49 | + println!("tail recursive Fakultät {} = {}", i, tail_faculty(i)); | ||
50 | + } | ||
51 | +} |
Please
register
or
login
to post a comment