[Haskell-cafe] Re: Need help from a newby

ChrisK haskell at list.mightyreason.com
Fri Nov 2 00:19:40 EDT 2007

karle wrote:
> ChrisK-3 wrote:
>> karle wrote:
>>> My declaration is as followed:-
>>> type Address = Int 
>>> data Port = C | D deriving(Eq,Show) 
>>> data Payload = UP[Char] | RTDP(Address,Port) deriving(Eq,Show) 
>>> data Pkgtype = RTD | U deriving(Eq,Show)
>>> type Pkg = (Pkgtype,Address,Payload) 
>>> type Table = [(Address,Port)] 
>>> findport::[Pkg]->[Table]->[([Pkg],[Pkg])]
>>> findport [(pt,ad,pa)]  [(a,p)] 
>>>       | ( p == C) = ([pt,ad,a],[])
>>>       | otherwise = ([],[pt,ad,a])
>>> Error received:-
>>> Type error in explicitly typed binding
>>> *** Term           : [(a,p)]
>>> *** Type           : [(a,b)]
>>> *** Does not match : [Table]
>>> Can anyone please help?
>> There seems to be several things that look wrong.
>> Could you explain what findport is supposed to be doing?
>> findport is just to check if the port in the Table  is equal to C then it
>> will place the packet in the first of the list. Otherwise it will place
>> the packet in the second of the list.

So for each Pkg in [Pkg] the findport function takes the Address element in the
Pkg and looks it up in the Table.  If the Address is C or D the original pkg
gets places in the first or second output list.

The 'partition' function from Data.List could help here:
> partition :: (a -> Bool) -> [a] -> ([a], [a])
> The partition function takes a predicate a list and returns the pair of lists
> of elements which do and do not satisfy the predicate, ...

But your findport function is fairly broken right now.

The output (pt,ad,a) should be (pt,ad,pa).  This was likely a typographical
error and is caught by the type system.

The declared type of the second argument should be Table, not [Table].  This is
the simple error that the compiler is reporting.

The first argument is matched against the pattern [(pt,ad,pa)] which only
matched lists with a single Pkg.  This is also true of the second argument
getting pattern matched against [(a,p)] since this only matches Tables with a
single entry.  These are errors in how findport has been written, I'll give you
a chance to try fixing each of these.  You will need to handle lists with any
number of elements (including empty lists).

You also do not say what should happen to a Pkg with an Address that is not in
the Table.


More information about the Haskell-Cafe mailing list