update code examples

This commit is contained in:
Leon Vatthauer 2024-01-11 13:42:27 +01:00
parent 7f3330f45c
commit 4d8bb098af
Signed by: leonv
SSH key fingerprint: SHA256:G4+ddwoZmhLPRB1agvXzZMXIzkVJ36dUYZXf5NxT+u8
3 changed files with 10 additions and 15 deletions

View file

@ -8,6 +8,7 @@ main = do
print (hd []::[String]) print (hd []::[String])
reverse :: [a] -> [a] reverse :: [a] -> [a]
reverse = reverseAcc [] reverse l = reverseAcc l []
where where
reverseAcc = foldl (flip (:)) reverseAcc [] a = a
reverseAcc (x:xs) a = reverseAcc xs (x:a)

View file

@ -13,16 +13,12 @@ module reverse where
now : A Delay A now : A Delay A
later : (Delay A) Delay A later : (Delay A) Delay A
foldl : {A B : Set} (A B A) A Colist B Delay A
foldl c n [] = now n
foldl c n (x xs) = later ( foldl c (c n x) ( xs))
-- reversing possibly infinite lists
reverse : {A : Set} Colist A Delay (Colist A) reverse : {A : Set} Colist A Delay (Colist A)
reverse {A} = reverseAcc [] reverse {A} = reverseAcc []
where where
reverseAcc : Colist A Colist A Delay (Colist A) reverseAcc : Colist A Colist A Delay (Colist A)
reverseAcc = foldl (λ xs x x ( xs)) -- 'flip _∷_' with extra steps reverseAcc [] a = now a
reverseAcc (x xs) a = later ( reverseAcc ( xs) (x ( a)))
run_for_steps : {A : Set} Delay A Delay A run_for_steps : {A : Set} Delay A Delay A
run now x for n steps = now x run now x for n steps = now x

View file

@ -19,9 +19,10 @@ head (x:xs) = x
\vskip 1cm \vskip 1cm
\begin{minted}{agda} \begin{minted}{agda}
reverse :: [a] -> [a] reverse :: [a] -> [a]
reverse = reverseAcc [] reverse l = reverseAcc l []
where where
reverseAcc = foldl (flip (:)) reverseAcc [] a = a
reverseAcc (x:xs) a = reverseAcc xs (x:a)
\end{minted} \end{minted}
\mycallout<4->{21, 2}{ \mycallout<4->{21, 2}{
@ -83,14 +84,11 @@ run later x for suc n steps = run ♭ x for n steps
\begin{frame}[c, fragile]{Partiality in Agda}{Reversing (possibly infinite) lists} \begin{frame}[c, fragile]{Partiality in Agda}{Reversing (possibly infinite) lists}
\centering \centering
\begin{minted}{agda} \begin{minted}{agda}
foldl : ∀ {A B : Set} → (A → B → A) → A → Colist B → Delay A
foldl c n [] = now n
foldl c n (x ∷ xs) = later (♯ foldl c (c n x) (♭ xs))
reverse : ∀ {A : Set} → Colist A → Delay (Colist A) reverse : ∀ {A : Set} → Colist A → Delay (Colist A)
reverse {A} = reverseAcc [] reverse {A} = reverseAcc []
where where
reverseAcc : Colist A → Colist A → Delay (Colist A) reverseAcc : Colist A → Colist A → Delay (Colist A)
reverseAcc = foldl (λ xs x → x ∷ (♯ xs)) reverseAcc [] a = now a
reverseAcc (x ∷ xs) a = later (♯ reverseAcc (♭ xs) (x ∷ (♯ a)))
\end{minted} \end{minted}
\end{frame} \end{frame}