calling a variable length parameter lambda expression

Hi everyone. I have a problem. A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a) my problem is now i know i have a list filled with the parameters for the lambda expression. but how can i call that expression? [parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression is there a function wich can do smth like that? lambda _ex (unfold_parameters parameters) best regards

Short answer: that's impossible. Well, with some oleging it should be possible, but the very fact that you're trying to do something like this indicates that you're doing something wrong. Where did this list of parameters came from? May be, you can apply your function to them one at a time, as they arrive? On 5 May 2009, at 20:49, Nico Rolle wrote:
Hi everyone.
I have a problem. A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a)
my problem is now i know i have a list filled with the parameters for the lambda expression. but how can i call that expression? [parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression
is there a function wich can do smth like that?
lambda _ex (unfold_parameters parameters)
best regards _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

This is a Hard Problem in Haskell.
Let me ask you, how many parameters does this function take?
a = (\x -> x)
How many parameters does this function take?
b = (\f x -> f x)
How many parameters does this function take?
c = (\f x y -> f x y)
What if I call
a (+)?
-- ryan
On Tue, May 5, 2009 at 9:49 AM, Nico Rolle
Hi everyone.
I have a problem. A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a)
my problem is now i know i have a list filled with the parameters for the lambda expression. but how can i call that expression? [parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression
is there a function wich can do smth like that?
lambda _ex (unfold_parameters parameters)
best regards _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Nico Rolle
A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a)
And the type of that function is..?
my problem is now i know i have a list filled with the parameters for the lambda expression. but how can i call that expression?
[parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression
Would this work? data LambdaExpr a = L0 a | L1 (a -> a) | L2 (a -> a -> a) | ... apply :: LambdaExpr a -> [a] -> a apply (L0 x) _ = x apply (L1 f) (x:_) = f x apply (L2 f) (x1:x2:_) = f x1 s2 : -k -- If I haven't seen further, it is by standing in the footprints of giants

On 6 May 2009, at 4:49 am, Nico Rolle wrote:
Hi everyone.
I have a problem. A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a)
Your first function has type Ord a => a -> a -> Bool so your list of parameters must have type Ord a => [a] and length 2. Your second function -- and why do you have the excess parentheses? they make it harder to read -- has type Ord a => a -> a -> a -> Bool so your list of parameters must have type Ord a => [a] and length 3.
but how can i call that expression? [parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression
is there a function wich can do smth like that?
lambda _ex (unfold_parameters parameters)
but in both cases lambda_ex wants a single Ord, so unfold_parameters parameters would have to return just that Ord. You _could_ fake something up for this case using type classes, but the big question is WHY do you want to do this? It makes sense for Lisp or Scheme, but not very much sense for a typed language. Why are you building a list [x,y] or [x,y,z] rather than a function (\f -> f x y) or (\f -> f x y z) so that you can do parameters lambda_ex?

On Tue, May 5, 2009 at 8:49 PM, Nico Rolle
Hi everyone.
I have a problem. A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a)
my problem is now i know i have a list filled with the parameters for the lambda expression. but how can i call that expression? [parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression
is there a function wich can do smth like that?
lambda _ex (unfold_parameters parameters)
Why not: lam1 = \[x, y] -> x > y lam2 = \[x, y, z, a] -> (x > y) && (z < a) doLam :: Ord a => ([a] -> Bool) -> [a] -> Bool doLam lam params = lam params So, this will work fine: doLam lam1 [1, 2] doLam lam2 [1,2,3,4] -- Victor Nazarov

isn't doLam just id with an Ord restriction there?
On Wed, May 6, 2009 at 5:55 AM, Victor Nazarov
On Tue, May 5, 2009 at 8:49 PM, Nico Rolle
wrote: Hi everyone.
I have a problem. A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a)
my problem is now i know i have a list filled with the parameters for the lambda expression. but how can i call that expression? [parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression
is there a function wich can do smth like that?
lambda _ex (unfold_parameters parameters)
Why not:
lam1 = \[x, y] -> x > y lam2 = \[x, y, z, a] -> (x > y) && (z < a)
doLam :: Ord a => ([a] -> Bool) -> [a] -> Bool doLam lam params = lam params
So, this will work fine:
doLam lam1 [1, 2] doLam lam2 [1,2,3,4]
-- Victor Nazarov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

super nice.
best solution for me so far.
big thanks.
regards
2009/5/6 Victor Nazarov
On Tue, May 5, 2009 at 8:49 PM, Nico Rolle
wrote: Hi everyone.
I have a problem. A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a)
my problem is now i know i have a list filled with the parameters for the lambda expression. but how can i call that expression? [parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression
is there a function wich can do smth like that?
lambda _ex (unfold_parameters parameters)
Why not:
lam1 = \[x, y] -> x > y lam2 = \[x, y, z, a] -> (x > y) && (z < a)
doLam :: Ord a => ([a] -> Bool) -> [a] -> Bool doLam lam params = lam params
So, this will work fine:
doLam lam1 [1, 2] doLam lam2 [1,2,3,4]
-- Victor Nazarov

Keep in mind that using lists for your parameters means you lose
static guarantees that you've passed the correct number of arguments
to a function (so you could crash at runtime if you pass too few or
too many parameters to a function).
On Wed, May 6, 2009 at 11:41 AM, Nico Rolle
super nice. best solution for me so far. big thanks. regards
2009/5/6 Victor Nazarov
: On Tue, May 5, 2009 at 8:49 PM, Nico Rolle
wrote: Hi everyone.
I have a problem. A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a)
my problem is now i know i have a list filled with the parameters for the lambda expression. but how can i call that expression? [parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression
is there a function wich can do smth like that?
lambda _ex (unfold_parameters parameters)
Why not:
lam1 = \[x, y] -> x > y lam2 = \[x, y, z, a] -> (x > y) && (z < a)
doLam :: Ord a => ([a] -> Bool) -> [a] -> Bool doLam lam params = lam params
So, this will work fine:
doLam lam1 [1, 2] doLam lam2 [1,2,3,4]
-- Victor Nazarov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Wed, May 6, 2009 at 7:44 PM, Daniel Peebles
Keep in mind that using lists for your parameters means you lose static guarantees that you've passed the correct number of arguments to a function (so you could crash at runtime if you pass too few or too many parameters to a function).
Yes, program will crash with error "Pattern match failure" or smth like that. And yes, doLam is just an id with restricted type. My solution is close to what seems the most common way of passing arguments in Scheme, and the payoff is Scheme's dynamic typing... This solution is not the Haskell way and should be avoid. -- Victor Nazarov
On Wed, May 6, 2009 at 11:41 AM, Nico Rolle
wrote: super nice. best solution for me so far. big thanks. regards
2009/5/6 Victor Nazarov
: On Tue, May 5, 2009 at 8:49 PM, Nico Rolle
wrote: Hi everyone.
I have a problem. A function is recieving a lambda expression like this: (\ x y -> x > y) or like this (\ x y z a -> (x > y) && (z < a)
my problem is now i know i have a list filled with the parameters for the lambda expression. but how can i call that expression? [parameters] is my list of parameters for the lambda expression. lambda_ex is my lambda expression
is there a function wich can do smth like that?
lambda _ex (unfold_parameters parameters)
Why not:
lam1 = \[x, y] -> x > y lam2 = \[x, y, z, a] -> (x > y) && (z < a)
doLam :: Ord a => ([a] -> Bool) -> [a] -> Bool doLam lam params = lam params
So, this will work fine:
doLam lam1 [1, 2] doLam lam2 [1,2,3,4]
-- Victor Nazarov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (7)
-
Daniel Peebles
-
Ketil Malde
-
Miguel Mitrofanov
-
Nico Rolle
-
Richard O'Keefe
-
Ryan Ingram
-
Victor Nazarov