# Parse String to Function

**URL:** <https://discourse.julialang.org/t/parse-string-to-function/89668>\
**Category:** General Usage\
**Created:** [November 2, 2022, 5:14pm UTC](https://discourse.julialang.org/t/parse-string-to-function/89668 "2022-11-02T17:14:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ricardo\_Lima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardo_lima/32/43312_2.png) [@Ricardo\_Lima](https://discourse.julialang.org/u/Ricardo_Lima)\
**Post date:** [November 2, 2022, 5:14pm UTC](https://discourse.julialang.org/t/parse-string-to-function/89668/1 "2022-11-02T17:14:48Z")

</div>

How to make this code work?

```julia
#pkg> add InverseLaplace

using InverseLaplace
import InverseLaplace: talbot

f2(s) = s / (s^2 + 1);
ft2 = Talbot(f2, 80);
ft2(pi / 2) #corret

f1(s) = Symbol("s / (s^2 + 1)")
ft1 = Talbot(f1, 80);
ft1(pi / 2) #error MethodError: no method matching *(::Float64, ::Symbol)

```

---

<div class="post-metadata">

**Author:** ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)\
**Post date:** [November 2, 2022, 5:30pm UTC](https://discourse.julialang.org/t/parse-string-to-function/89668/2 "2022-11-02T17:30:19Z")

</div>

You can use this:

```julia
using InverseLaplace
import InverseLaplace: talbot

f1 = eval(Meta.parse("f1(s) = s / (s^2 + 1)")) 
ft1 = Talbot(f1, 80);
ft1(pi / 2) 

```

To add a little bit more info. `Meta.parse` will “parse” the string to an expression. Then `eval` will evaluate the expression, basically “checking the value of the expression” (maybe [this](https://subscription.packtpub.com/book/application-development/9781785882012/2/ch02lvl1sec21/the-eval-function#:~:text=The%20eval()%20function%20is,done%20in%20a%20global%20scope.) makes it more clear). I moved the `f1(s)` within the string as it should be part of the expression.

There is a discussion [here](https://discourse.julialang.org/t/performance-of-meta-parse-and-eval/52061) about whether you should use expression parsing.
