1

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!

5
  • 4
    Are 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]? Commented Feb 22, 2012 at 15:50
  • 1
    For clarification: besides the list of var, you also have a csp and an int, and you want a list of csps, is this correct? Commented Feb 22, 2012 at 15:50
  • 1
    There 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.? Commented Feb 22, 2012 at 15:51
  • 1
    i 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] Commented Feb 22, 2012 at 15:52
  • 1
    An example wouldn't hurt Commented Feb 22, 2012 at 15:55

6 Answers 6

7

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)
Sign up to request clarification or add additional context in comments.

Comments

2

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

1

http://zvon.org/other/haskell/Outputprelude/map_f.html

This might be what you're looking for, no?

2 Comments

ive tried using map, but i get this error couldn't match expected type [CSP] with inferred type CSP
OP wants to map a value onto the second argument of a three-argument function. This needs more explanation.
1

You want a fold, or so it sounds to me. Suppose the function you have is f

f :: Csp -> Var -> Int -> Csp
vars = [ ..... ] :: [Var]
i :: Int -- the constant int argument to f

foldl g vars where g c v = f c v i

Comments

1

I think you need a fold.

answer :: (Csp -> Var -> Int -> Csp) -> Csp -> [Var] -> Int -> Csp
answer f csp vs i = foldl (\csp' v -> f csp' v i) csp vs

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.