user438034
user438034

Reputation:

FParsec: Parameterizing `parray n p` with elements of a list

The problem is this: I know I need to parse n occurrences of p, but I also have a list xs from which I need to pass an element to p on each application. Passing one value to p is easy enough -- parray n (p xs) -- but then I don't know which element in the list I need to access.

What would be the cleanest way to achieve this?

Upvotes: 2

Views: 68

Answers (1)

user438034
user438034

Reputation:

The solution I arrived at was:

let rec pmap f xs =
    match xs with
    | x :: xs -> parse { let! y = f x
                         let! ys = pmap f xs
                         return y :: ys }
    | [] -> parse { return [] }

This can then be used as:

let p x = ...
pmap p xs

Would still like to hear other ideas!

Upvotes: 1

Related Questions