Im doing a small system from Haskell and im getting two errors called
“Last generator in do {…} must be an expression” and
“Type error in explicitly typed binding
Term : (fname,lname,cnic)
Type : (a,b,c)
Does not match : Database“
im new to haskell so plz help me out.
-------Data types-------
type FirstName = String
type LastName = String
type CustomerNIC = String
type Database = ( FirstName , LastName , CustomerNIC )
--type Details = [Database]
------Data base---------
exampleBase :: [Database]
exampleBase = [ ( "Kevin" , "Desilva" , "8929323V" ),( "Nimal" , "Perera" , "89120323V" ) ]
-------Main Menu-----------------------------
getInt :: IO Int
getInt = do line <- getline
return (read line :: Int)
selectsearch ::IO()
selectsearch = do
putStr"\n\n\t 1.Search by NIC:"
putStr"\n\n\t 2.Search by First Name:"
putStr"\n\n\t Your Chocie:"
input<-getInt
subsearch input
subsearch :: Int->IO()
subsearch x = do
if(x=1) then do
putStr"\n\t Enter NIC:"
cnic <- getLine
subsearch
else if (x=2) then do
putStr"\n\t Enter First Name:"
cnic <- getLine
subsearch
else if (x=3) then putStr "\n ERROR"
selectsearch
else MainMenu
------- Search ------------
getfName :: Database -> FirstName
getfName ( fname , lname , cnic ) = fname
searchByFirstName :: Database -> FirstName -> Database
searchByFirstName (a:ax) fname
| fname == getfName a = a
| length ax == 0 && getfName a/= fname = ("No Data","",0)
| otherwise = searchByFirstName ax fname
A few points:
There are weird indentation things throughout. Make sure you’re using spaces and not tabs.
In
subsearch, you often havecnic <- getLine(when you probably wantgetInt), but then not use that in the recursive call.In your
ifstatements, you should havex == 1, etc. rather thanx = 1. Also consider using a case statement or guards rather than nested if-then-else.You’re missing a “do” in your
x == 3case.getIntis identical toreadLn.Your
searchByFirstNamefunction could be written better.A
Databaseis made of threeStringfields, but your error case insearchByFirstNamereturns a value of type(String, String, Int).