-
Notifications
You must be signed in to change notification settings - Fork 4
2. French ‐ CoxyHasList
Bernard Sibanda edited this page Sep 10, 2025
·
1 revision
Coxygen Global 10-09-2025 LICENCE : MIT
- 🔧 Module complet (prêt à copier-coller)
- ✅ Mini aide de test :
assert - 📏 Taille d’une liste :
sizeOfList - 🔎 Rechercher/Vérifier des éléments :
isItemInTheList,findPosItemList,findElemList - 🪪 Accès sûr :
firstElemList - 🧰 Conserver/Supprimer avec condition :
filterItemsList,removeItemList - ➕ Combiner des listes :
joinList - ✂️ Retirer les extrémités :
notHeadList,notLastList,bodyElemList - 🧱 Ajouter en tête :
addItemList - 🔁 Inverser :
reverseList - 🎯 Dernier élément (sécurisé) :
lastElemList - 🗂️ Trier en croissant/décroissant :
orderList - 🔠 Changer la casse (sans imports) :
strToUpper,strToLower - 🧼 Nettoyages de chaînes :
removeSpaces,removeVowels,removeNoAlphabetLetters,removeSymbols,lettersOnly - ✍️ Rechercher & Remplacer (sous-chaîne) :
findAndReplaceStrings,startsWith - 🏆 Extrêmes :
biggestItemList,smallestItemList - 📚 Glossaire
module CoxyHasList where
-- 2) ✅ Mini aide de test
assert :: (Eq a, Show a) => String -> a -> a -> String
assert itemNameTested expected actual =
if expected == actual
then "[Pass] : " ++ itemNameTested ++ " Answer : " ++ show actual
else "[Fail] : " ++ itemNameTested ++
" Expected : " ++ show expected ++
" But got : " ++ show actual
-- 3) 📏 Taille de liste
sizeOfList :: [a] -> Int
sizeOfList [] = 0
sizeOfList [_] = 1
sizeOfList (_:xs) = 1 + sizeOfList xs
-- 4) 🔎 Rechercher/Vérifier des éléments
isItemInTheList :: (Eq a) => a -> [a] -> Bool
isItemInTheList _ [] = False
isItemInTheList item (x:xs)
| item == x = True
| otherwise = isItemInTheList item xs
-- Alias utilisé par main dans votre texte
findElemList :: Eq a => a -> [a] -> Bool
findElemList = isItemInTheList
findPosItemList :: Eq a => [a] -> a -> Maybe Int
findPosItemList xs y = go 0 xs
where
go :: Eq a => Int -> [a] -> Maybe Int
go _ [] = Nothing
go i (x:xs)
| x == y = Just i
| otherwise = go (i + 1) xs
-- 5) 🪪 Accès sûr
firstElemList :: [a] -> Maybe a
firstElemList [] = Nothing
firstElemList (x :_) = Just x
-- 6) 🧰 Conserver/Supprimer avec condition
filterItemsList :: (a -> Bool) -> [a] -> [a]
filterItemsList _ [] = []
filterItemsList p (x:xs)
| p x = x : filterItemsList p xs
| otherwise = filterItemsList p xs
removeItemList :: Eq a => a -> [a] -> [a]
removeItemList x = filter (\y -> y /= x)
-- 7) ➕ Combiner des listes
joinList :: [a] -> [a] -> [a]
joinList xs ys = xs ++ ys
-- 8) ✂️ Retirer les extrémités
notHeadList :: [a] -> [a]
notHeadList [] = []
notHeadList (_:xs) = xs
notLastList :: [a] -> [a]
notLastList [] = []
notLastList [_] = []
notLastList (x:xs) = x : notLastList xs
-- Deux versions équivalentes existaient ; on garde la plus simple :
bodyElemList :: [a] -> [a]
bodyElemList xs = notLastList (notHeadList xs)
-- 9) 🧱 Ajouter en tête
addItemList :: a -> [a] -> [a]
addItemList a as = a : as
-- 10) 🔁 Inverser
reverseList :: [a] -> [a]
reverseList [] = []
reverseList (a:as) = reverseList as ++ [a]
-- 11) 🎯 Dernier élément (sécurisé)
lastElemList :: [a] -> Maybe a
lastElemList [] = Nothing
lastElemList [a] = Just a
lastElemList (_:as) = lastElemList as
-- 12) 🗂️ Trier croissant/décroissant (style quicksort)
orderList :: (Ord a) => [a] -> String -> [a]
orderList [] _ = []
orderList (x:xs) dir
| dir == "asc" =
let smaller = [y | y <- xs, y <= x]
larger = [y | y <- xs, y > x]
in orderList smaller dir ++ [x] ++ orderList larger dir
| dir == "des" =
let smaller = [y | y <- xs, y > x]
larger = [y | y <- xs, y <= x]
in orderList smaller dir ++ [x] ++ orderList larger dir
| otherwise = []
-- 13) 🔠 Changer la casse (ASCII uniquement, sans imports)
strToUpper :: String -> String
strToUpper str =
[ if 'a' <= c && c <= 'z'
then toEnum (fromEnum c - 32)
else c
| c <- str ]
strToLower :: String -> String
strToLower str =
[ if 'A' <= c && c <= 'Z'
then toEnum (fromEnum c + 32)
else c
| c <- str ]
-- 14) 🧼 Nettoyages de chaînes
removeSpaces :: String -> String
removeSpaces str = [ y | y <- str
, y /= '\n' && y /= '\r' && y /= ' ' && y /= '\t' ]
removeVowels :: String -> String
removeVowels str = [ y | y <- str, y `notElem` "aeiou" ]
-- corrigé : ne garder que les lettres de l’alphabet (ASCII)
removeNoAlphabetLetters :: String -> String
removeNoAlphabetLetters str = [ y | y <- str
, y `elem` (['a'..'z'] ++ ['A'..'Z']) ]
-- en réalité « conserve uniquement les lettres » (donc supprime les symboles)
removeSymbols :: String -> String
removeSymbols str = [ y | y <- str
, y `elem` (['a'..'z'] ++ ['A'..'Z']) ]
-- ne garder que les consonnes (ASCII)
lettersOnly :: String -> String
lettersOnly str =
[ y | y <- str
, y `elem` (['a'..'z'] ++ ['A'..'Z'])
, y `notElem` "aeiou" ]
-- 15) ✍️ Rechercher & Remplacer (sous-chaîne, non chevauchant)
findAndReplaceStrings :: String -> String -> String -> String
findAndReplaceStrings old new s
| null old = s
| otherwise = go s
where
n = length old
go [] = []
go str
| startsWith old str = new ++ go (drop n str)
| otherwise = head str : go (tail str)
startsWith :: String -> String -> Bool
startsWith [] _ = True
startsWith _ [] = False
startsWith (x:xs) (y:ys) = x == y && startsWith xs ys
-- 16) 🏆 Extrêmes
biggestItemList :: Ord a => [a] -> a
biggestItemList [] = error "biggestItemList: empty list"
biggestItemList [x] = x
biggestItemList (x:xs) =
let m = biggestItemList xs
in if x >= m then x else m
smallestItemList :: Ord a => [a] -> a
smallestItemList [] = error "smallestItemList: empty list"
smallestItemList [x] = x
smallestItemList (x:xs) =
let m = smallestItemList xs
in if x <= m then x else m-
🧠 Qu’est-ce que c’est ? Petite aide pour vérifier attendu vs obtenu.
-
🧪 Utilisation :
assert "reverse" [3,2,1] (reverseList [1,2,3])
main ::IO()
main = do
print $ findElemList 3 [2,5,6]
let l1 = [2,3,4]
let l2 = [4,6,7,8,9,3,5,1,56,1]
print $ joinList l1 l2
print $ assert "Test 3 functions head, last and body using [4,6,7,8,9]" [6,7,8] $ bodyElemList l2
print $ addItemList 78 l1
print $ reverseList l2
print $ assert "List reversed " [9,8,7,6,4] $ reverseList [4,6,7,8,9]
print $ lastElemList l2
print $ orderList l2 "des"
print $ strToUpper "bernarD"
print $ strToLower "THIS IS BIGG"
print $ removeSpaces "this is nothing \r \n "
print $ removeVowels "hellow world"
print $ removeSymbols "Why? is th94375478484 .<>"
let string = "the quick brown fox jumps over the lazy dog"
print $ lettersOnly string
-- print $ findAndReplace "the" "256" string
print $ findAndReplaceStrings "the" "145" string
print $ findAndReplaceStrings "a" "@" string - Compte les éléments récursivement.
Pourquoi ces motifs ?
-
[]a la taille 0. -
[_](un seul élément) a la taille 1. - Sinon, on enlève la tête et on ajoute 1.
-
isItemInTheList→ appartenance booléenne. -
findPosItemList→ premier indice (Maybe Int). -
findElemList→ alias utilisé dans votremain.
- Tête sûre (retourne
Nothingsi la liste est vide).
-
filterItemsList p xsconserve les éléments pour lesquelsp xestTrue. -
removeItemList v xssupprime toutes les occurrences dev.
- Concatène deux listes.
- Supprimer le premier, le dernier, ou les deux extrémités.
- Ajoute en tête : O(1).
- Inversion récursive classique.
- Dernier élément sûr via
Maybe.
- Tri style quicksort ;
"asc"ou"des". - Tout autre indicateur →
[](selon votre choix de conception).
- Transformations ASCII via
fromEnum/toEnum(sans imports).
-
Corrections effectuées :
-
removeNoAlphabetLetters: utilisait"a..Z"(pas une plage valide). Désormais, utilisation correcte des plages. -
removeSymbols: le nom suggère « enlever les symboles », et l’implémentation conserve uniquement les lettres (donc les symboles sont bien retirés). Comportement maintenu.
-
- Remplacement non-chevauchant (comportement standard).
- Fonctionne pour caractères, mots, groupes de chiffres — toute sous-chaîne.
- Extrêmes récursifs (lèvent une erreur sur liste vide — comportement d’origine).
- Eq / Ord : Typeclasses pour l’égalité / l’ordre.
-
Maybe : Type optionnel :
Just xouNothing. -
Appariement de motifs (Pattern Matching) : Formes comme
x:xs,[]. -
Gardes (Guards) :
| condition = ...pour choisir des branches. -
Compréhension de liste :
[y | y <- xs, prédicat y]. - Récursion : Fonction qui s’appelle elle-même sur une entrée plus petite.
-
Concaténation (
++) : Joindre des listes bout à bout.
Bernard Sibanda is a global Technology Entrepreneur, Web3 and Software Consultant with a deep focus on Cardano Blockchain, Midnight and Community building.
Key Positions:
- Founder, CTO, Developer Advocate cohort #1, Fullstake Developer, Cardano Ambassador, Catalyst Project Manager, DREP-WIMS:
- Co-founder of ABL Tech and Cardano Africa Live
- EBU-certified Plutus Pioneer (Plutus/Haskell)
- Cohort #1 Plutus Pioneer Developer
- Catalyst Community Reviewer & Funded Projects Manager
-
DRep for WIMS-Cardano (ID:
drep1yguj8zu48n99pv70yl6ckzt9hdgjy8yjnlqs2uyzcpafnjgu4vkul) - Intersect Developer Advocate
- Intersect Committe Member 2025-2026
- Cardano Marketer,Promoter and blogger
- Cardano Open Source Contributor
- Cardano communities and events organizer and builder
- Cardano Ambassador for South Africa
Official links:
- Stablecoins Dex
- Coxygen Global Universities
- WIMS Cardano Global
- Cardano Africa Live
- WIMS Cardano Videos
- Cardano Smart Contract Videos
- Fullstack IT Consulting
Social links: