Re: [Haskell-cafe] Defining new operators
On 8/10/07, Shachaf Ben-Kiki [EMAIL PROTECTED] wrote: Also consider using: data Step = Step { ..., scenario :: Scenario, ... } Just to expand on Shachaf's answer, when defining a data type you can use a special record syntax to give names to each of the components, like this: data Monkey = M { species :: Species, color :: Color } This automatically gives you functions called 'species' and 'color' which you can apply to values of type Monkey to extract the relevant components. So in your case, you could write something like data Step = Step { ..., owner :: Scenario, ... } ...which would give you the 'owner' function you defined above for free, without having to type it out. To expand on Dan and Albert's answers, the 'functional idiom' would be to just write 'owner x' -- introducing something like a different definition of . to do 'record selection' might make things easier in the short term (i.e. if you are used to programming in an OO paradigm) but seems quite detrimental in the long term. Trying to force Haskell to look and feel like other languages you are used to is like taking two of the wheels off your Porsche because you are used to riding a bicycle. =) -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Defining new operators
Hi all, Given the follwing function: owner :: Step - Scenario owner (Step id scenario action state response) = scenario Is it possible to define the owner function in such way that I can write x.owner (returning the scenario related with the Step x)? Thanks in advance, Rodrigo. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Defining new operators
Prelude let (.) = flip ($) in 5 . odd True But please don't, the (.) operator is a sacred artifact in my religion, and I'd hate to see it desecrated... :( Dan rodrigo.bonifacio wrote: Hi all, Given the follwing function: owner :: Step - Scenario owner (Step id scenario action state response) = scenario Is it possible to define the owner function in such way that I can write x.owner (returning the scenario related with the Step x)? Thanks in advance, Rodrigo. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Defining new operators
Hi all, Given the follwing function: owner :: Step - Scenario owner (Step id scenario action state response) = scenario Is it possible to define the owner function in such way that I can write x.owner (returning the scenario related with the Step x)? Some people use (|), which looks like an arrow: (|) :: a - (a - b) - b x | f = f x Then you can use step | owner. Also consider using: data Step = Step { ..., scenario :: Scenario, ... } Shachaf ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Defining new operators
rodrigo.bonifacio wrote: owner :: Step - Scenario owner (Step id scenario action state response) = scenario Is it possible to define the owner function in such way that I can write x.owner (returning the scenario related with the Step x)? The . is already taken. Choose some other symbol, say x f = f x Now you can use xowner. P.S. What's wrong with plain and simple owner x? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Defining new operators
Hello, I have created the following function: dist :: String - [[String]] - [[String]] dist x y = [ x:e | e-y ] eg.: dist 1M [[], [2M], [2M, 3M]] = [[1M],[1M,2M],[1M,2M, 3M]] How can I create an operator that perform the same function as dist? I want to write something like: 1M ++ [[], [2M], [2M, 3M]] Thanks in advance. Rodrigo. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe