# Extract Value From Script (Daml Syntax)

**URL:** <https://forum.canton.network/t/extract-value-from-script-daml-syntax/5841>\
**Category:** App Development\
**Tags:** daml, haskell\
**Created:** [December 20, 2022, 7:36am UTC](https://forum.canton.network/t/extract-value-from-script-daml-syntax/5841 "2022-12-20T07:36:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![VictorShneer](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/victorshneer/32/5413_2.png) [@VictorShneer](https://forum.canton.network/u/VictorShneer)\
**Post date:** [December 20, 2022, 7:36am UTC](https://forum.canton.network/t/extract-value-from-script-daml-syntax/5841/1 "2022-12-20T07:36:29Z")

</div>

Hi, Daml community!

I’m trying to write a helper function with a notation:

```haskell
extractFirstCidFromQueryResults: (Template t) => Script([(ContractId t, t)]) -> ContractId t

```

Usually, in the script context, I do it like this:

```haskell
 firstCidFromQuery <- fst . head <$> query @Template party

```

Now I want to write a helper function to hide this part:  
`fst . head <$>`

And I can’t figure out how to do this… How can I do \<$\> as a first action in the function… I tried \>\>=, but it looks like I’m missing a concept here. Help!

Thanks!

PS

And I tried to query inside a function:

```haskell
queryAndTakeFirstCid: (Template temp) => Party -> temp -> Script(ContractId temp)
queryAndTakeFirstCid party temp = query @temp party >>= \case
                                      -- ^Not in scope: type variable ‘temp’
  x:xs -> return $ fst x
  _ -> error "no cids" 

queryAndTakeFirstCid party MyTemplate

```

I will highly appreciate it if you can give me a clue on how to construct a function like this.

---

<div class="post-metadata">

**Author:** ![Matteo\_Limberto](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/matteo_limberto/32/2823_2.png) [@Matteo\_Limberto](https://forum.canton.network/u/Matteo_Limberto)\
**Post date:** [December 20, 2022, 9:05am UTC](https://forum.canton.network/t/extract-value-from-script-daml-syntax/5841/2 "2022-12-20T09:05:09Z")

</div>

Hi @VictorShneer,

In the expression you would write within a script

```haskell
firstCidFromQuery <- fst . head <$> query @Template party

```

you are using the `<-` operator to extract a value of type `x` from a `Script x`.

Hence, the expression to the right of `<-` must be wrapped in a `Script`. If you want to abstract this logic behind a helper function, the helper function should then return a `Script (ContractId t)` rather than just a `ContractId t`.

```haskell
extractFirstCidFromQueryResults: (Template t) => Script([(ContractId t, t)]) -> Script (ContractId t)

```

Additionally, I think the `Template t` type-class constraint is unnecessary.

On your second point, you don’t really need the `@temp` type specification (it is automatically inferred by the compiler) and can write

```haskell
queryAndTakeFirstCid: (Template t) => Party -> Script (ContractId t)
queryAndTakeFirstCid party = query party >>= \case
  x :: xs -> pure $ fst x
  [] -> error "no cids"

```

Please, note that I used `::` to pattern-match a non-empty list rather than `:`.

Matteo
