Salut à toutes et à tous !
Vous l'aurez peut-être déjà deviné au titre, je débute en Haskell (soit dit au passage j'adore déjà !).
Alors que je m'amusais avec les structures de données, voici que ghci me fait une petite surprise. Voici mon code:
Quand, avec ghci, j'essaye d'exécuter cette ligne :Code:
1
2
3
4
5
6
7
8
9
10
11 data BookInfo = Book Int String [String] deriving (Show) data MagazineInfo = Magazine Int String [String] deriving (Show) data CustomerID = CustomerID*Int deriving (Show) data BookReview = BookReview BookInfo CustomerID String deriving (Show)
Eh bien il me sort un vilain message d'erreur :cry:Code:BookReview (Book 0 "Title" []) 14 "Hello"
Bon, j'ai bien compris le message qu'il veut me faire passer : il ne sait pas convertir un "Num" en "CustomerID". Et gentiment, il me suggère une solution possible : "add an instance declaration for (Num CustomerID)".Code:
1
2
3
4
5
6
7 No instance for (Num CustomerID) arising from the literal `14' at <interactive>:1:31-32 Possible fix: add an instance declaration for (Num CustomerID) In the second argument of `BookReview', namely `14' In the expression: BookReview (Book 0 "Title" []) 14 "Hello" In the definition of `it': it = BookReview (Book 0 "Title" []) 14 "Hello"
Comment faire ce qu'il me propose ?
En passant, il y a un petit exercice que je n'ai pas su résoudre (tiré de http://book.realworldhaskell.org/rea...functions.html ):
J'avoue, je sèche :PCitation:
Écrivez une fonction qui retourne l'avant dernier élément d'une liste.
Edit : ah, j'ai trouvé pour cette dernière question :
Code:
1
2 oneButLast (x:_:[]) = x oneButLast (x:xs) = oneButLast(xs)