Commit 863d0ee8 authored by Adithya C. Ganesh's avatar Adithya C. Ganesh 💬

Fix indentation

parent b94e1eb5
Pipeline #44414835 passed with stages
in 5 minutes and 25 seconds
......@@ -8,63 +8,63 @@ Pick three of the following.
1. Implement the `map` function using a fold.
```haskell
map' f = foldr (\ x xs -> f x : xs) []
```
```haskell
map' f = foldr (\ x xs -> f x : xs) []
```
2. Implement the `filter` function using a fold.
```haskell
filter' p xs = foldr step [] xs
where step x ys | p x = x : ys
| otherwise = ys
```
```haskell
filter' p xs = foldr step [] xs
where step x ys | p x = x : ys
| otherwise = ys
```
3. Implement `foldl` using `foldr`.
```haskell
foldl' :: (a -> b -> a) -> a -> [b] -> a
```haskell
foldl' :: (a -> b -> a) -> a -> [b] -> a
foldl' f z xs = foldr step id xs z
where step x g a = g (f a x)
```
foldl' f z xs = foldr step id xs z
where step x g a = g (f a x)
```
4. Write code to compute the smallest positive number that is evenly divisible
by all the numbers from 1 to $n$. Provide an answer for $n = 20$.
```haskell
myGCD :: Integral a => a -> a -> a
myGCD x 0 = x
myGCD x y = myGCD y (x `mod` y)
```haskell
myGCD :: Integral a => a -> a -> a
myGCD x 0 = x
myGCD x y = myGCD y (x `mod` y)
myLCM :: Integral a => a -> a -> a
myLCM x y = (x * y) `div` (myGCD x y)
myLCM :: Integral a => a -> a -> a
myLCM x y = (x * y) `div` (myGCD x y)
main = putStrLn . show $ n
where
n = foldl myLCM 1 [1..20]
```
main = putStrLn . show $ n
where
n = foldl myLCM 1 [1..20]
```
`Output: 232792560.`
`Output: 232792560.`
5. Write code to compute the $n$th prime number. Provide an answer for $n = 10001$.
```haskell
-- See https://wiki.haskell.org/Prime_numbers
-- for several optimized implementations.
```haskell
-- See https://wiki.haskell.org/Prime_numbers
-- for several optimized implementations.
primesTo m = sieve [2..m]
where
sieve (p:xs)
| p*p > m = p : xs
| True = p : sieve [x | x <- xs, rem x p > 0]
primesTo m = sieve [2..m]
where
sieve (p:xs)
| p*p > m = p : xs
| True = p : sieve [x | x <- xs, rem x p > 0]
main = putStrLn . show $ (ps !! 10000)
where
ps = primesTo 1000000
```
main = putStrLn . show $ (ps !! 10000)
where
ps = primesTo 1000000
```
`Output: 104743.`
`Output: 104743.`
## References
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment