1

I have one function first with type: Int -> [a] -> (Error ([a],[a])) and a second function second with type: [a] -> [a] -> [a]

I am trying to make a third function now that uses the above functions. the type I have for this function is: [Int] -> [a] -> Error [a]

I have been given these types to work around so cant change them.

This is what I tried:

last :: [Int] -> [a] -> Error [a]
last (x:xs) list = second (first x list)

Can you pass outputs from functions that use the error function in to others?

6
  • 4
    Function names must be lower case. Have you tried implementing this? Commented Mar 24, 2011 at 15:43
  • @Joel you can have functions with upper case names. They're also known as data constructors (ok, maybe function-like values?). E.g. Just :: a -> Maybe a. Commented Mar 24, 2011 at 15:44
  • 2
    I understand that data constructors are upper case, but F and Q are not data constructors. Commented Mar 24, 2011 at 15:47
  • i have changed the function names, that was just for the example, fixed. Commented Mar 24, 2011 at 17:52
  • What is Error here? The answer from Martinho seems to imply that it is a monad, is it so? Commented Mar 24, 2011 at 22:50

1 Answer 1

3

Assuming Error is an error monad, you can use the monadic bind operator (>>=) and the uncurry function:

z (x:xs) list = F x list >>= return . uncurry Q

uncurry transforms Q from a function with two arguments (aka a curried function) into a function on pairs. This means that uncurry Q :: ([a], [a]) -> [a]

The bind operator takes a value out of a monad and passes it into a monadic function. Here we're extracting the value of the Error monad returned by F and passing it to Q, now turned into a monadic function that works on a pair of lists, thanks to return and uncurry.

Sign up to request clarification or add additional context in comments.

3 Comments

You can also use the functor instance instead: z (x:_) list = uncurry Q <$> F x list
@FUZxxl Thanks! I knew there was a way to write fmap (uncurry Q) (F x list) that looked elegant without parenthesis. I just couldn't remember what the operator was. I guess my Haskell is getting rusty :(
@Marthino Fernandes: The problem is, that the Prelude doesn't exports <$> by default.

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.