added ListsAns
This commit is contained in:
parent
8011389aa9
commit
e37d1692c9
3 changed files with 97 additions and 29 deletions
|
@ -14,7 +14,7 @@ import Relation.Binary.PropositionalEquality as Eq
|
||||||
open Eq using (_≡_; refl; sym; trans; cong)
|
open Eq using (_≡_; refl; sym; trans; cong)
|
||||||
open Eq.≡-Reasoning
|
open Eq.≡-Reasoning
|
||||||
open import Data.Nat using (ℕ; zero; suc; _+_; _*_)
|
open import Data.Nat using (ℕ; zero; suc; _+_; _*_)
|
||||||
open import Data.Nat.Properties.Simple using (distribʳ-*-+)
|
open import Data.Nat.Properties.Simple using (distribʳ-*-+; *-comm)
|
||||||
\end{code}
|
\end{code}
|
||||||
|
|
||||||
## Lists
|
## Lists
|
||||||
|
@ -380,33 +380,6 @@ of the first list, reversing a list in this way takes
|
||||||
time proportional to the *square* of the length of the
|
time proportional to the *square* of the length of the
|
||||||
list, since `1 + ⋯ + n ≡ n * (n + 1) / 2`.
|
list, since `1 + ⋯ + n ≡ n * (n + 1) / 2`.
|
||||||
|
|
||||||
\begin{code}
|
|
||||||
upto : ℕ → List ℕ
|
|
||||||
upto zero = []
|
|
||||||
upto (suc n) = suc n ∷ upto n
|
|
||||||
|
|
||||||
sum : List ℕ → ℕ
|
|
||||||
sum [] = zero
|
|
||||||
sum (x ∷ xs) = x + sum xs
|
|
||||||
|
|
||||||
sum-upto : ∀ (n : ℕ) → 2 * sum (upto n) ≡ n * suc n
|
|
||||||
sum-upto zero = refl
|
|
||||||
sum-upto (suc n) = {!!}
|
|
||||||
{-
|
|
||||||
begin
|
|
||||||
2 * sum (upto (suc n))
|
|
||||||
≡⟨⟩
|
|
||||||
2 * sum (suc n ∷ upto n)
|
|
||||||
≡⟨⟩
|
|
||||||
2 * (suc n + sum (upto n))
|
|
||||||
≡⟨ +-dist-* 2 (suc n) (sum (upto n)) ⟩
|
|
||||||
(2 * suc n) + (2 * sum (upto n))
|
|
||||||
≡⟨ cong (_+_ (2 * suc n)) (sup-upto n) ⟩
|
|
||||||
(2 * suc n) + (n * suc n)
|
|
||||||
≡⟨ sym (+-dist-*
|
|
||||||
-}
|
|
||||||
\end{code}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Reverse
|
## Reverse
|
||||||
|
@ -431,6 +404,45 @@ ex₆ : foldr _+_ 0 ([ 1 , 2 , 3 ]) ≡ 6
|
||||||
ex₆ = refl
|
ex₆ = refl
|
||||||
\end{code}
|
\end{code}
|
||||||
|
|
||||||
|
\begin{code}
|
||||||
|
downto : ℕ → List ℕ
|
||||||
|
downto zero = []
|
||||||
|
downto (suc n) = suc n ∷ downto n
|
||||||
|
|
||||||
|
sum : List ℕ → ℕ
|
||||||
|
sum = foldr _+_ 0
|
||||||
|
|
||||||
|
infix 6 _+
|
||||||
|
|
||||||
|
_+ : ℕ → ℕ → ℕ
|
||||||
|
(m +) n = m + n
|
||||||
|
|
||||||
|
cong2 : ∀ {A B C : Set} {x x′ : A} {y y′ : B} →
|
||||||
|
(f : A → B → C) → (x ≡ x′) → (y ≡ y′) → (f x y ≡ f x′ y′)
|
||||||
|
cong2 f x≡x′ y≡y′ rewrite x≡x′ | y≡y′ = refl
|
||||||
|
|
||||||
|
sum-downto : ∀ (n : ℕ) → sum (downto n) * 2 ≡ suc n * n
|
||||||
|
sum-downto zero = refl
|
||||||
|
sum-downto (suc n) =
|
||||||
|
begin
|
||||||
|
sum (downto (suc n)) * 2
|
||||||
|
≡⟨⟩
|
||||||
|
sum (suc n ∷ downto n) * 2
|
||||||
|
≡⟨⟩
|
||||||
|
(suc n + sum (downto n)) * 2
|
||||||
|
≡⟨ distribʳ-*-+ 2 (suc n) (sum (downto n)) ⟩
|
||||||
|
suc n * 2 + sum (downto n) * 2
|
||||||
|
≡⟨ cong (suc n * 2 +) (sum-downto n) ⟩
|
||||||
|
suc n * 2 + suc n * n
|
||||||
|
≡⟨ cong2 _+_ (*-comm (suc n) 2) (*-comm (suc n) n) ⟩
|
||||||
|
2 * suc n + n * suc n
|
||||||
|
≡⟨ sym (distribʳ-*-+ (suc n) 2 n)⟩
|
||||||
|
(2 + n) * suc n
|
||||||
|
∎
|
||||||
|
\end{code}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
\begin{code}
|
\begin{code}
|
||||||
data _∈_ {A : Set} (x : A) : List A → Set where
|
data _∈_ {A : Set} (x : A) : List A → Set where
|
||||||
|
|
56
src/ListsAns.lagda
Normal file
56
src/ListsAns.lagda
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
---
|
||||||
|
title : "Lists Answers"
|
||||||
|
layout : page
|
||||||
|
permalink : /ListsAns
|
||||||
|
---
|
||||||
|
|
||||||
|
\begin{code}
|
||||||
|
import Relation.Binary.PropositionalEquality as Eq
|
||||||
|
open Eq using (_≡_; refl; sym; trans; cong)
|
||||||
|
open Eq.≡-Reasoning
|
||||||
|
open import Data.Nat using (ℕ; suc; zero; _+_; _*_)
|
||||||
|
open import Data.Nat.Properties.Simple using (*-comm; distribʳ-*-+)
|
||||||
|
open import Data.List using (List; []; _∷_; _++_; foldr)
|
||||||
|
|
||||||
|
*-distrib-+ : ∀ (m n p : ℕ) → (m + n) * p ≡ m * p + n * p
|
||||||
|
*-distrib-+ m n p = distribʳ-*-+ p m n
|
||||||
|
\end{code}
|
||||||
|
|
||||||
|
*Sum of count*
|
||||||
|
|
||||||
|
\begin{code}
|
||||||
|
sum : List ℕ → ℕ
|
||||||
|
sum = foldr _+_ 0
|
||||||
|
|
||||||
|
countdown : ℕ → List ℕ
|
||||||
|
countdown zero = []
|
||||||
|
countdown (suc n) = suc n ∷ countdown n
|
||||||
|
|
||||||
|
infix 6 _+
|
||||||
|
|
||||||
|
_+ : ℕ → ℕ → ℕ
|
||||||
|
(m +) n = m + n
|
||||||
|
|
||||||
|
cong2 : ∀ {A B C : Set} {x x′ : A} {y y′ : B} →
|
||||||
|
(f : A → B → C) → (x ≡ x′) → (y ≡ y′) → (f x y ≡ f x′ y′)
|
||||||
|
cong2 f x≡x′ y≡y′ rewrite x≡x′ | y≡y′ = refl
|
||||||
|
|
||||||
|
sum-countdown : ∀ (n : ℕ) → sum (countdown n) * 2 ≡ suc n * n
|
||||||
|
sum-countdown zero = refl
|
||||||
|
sum-countdown (suc n) =
|
||||||
|
begin
|
||||||
|
sum (countdown (suc n)) * 2
|
||||||
|
≡⟨⟩
|
||||||
|
sum (suc n ∷ countdown n) * 2
|
||||||
|
≡⟨⟩
|
||||||
|
(suc n + sum (countdown n)) * 2
|
||||||
|
≡⟨ *-distrib-+ (suc n) (sum (countdown n)) 2 ⟩
|
||||||
|
suc n * 2 + sum (countdown n) * 2
|
||||||
|
≡⟨ cong (suc n * 2 +) (sum-countdown n) ⟩
|
||||||
|
suc n * 2 + suc n * n
|
||||||
|
≡⟨ cong2 _+_ (*-comm (suc n) 2) (*-comm (suc n) n) ⟩
|
||||||
|
2 * suc n + n * suc n
|
||||||
|
≡⟨ sym (*-distrib-+ 2 n (suc n))⟩
|
||||||
|
(2 + n) * suc n
|
||||||
|
∎
|
||||||
|
\end{code}
|
|
@ -6,7 +6,7 @@ permalink : /PropertiesAns
|
||||||
|
|
||||||
\begin{code}
|
\begin{code}
|
||||||
open import Data.Nat using (ℕ; suc; zero; _+_; _*_; _∸_)
|
open import Data.Nat using (ℕ; suc; zero; _+_; _*_; _∸_)
|
||||||
open import Properties using (+-assoc; +-comm)
|
open import Data.Nat.Properties.Simple using (+-assoc; +-comm)
|
||||||
open import Relation.Binary.PropositionalEquality using (_≡_; refl; sym)
|
open import Relation.Binary.PropositionalEquality using (_≡_; refl; sym)
|
||||||
|
|
||||||
\end{code}
|
\end{code}
|
||||||
|
|
Loading…
Reference in a new issue