I have a function of type Csp -> Var -> Int -> Csp, and I want to apply this function to a list of [Var], just wondering if anyone can help me out I'm totally bamboozled!
-
4Are the int/csp arguments supposed to be fixed, do you want to use the csp argument to iterate on or do you want the result to be a list of type [csp -> int -> csp]?Tilo Wiklund– Tilo Wiklund2012-02-22 15:50:28 +00:00Commented Feb 22, 2012 at 15:50
-
1For clarification: besides the list of var, you also have a csp and an int, and you want a list of csps, is this correct?Boris– Boris2012-02-22 15:50:30 +00:00Commented Feb 22, 2012 at 15:50
-
1There isn't much to go on here. Could you be more specific and add some code showing what you've tried, what results you're expecting, etc.?hammar– hammar2012-02-22 15:51:08 +00:00Commented Feb 22, 2012 at 15:51
-
1i want the result to be a csp, i want to apply the function to all the the items in the list of [var] using the csp and the int, the int should stay the same but the csp should change each time the function is used on somthing in [var]user1226239– user12262392012-02-22 15:52:44 +00:00Commented Feb 22, 2012 at 15:52
-
1An example wouldn't hurtARRG– ARRG2012-02-22 15:55:08 +00:00Commented Feb 22, 2012 at 15:55
6 Answers
From your comment, it sounds like you want a fold, for example:
foo :: Csp -> Var -> Int -> Csp -- your function
bar :: Csp -> [Var] -> Int -> Csp
bar c0 vs x = foldl (\c v -> foo c v x) c0 vs
Though it might be worth changing the order of the arguments a little to make it more suited for partial application:
foo :: Int -> Csp -> Var -> Csp
bar :: Int -> Csp -> [Var] -> Csp
bar x = foldl (foo x)
Comments
What you want is a fold. foldl has the signature foldl :: (a -> b -> a) -> a -> [b] -> a, so in your case you want a to be Csp h and b to be Var, giving you the type foldl :: (Csp -> Var -> Csp) -> Csp -> [Var] -> Csp. For the first argument you just pass it something like \csp var -> f csp var yourFixedInt where f is your function.
If you aren't familiar with folds what this does is apply, for each var in your list of Vars, the function you pass it (in this case just your function with the Int argument fixed) to a Csp accumulator and var.
(There are a lot better explanations of folds around, but I figured I'd include at least a short comment)
Comments
http://zvon.org/other/haskell/Outputprelude/map_f.html
This might be what you're looking for, no?
2 Comments
map takes a function and applies it to a list of values.
So as a simple example, if I have a function f x = x + 5, and a list l = [1,2,3], then map f l will return [6,7,8]
Given f :: Csp -> Var -> Int -> Csp, and l :: [Var], you should write
map (\x -> f csp x int) l -- note that the variable names (like "csp") can be anything
which will have the type :: Csp -> Int -> Csp. In other words, it will return a list of functions.