[Haskell-cafe] Newbie list question

2007-05-27 Thread junkywunky

type Person = (NI, Age, Balance)
type Bank = [Person]

credit :: Bank -> [Person]
credit [(a,b,c)] = [(a,b,c)]

This code works when I type in:

credit [(1,2,3)]

but doesn't work when I type in:

credit [(1,2,3),(4,5,6)]

Any help?

Thanks in advance.
-- 
View this message in context: 
http://www.nabble.com/Newbie-list-question-tf3823011.html#a10823175
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Newbie list question

2007-05-27 Thread Donald Bruce Stewart
junkywunky:
> 
> type Person = (NI, Age, Balance)
> type Bank = [Person]
> 
> credit :: Bank -> [Person]
> credit [(a,b,c)] = [(a,b,c)]
> 
> This code works when I type in:
> 
> credit [(1,2,3)]
> 
> but doesn't work when I type in:
> 
> credit [(1,2,3),(4,5,6)]

You're pattern matching in 'credit' on a list of a single element.
Perhaps you mean to write:

credit :: Bank -> [Person]
credit x = x

or perhaps return just the first element of the list:

credit [] = []
credit (x:xs) = x

You might want to start with one of the tutorials on Haskell programming
listed on haskell.org. The wikibook is quite a good start.

-- Don
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Newbie list question

2007-05-27 Thread Andrew Coppin

junkywunky wrote:

type Person = (NI, Age, Balance)
type Bank = [Person]

credit :: Bank -> [Person]
credit [(a,b,c)] = [(a,b,c)]

This code works when I type in:

credit [(1,2,3)]

but doesn't work when I type in:

credit [(1,2,3),(4,5,6)]

Any help?

Thanks in advance.
  


The expression [(1,2,3),(4,5,6)] doesn't match the pattern [(a,b,c)].

Now, since Bank and [Person] are actually the exact same type and the 
credit function actually does nothing, you could simply write


 credit x = x

(Or, for that matter, credit = id.) It would then work for both examples.

I presume that the idea is that the credit function will eventually do 
something - in that case, it might be helpful to say exactly what you 
want it to actually do.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Newbie list question

2007-05-27 Thread junkywunky

That's the thing. I want to return a list of people who are not overdrawn.
Something like:

type NI = Int
type Age = Int
type Balance = Int
type Person = (NI, Age, Balance)
type Bank = [Person]

credit :: Bank -> [Person]
credit [(a,b,c)] = [(a,b,c)] if c >= 0 
then [(a,b,c)] 
else error "overdrawn customer"

except this doesn't work with things like:

credit [(1,2,3),(4,5,6)] 

or

credit [(1,2,3),(4,5,6),(7,8,-9)] 


Andrew Coppin wrote:
> 
> junkywunky wrote:
>> type Person = (NI, Age, Balance)
>> type Bank = [Person]
>>
>> credit :: Bank -> [Person]
>> credit [(a,b,c)] = [(a,b,c)]
>>
>> This code works when I type in:
>>
>> credit [(1,2,3)]
>>
>> but doesn't work when I type in:
>>
>> credit [(1,2,3),(4,5,6)]
>>
>> Any help?
>>
>> Thanks in advance.
>>   
> 
> The expression [(1,2,3),(4,5,6)] doesn't match the pattern [(a,b,c)].
> 
> Now, since Bank and [Person] are actually the exact same type and the 
> credit function actually does nothing, you could simply write
> 
>   credit x = x
> 
> (Or, for that matter, credit = id.) It would then work for both examples.
> 
> I presume that the idea is that the credit function will eventually do 
> something - in that case, it might be helpful to say exactly what you 
> want it to actually do.
> 
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Newbie-list-question-tf3823011.html#a10823244
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Newbie list question

2007-05-27 Thread Donald Bruce Stewart
junkywunky:
> 
> That's the thing. I want to return a list of people who are not overdrawn.
> Something like:
> 
> type NI = Int
> type Age = Int
> type Balance = Int
> type Person = (NI, Age, Balance)
> type Bank = [Person]
> 
> credit :: Bank -> [Person]
> credit [(a,b,c)] = [(a,b,c)] if c >= 0 
>   then [(a,b,c)] 
>   else error "overdrawn customer"
> 
> except this doesn't work with things like:
> 

Right, you mean to write a list filter. List comprehensions are useful
for this:


credit xs = [ p | p@(a,b,c) <- xs, c >= 0 ] 

or maybe:

credit xs = filter ok xs
where
ok (a,b,c) = c >= 0

-- Don
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Newbie list question

2007-05-27 Thread Andrew Coppin

Donald Bruce Stewart wrote:


Don types faster than me. ;-)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Newbie list question

2007-05-27 Thread David Tolpin

> type Person = (NI, Age, Balance)
> type Bank = [Person]
>
> credit :: Bank -> [Person]
> credit [(a,b,c)] = [(a,b,c)] if c >= 0
>   then [(a,b,c)]
>   else error "overdrawn customer"
>
> except this doesn't work with things like:
>
> credit [(1,2,3),(4,5,6)]
>

Hi,

that's because Haskell syntax is made for brains with high modality. When you 
declare a type, writing a type signature in square brackets make it to be a 
list of arbitrary number of elements of the inner type; when you write a 
pattern, one with an element in square brackets matches a single-element list. 
What you want is to

credit abcs = filter (\(a,b,c) -> c>=0) abcs

And if you think it looks like a machine-level assembly language, then you are 
probably right.

David
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe