Assignment 8 Done

This commit is contained in:
static
2024-11-20 06:25:30 +00:00
parent 858ef49b59
commit 485100c9c5
2 changed files with 68 additions and 14 deletions

View File

@@ -35,17 +35,28 @@ pub fn zero<T: 'static>() -> Church<T> {
/// Implement a function to add 1 to a given Church numeral. /// Implement a function to add 1 to a given Church numeral.
pub fn succ<T: 'static>(n: Church<T>) -> Church<T> { pub fn succ<T: 'static>(n: Church<T>) -> Church<T> {
todo!() Rc::new(move |f| {
let n = n.clone();
Rc::new(move |x| f(n(f.clone())(x)))
})
} }
/// Implement a function to add two Church numerals. /// Implement a function to add two Church numerals.
pub fn add<T: 'static>(n: Church<T>, m: Church<T>) -> Church<T> { pub fn add<T: 'static>(n: Church<T>, m: Church<T>) -> Church<T> {
todo!() Rc::new(move |f| {
let n = n.clone();
let m = m.clone();
Rc::new(move |x| m(f.clone())(n(f.clone())(x)))
})
} }
/// Implement a function to multiply (mult) two Church numerals. /// Implement a function to multiply (mult) two Church numerals.
pub fn mult<T: 'static>(n: Church<T>, m: Church<T>) -> Church<T> { pub fn mult<T: 'static>(n: Church<T>, m: Church<T>) -> Church<T> {
todo!() Rc::new(move |f| {
let n = n.clone();
let m = m.clone();
Rc::new(move |x| m(n(f.clone()))(x))
})
} }
/// Implement a function to raise one Church numeral to the power of another. /// Implement a function to raise one Church numeral to the power of another.
@@ -56,18 +67,35 @@ pub fn mult<T: 'static>(n: Church<T>, m: Church<T>) -> Church<T> {
/// base). Note: This function should be implemented *WITHOUT* using the `to_usize` or any /// base). Note: This function should be implemented *WITHOUT* using the `to_usize` or any
/// `pow`-like method. /// `pow`-like method.
pub fn exp<T: 'static>(n: usize, m: usize) -> Church<T> { pub fn exp<T: 'static>(n: usize, m: usize) -> Church<T> {
// ACTION ITEM: Uncomment the following lines and replace `todo!()` with your code. let n = from_usize(n);
// let n = from_usize(n); let m = from_usize(m);
// let m = from_usize(m); m(n)
todo!()
} }
/// Implement a function to convert a Church numeral to a usize type. /// Implement a function to convert a Church numeral to a usize type.
pub fn to_usize<T: 'static + Default>(n: Church<T>) -> usize { pub fn to_usize<T: 'static + Default>(n: Church<T>) -> usize {
todo!() let result = Rc::new(RefCell::new(0));
{
let result = result.clone();
let _unused = n(Rc::new(move |x| {
*result.borrow_mut() += 1;
x
}))(T::default());
}
{
let result = *result.borrow();
result
}
} }
/// Implement a function to convert a usize type to a Church numeral. /// Implement a function to convert a usize type to a Church numeral.
pub fn from_usize<T: 'static>(n: usize) -> Church<T> { pub fn from_usize<T: 'static>(n: usize) -> Church<T> {
todo!() Rc::new(move |f| {
Rc::new(move |mut x| {
for _ in 0..n {
x = f(x)
}
x
})
})
} }

View File

@@ -7,8 +7,12 @@
/// ///
/// Refer `test_repeat` in `assignment08_grade.rs` for detailed examples. /// Refer `test_repeat` in `assignment08_grade.rs` for detailed examples.
pub fn repeat<T, F: FnMut(T) -> T>(n: usize, mut f: F) -> impl FnMut(T) -> T { pub fn repeat<T, F: FnMut(T) -> T>(n: usize, mut f: F) -> impl FnMut(T) -> T {
todo!(); move |mut x| {
f // This line has been added to prevent compile error. You can erase this line. for i in 0..n {
x = f(x)
}
x
}
} }
/// Funny Map /// Funny Map
@@ -20,7 +24,13 @@ pub fn repeat<T, F: FnMut(T) -> T>(n: usize, mut f: F) -> impl FnMut(T) -> T {
/// ///
/// Refer `test_funny_map` in `assignment08_grade.rs` for detailed examples. /// Refer `test_funny_map` in `assignment08_grade.rs` for detailed examples.
pub fn funny_map<T, F: Fn(T) -> T>(f: F, vs: Vec<T>) -> Vec<T> { pub fn funny_map<T, F: Fn(T) -> T>(f: F, vs: Vec<T>) -> Vec<T> {
todo!() let mut result = vec![];
for element in vs {
result.push(repeat(result.len(), &f)(element));
}
result
} }
/// Count Repeat /// Count Repeat
@@ -33,7 +43,20 @@ pub fn count_repeat<T, F: Fn(T) -> T>(f: F, x: T) -> usize
where where
T: PartialEq + Copy, T: PartialEq + Copy,
{ {
todo!() let mut set = vec![x];
let mut x = x;
loop {
x = f(x);
if set.contains(&x) {
break;
} else {
set.push(x);
}
}
set.len()
} }
/// Either `T1`, or `T2`. /// Either `T1`, or `T2`.
@@ -64,6 +87,9 @@ impl<T1, T2> Either2<T1, T2> {
F1: FnOnce(T1) -> U1, F1: FnOnce(T1) -> U1,
F2: FnOnce(T2) -> U2, F2: FnOnce(T2) -> U2,
{ {
todo!() match self {
Self::Case1 { inner } => Either2::Case1 { inner: f1(inner) },
Self::Case2 { inner } => Either2::Case2 { inner: f2(inner) },
}
} }
} }