Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieve an element from a list of tuple with more than 2 elements (Haskell)

I'm new to Haskell and need some help on this situation. I have the following list

-- create a type for bank account
type AcNo = String
type Name = String
type City = String
type Amnt = Int

type AcInfo = [(AcNo, Name, City, Amnt)]

-- function to get the data of bank accounts to a list of tuples
bankAccounts :: AcInfo
bankAccounts = [("oo1", "Sahan", "Colomb", 100),("002", "John", "Jafna", 200)]

My requirement is to get the amount corresponding to the account number, e.g., for 001 it should give 100.

The function I wrote was this

--Function to check the balance of a person
checkBalance :: bankAccounts -> AcNo -> Amnt
checkBalance dbase number = Amnt|(AcNo, Name, City, Amnt) <- dbase, AcNo==number}

The second line is where im stuck at which gives the error message

Syntax error in input (unexpected `|')

I'd like to have some help on this. Thanx.

like image 546
Thaveesha Gamage Avatar asked Dec 12 '22 11:12

Thaveesha Gamage


1 Answers

Additional to Greg's excellent answer I want to point out that you shouldn't use tuples for bigger sets of values that constitute a logical unit. I would suggest to have an Account type, e.g. using record syntax, which makes things like accessing elements or making account changes more convenient:

data Account = Account { acNo :: AcNo
                       , name :: Name
                       , city :: City
                       , amount :: Amnt
                       } deriving (Eq, Show)   

See http://learnyouahaskell.com/making-our-own-types-and-typeclasses#record-syntax for details.

Then you should write functions in terms of Account, not in terms of AcInfo, and use normal list functions. Often the extractor functions provided by the record are good enough, as in your example:

checkBalance :: [Account] -> AcNo -> Maybe Amnt
checkBalance dbase number = fmap amount $ find (\acc -> number == acNo acc) dbase

Here acNo acc gets the account number and amount acc gets the amount from an account.

like image 153
Landei Avatar answered Jan 16 '23 11:01

Landei