Question: I am new to react and struggling to change the state of a component, and on the change replace the contents.

I have the following:

```js
function App() {

 const [page, setPage] = React.useState('list');

 switch(page) {
  case 'A':
   return(
   <body> 
    <Home />
    <A />
   </body>
  )
  case 'B':
  return(
   <body> 
    <Home />
    <B />
   </body>
  )
 }
}

function A() {

 return (
  <div>
   <div onClick={() => setPage('B')}>GoToB</div>
  </div>
 )
}

function B() {
    return (
       <div>test</div>
    )
}

```

There is a separate `Home` component that displays properly. How do I correctly call the `setPage` function to change the state so that the switch case is triggered and changes out the component?

Answer: 
The `mod n 2 == 0` constraints `n` to be an [**`Integral`** type](https://hackage.haskell.org/package/base-4.15.0.0/docs/Prelude.html#t:Integral), but then you use `x/n` and since the `(/)` function has signature [**`(/) :: Fractional a => a -> a -> a`**](https://hackage.haskell.org/package/base-4.15.0.0/docs/Prelude.html#v:-47-), this means that `x` and `n` have the same [**`Fractional`** type](https://hackage.haskell.org/package/base-4.15.0.0/docs/Prelude.html#t:Fractional). It makes no sense that a number type is both `Integral` and `Fractional`.

You can work with `fromIntegral` to convert `n` to a `Fractional` type:

```
recursion :: (Integral a, Floating b) => a -> b -> b
recursion n x = if mod n 2 == 0 
                  then x/**fromIntegral** n + (recursion (n-1) x)**(-1) 
                  else **fromIntegral** n/x + (recursion (n-1) x)**(-1)
```

Another problem with your function is that there is no *stop condition*. You need to add an extra clause:

```
recursion :: (Integral a, Floating b) => a -> b -> b
recursion n x
  | **n <= 0 = 0**
  | mod n 2 == 0 = x/**fromIntegral** n + (recursion (n-1) x)**(-1) 
  | otherwise = **fromIntegral** n/x + (recursion (n-1) x)**(-1)
```

This then produces for example:

```
Prelude> recursion 10 2
0.3481658094621751

```
What you apparently meant was

```
recursion n x = if snd (properFraction (n / 2)) > 0   -- isOdd
                then x/n + recursion (n-1) (x**(-1))
                else n/x + recursion (n-1) (x**(-1))

```

but there's two problems here. First of all *correctness*, since you start from `n`th term and go back to `0`th, but use `x` as the starting value always, whereas it differs depending on whether `n` is even or odd. We fix problems like this by moving the actual work into the inner, "worker" function, and fixing the starting value of the iteration.

Second problem is that it is *not* *tail recursive*. We fix problems like that by introducing an accumulating parameter to the inner, "worker" function. One tool to fix two problems!

```
-- T = 1/x + x/2 + 3/x + x/4 + ...

recursion :: RealFrac a => a -> a -> a
recursion n x = if snd (properFraction (n / 2)) > 0   -- n is odd
                then goOdd  n 0
                else goEven n 0
  where
  goOdd  n acc     = goEven (n-1) (acc + n/x)
  goEven n acc 
       | n <= 0    = acc
       | otherwise = goOdd  (n-1) (acc + x/n)

```

Now it's correct, and tail recursive as you wanted.