Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a strict version of length #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions E01/A3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module E01.A3 where
-- nicht zur Verfügung, daher definieren wir sie hier erneut, müssen aber
-- den automatischen Import mit hiding unterdrücken.
import Prelude hiding (length, maximum)
import Data.List (foldl')

-- (a)
l :: [[Int]]
Expand Down Expand Up @@ -44,6 +45,10 @@ length (x:xs) = 1 + length xs
length1 :: [Int] -> Int
length1 = foldr (\x y -> y + 1) 0

-- Alternativ: Strikte Faltung mit Data.List.foldl'.
length2 :: [Int] -> Int
length2 = foldl' (\x _ -> x + 1) 0

maximum :: [Int] -> Int
maximum [] = error "A3.maximum: empty list"
maximum [x] = x
Expand Down