Re: [Haskell-cafe] Newbie syntax question
On Fri, 16 Sep 2005, ChrisK wrote: So I think map ( (flip foo) 5 ) my_list_of_lists_of_doubles Function application is left-associative, so you can save parentheses. map (flip foo 5) my_list_of_lists_of_doubles ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie syntax question
1. map (flip foo 5) my_list_of_lists_of_doubles 2. map (`foo` 5) my_list_of_lists_of_doubles 3. map (\x-foo x 5) my_list_of_lists_of_doubles 4. [foo x 5 | x - my_list_of_lists_of_doubles] 5. map (foo `flip` 5) my_list_of_lists_of_doubles Thank you all, for your answers. This nice discussion made even more clear to me how powerful Haskell is. Cheers, André ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie syntax question
Hi, Andre, map (foo 5) my_list_of_lists_of_doubles ...But how to do that (if possible) when I invert the parameters list ?! Let me add one more solution, and then summarize: The problem disapears if you use a list comprehension instead of map: [foo x 5 | x - my_list_of_lists_of_doubles] List comprehensions are funny. Sometimes, they are powerful, consise, and crystal clear. Other times they can seem wordy and confusing. In this case it works well. OK, so the four solutions that have been suggested are: 1. map (flip foo 5) my_list_of_lists_of_doubles 2. map (`foo` 5) my_list_of_lists_of_doubles 3. map (\x-foo x 5) my_list_of_lists_of_doubles 4. [foo x 5 | x - my_list_of_lists_of_doubles] (1) and (2) are closest to what you originally wanted to write. They do not neatly generalize beyond the second parameter. (3) is more general - but a little less clear. (4) is the most different from your original idea. In a certain sense, it is even more general than (3). In this case, it is also simple and clear - though you may not always be so lucky. BTW - great question! Regards, Yitz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie syntax question
map (foo 5) my_list_of_lists_of_doubles 1. map (flip foo 5) my_list_of_lists_of_doubles 2. map (`foo` 5) my_list_of_lists_of_doubles 3. map (\x-foo x 5) my_list_of_lists_of_doubles 4. [foo x 5 | x - my_list_of_lists_of_doubles] well, i've followed this discussion a while, but i did not see that solution i used for years. (i like solution 2, never thought of it before.) my solution is a variant of 1, that makes the reading a little bit easier -- at least to me. 5. map (foo `flip` 5) my_list_of_lists_of_doubles the `flip` shows the position behind foo, where to put the parameter. its a pitty that it only works if there is only one parameter behind `flip`. so, with abcd :: a-b-c-d-x abcd a b `flip` d :: c-x works, but abcd a `flip` c d :: b-x does not. experimenting with Data.Arrow, it could look like: (abcd a ($ c) ($ d)) :: b-x or with a flip-arrow combination: (abcd a `flip` c) ($ d) :: b-x so, instead of hacking with arrows on it, i prefer solutions 3 and 4 whenever 5 does not work. - marc ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie syntax question
Hello André, Friday, September 16, 2005, 10:55:16 PM, you wrote: AVAdC map (foo 5) my_list_of_lists_of_doubles AVAdC But how to do that (if possible) when I invert the parameters list ?! third, most iniversal solution is to create anonymous lambda function: map (\x-foo x 5) my_list_of_lists_of_doubles of course, it is not needed for such trivial example, really i also prefer `foo` for this case -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Newbie syntax question
Hi, I have been using Haskell for a few weeks now and I can say that I am totally impressed by how easy it is to program with it. I am now doing some small exercises to get used to the language syntax and I have a little (newbie) question: Suppose I declare a function foo like: foo :: [Double] - Int - Int foo a b Suppose now that I want to apply this function to a list that contains lists of doubles (something like [[Double]]) using map, but I want to keep the 'b' parameter fixed (with a value of 5 for instance). Is it possible to use map passing the function foo with the 2nd argument only ? In other words, if I wrote this function like: foo :: Int - [Double] - Int I could clearly call it with: map (foo 5) my_list_of_lists_of_doubles But how to do that (if possible) when I invert the parameters list ?! Thanks in advance, Andre Abs da Cruz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie syntax question
That's exactly what i was looking for. Thank you very much !!! Cheers, André ChrisK wrote: There is the flip function which changes the order of the first 2 parameters Prelude :t flip flip :: forall c a b. (a - b - c) - b - a - c So I think map ( (flip foo) 5 ) my_list_of_lists_of_doubles will work, as will using a lambda expression map (\x - foo x 5) may_list_of_lists_of_doubles André Vargas Abs da Cruz wrote: Hi, I have been using Haskell for a few weeks now and I can say that I am totally impressed by how easy it is to program with it. I am now doing some small exercises to get used to the language syntax and I have a little (newbie) question: Suppose I declare a function foo like: foo :: [Double] - Int - Int foo a b Suppose now that I want to apply this function to a list that contains lists of doubles (something like [[Double]]) using map, but I want to keep the 'b' parameter fixed (with a value of 5 for instance). Is it possible to use map passing the function foo with the 2nd argument only ? In other words, if I wrote this function like: foo :: Int - [Double] - Int I could clearly call it with: map (foo 5) my_list_of_lists_of_doubles But how to do that (if possible) when I invert the parameters list ?! Thanks in advance, Andre Abs da Cruz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- --- André Vargas Abs da Cruz Laboratório de Inteligência Computacional Aplicada Departamento de Engenharia Elétrica Pontifícia Universidade Católica - Rio de Janeiro http://www.ica.ele.puc-rio.br/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie syntax question
So I think map ( (flip foo) 5 ) my_list_of_lists_of_doubles will work, as will using a lambda expression map (\x - foo x 5) may_list_of_lists_of_doubles I really like the `foo` syntax. map (`foo` 5) my_list also works, without an auxiliary function and without a lambda. In reality this gets de-sugared to a flip, but i prefer this syntax. `foo` uses foo as an infix operator. Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
newbie syntax question
Hi, I am extremely new to Haskell. This will be my first question, so go easy. I have just read Chapter 1 on Simon Thompson's book. for example a function declaration is given as follows scale : : Picture - Int - Picture If the first two types are input variables why does the syntax require me to use arrows twice? I mean isn't the following syntax more readable (hypothetically)? scale : : Picture , Int - Picture Is there a specific reason not to be able to distinguish the input parameters from the output parameter? Thanks
RE: newbie syntax question
Actually, all functions in Haskell take only one argument, although the people writing the program usually don't think of it this way. scale could alternatively have been defined with type scale :: (Picture, Int) - Picture which looks more like what we would expect in a function of two arguments. But even here there is actually only a single argument, which happens to be a pair of values. The given declaration scale :: Picture - Int - Picture is written in 'curried' form. This means that scale is a function of oneargument, 'Picture', and that its return value is a new function with type Int - Picture. That new function can immediately be applied to an int value so that it appears as if you called scale with two values instead of just one. instead of writing scale pic 3 for example, you could have written (scale pic) 3 or even let x = scale pic in x 3 Which might make it a little easier to see what is actually happening. Hope this helps, -- Gary -Original Message-From: Cagdas Ozgenc [mailto:[EMAIL PROTECTED]]Sent: Tuesday, July 24, 2001 1:07 PMTo: [EMAIL PROTECTED]Subject: newbie syntax question Hi, I am extremely new to Haskell. This will be my first question, so go easy. I have just read Chapter 1 on Simon Thompson's book. for example a function declaration is given as follows scale : : Picture - Int - Picture If the first two types are input variables why does the syntax require me to use arrows twice? I mean isn't the following syntax more readable (hypothetically)? scale : : Picture , Int - Picture Is there a specific reason not to be able to distinguish the input parameters from the output parameter? Thanks
Re: newbie syntax question
At 3:07 PM -0500 7/24/01, Cagdas Ozgenc wrote: Hi, I am extremely new to Haskell. This will be my first question, so go easy. I have just read Chapter 1 on Simon Thompson's book. for example a function declaration is given as follows scale : : Picture - Int - Picture If the first two types are input variables why does the syntax require me to use arrows twice? I mean isn't the following syntax more readable (hypothetically)? scale : : Picture , Int - Picture Is there a specific reason not to be able to distinguish the input parameters from the output parameter? Thanks Welcome to Haskell! May you find it as rewarding as I have. As you read further, you'll find that Haskell allows functions to be applied to less than their full complement of arguments. For example, if pict :: Picture, then scale pict is a function which, applied to an Int, produces a Picture. That is, scale pict :: Int - Picture This feature, known as Currying, is very useful for making rather general functions which can be specialized by supplying some of their arguments. The type notation has been designed to accommodate Currying in two ways: 0. each parameter type is followed by an arrow 1. the arrow has been designed to be right-associative. The arrow's right-associativity means that these two types are identical: scale : : Picture - Int - Picture scale : : Picture - (Int - Picture) That is, you are free to think of scale as either of a function of two arguments which returns a Picture a function of one argument which returns (a function of one argument which returns a Picture) That equivalence would be a bit less clear using the comma syntax you suggest. Cheers, --Ham -- Hamilton Richards, PhD Department of Computer Sciences Senior Lecturer Mail Code C0500 512-471-9525 The University of Texas at Austin Taylor Hall 5.138Austin, Texas 78712-1188 [EMAIL PROTECTED][EMAIL PROTECTED] -- ___ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell
Re: newbie syntax question
this should be on a list of the 10 first questions someone will ask when learning haskell. I have introduced several friends to the language and they all seem to ask the same things, if not always in the same order.. whats the deal with Int - Int - Int ... (currying) what does $ mean? is it 'special' syntax? (no, just a $ b = a b) why can't a - getChar be a = getChar in a do expression? how do I convert an IO a to just an a? (unsafePerformIO, but that is the wrong answer to the wrong question) general confusion about namespaces, the difference between (a,b) the type and (a,b) the value for instance. how to write some basic idioms only used in an imperitive setting, incrementing a counter, processing input, maintaining state with variables which don't directly translate to haskell usually leading to some confusion when you try to explain why they need to restructure their program. and on a more abstract level, the view of 'do' as a hack for IO rather than monads as an independant and useful abstraction. (i find the Maybe monad one of the better ways to introduce people to non-IO monads, especially if they are used to Maybe in non-monadic usage.) I have some email exchanges somewhere where I explained some of these concepts, perhaps I will edit them and add them to the Wiki, or better yet, find the best explanations from the list and add them... and of course any other newbie questions other people find they encounter... this is somewhat odd ground, because when helping someone learn the language, I WANT people to come to these questions on their own and ask them since it means they are thinking and noticing the places where they need to readjust, so its maybe not a newbies FAQ, for someone evaluating whether to learn the language, but more of a two weeks in and not quite getting everything FAQ. John -- --- John Meacham - California Institute of Technology, Alum. - [EMAIL PROTECTED] --- ___ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell