From 1318e8c63aa57f9c9e6c5fc95daf8c4313a50e52 Mon Sep 17 00:00:00 2001 From: Niklas Heim Date: Tue, 9 Apr 2024 10:12:39 +0200 Subject: [PATCH] reverse list --- lectures/lecture08.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lectures/lecture08.md b/lectures/lecture08.md index 21efdf7..d08b136 100644 --- a/lectures/lecture08.md +++ b/lectures/lecture08.md @@ -279,10 +279,21 @@ instance Show a => Show (List a) where which will result in lists printed like below ```haskell -𝝺> (Cons 1 (Cons 2 (Cons 3 Nil))) +𝝺> lst = (Cons 1 (Cons 2 (Cons 3 Nil))) <1,2,3> ``` +Let's do something useful with our list and reverse it! +```haskell +rev :: List a -> List a +rev lst = iter lst Nil where + iter Nil acc = acc + iter (Cons x l) acc = iter l (Cons x acc) + +𝝺> rev lst +<3,2,1> +``` + ### Arithmetic Expressions Another example that will prepare you for your [homework](/homework/hw03) is a simple expression