[Haskell-beginners] Use a list as data for a data type?

Bryce Verdier bryceverdier at gmail.com
Fri Feb 22 21:40:15 CET 2013


Brent,

Thank you for the responses. I appreciate it. Bummer about there not 
being some kind of built-in/generic shortcut.

Bryce

On 02/22/2013 12:14 PM, Brent Yorgey wrote:
> On Fri, Feb 22, 2013 at 02:07:43PM -0500, Brent Yorgey wrote:
>> On Fri, Feb 22, 2013 at 10:43:32AM -0800, Bryce Verdier wrote:
>>> Lets say I have a data type like so:
>>>
>>> data IpAddress = IpAddress {$
>>>           o1 :: !Int,$
>>>           o2 :: !Int,$
>>>           o3 :: !Int,$
>>>           o4 :: !Int$
>>>       } deriving (Show)$
>>>
>>> which you can create by doing:
>>> IpAddress 192 168 1 1
>>>
>>> Is there a way to do the same thing with the input data in a list? Like:
>>> IpAddress [192,168,1,1]
>>>
>>> or using some operator (or combination of operators):Like (I'm
>>> guessing (<????>) isn't in use):
>>> IpAddress <????> [192,168,1,1]
>> No, there isn't.  But you can make a function
>>
>>    ipAddress :: [Int] -> IpAddress
>>    ipAddress [a,b,c,d] = IpAddress a b c d
> For that matter you can define the operator (<????>) yourself:
>
> (<????>) :: (a -> a -> a -> a -> b) -> [a] -> b
> f <????> [a,b,c,d] = f a b c d
>
> Not sure how useful it is though.
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list