# Elm parser tried first time - results not as expected

**URL:** <https://discourse.elm-lang.org/t/elm-parser-tried-first-time-results-not-as-expected/9313>\
**Category:** Learn\
**Created:** [September 9, 2023, 11:42am UTC](https://discourse.elm-lang.org/t/elm-parser-tried-first-time-results-not-as-expected/9313 "2023-09-09T11:42:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![polarit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/polarit/32/4651_2.png) [@polarit](https://discourse.elm-lang.org/u/polarit)\
**Post date:** [September 9, 2023, 11:42am UTC](https://discourse.elm-lang.org/t/elm-parser-tried-first-time-results-not-as-expected/9313/1 "2023-09-09T11:42:23Z")

</div>

I aimed to create a [parser](https://ellie-app.com/nRYM8F3rTnsa1) getting the point (x,y) from the input string " (1.25, 2.50) "

The parsing result looks correct: Ok { x = 1.25, y = 2.5 }  
However, this is not the record { x = 1.25, y = 2.5 } of alias type Point, I wanted.  
Thus, I can’t get the co-ordinates x, y simply from the point record with parsedResult.x and parsedResult.y.

Further, I don’t understand, why the parsing result is of type Result (List Parser.DeadEnd) Point .  
Why not just type _Point_ ? What does actually _Parser.DeadEnd_ mean? Is there something missing in my code?

Before I tried parser, I planned to use some usual string handling to get the the geo-location co-ordinates latitude and longitude for the given string “lat degree, lat minute, lon degree lon minute”.  
So I made a [rough code](https://ellie-app.com/nS38CvDT83La1) for that. It’s not ready for any real use. It should make more sense as a separate module.  
Then I imagined, it should be better to do with a Parser library with error handling etc.

---

<div class="post-metadata">

**Author:** ![wondible](https://avatars.discourse-cdn.com/v4/letter/w/e9c0ed/32.png) [@wondible](https://discourse.elm-lang.org/u/wondible)\
**Post date:** [September 9, 2023, 12:16pm UTC](https://discourse.elm-lang.org/t/elm-parser-tried-first-time-results-not-as-expected/9313/2 "2023-09-09T12:16:11Z")

</div>

Point is a type _alias_. As far as the compiler is concerned, any record with that shape is interchangeable and anonymous. You’ve correctly used the Point constructor in your parser, but it doesn’t carry the name with it, just creates a record of that form. You could make a stronger type, but it won’t be just point.x to access fields.

The Result type is so you can report errors, for the base parser you can use `Parser.deadEndsToString` for debugging, but it’s not great for user visible errors. Like any Result, you can either case off of Ok/Err or use something like `Result.withDefault (Point 0 0)` once you’ve handled errors.

---

<div class="post-metadata">

**Author:** ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)\
**Post date:** [September 9, 2023, 12:23pm UTC](https://discourse.elm-lang.org/t/elm-parser-tried-first-time-results-not-as-expected/9313/3 "2023-09-09T12:23:05Z")

</div>

Your Ellie has a certain beauty to it already, and passing in an invalid string to your parser might show you why.

A parser returns a result because it can fail. Combine this with your “Expected” and you can produce beautiful error messages explaining why the parsing failed and maybe even ways to fix the input.

---

<div class="post-metadata">

**Author:** ![polarit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/polarit/32/4651_2.png) [@polarit](https://discourse.elm-lang.org/u/polarit)\
**Post date:** [September 10, 2023, 10:47pm UTC](https://discourse.elm-lang.org/t/elm-parser-tried-first-time-results-not-as-expected/9313/4 "2023-09-10T22:47:56Z")

</div>

Thanks for helping, I made a [new version](https://ellie-app.com/nSWH6vg8qgWa1) using **Result.withDefault** and **Ok** /Err .

I made yet an [extended version](https://ellie-app.com/nTGyb8bd6pra1). It can use the notations N, S (North, South) for latitudes and E, W (East, West) for longitudes.

---

<div class="post-metadata">

**Author:** ![polarit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/polarit/32/4651_2.png) [@polarit](https://discourse.elm-lang.org/u/polarit)\
**Post date:** [September 10, 2023, 11:03pm UTC](https://discourse.elm-lang.org/t/elm-parser-tried-first-time-results-not-as-expected/9313/5 "2023-09-10T23:03:35Z")

</div>

Thanks for helping! I agree, testing with different invalid strings gives good hints for necessary error handling. I see my latest version has it very little yet.

---

<div class="post-metadata">

**Author:** ![kanishka](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@kanishka](https://discourse.elm-lang.org/u/kanishka)\
**Post date:** [September 11, 2023, 8:01pm UTC](https://discourse.elm-lang.org/t/elm-parser-tried-first-time-results-not-as-expected/9313/6 "2023-09-11T20:01:14Z")

</div>

After reading this, I looked a little deeper into why it returns a List of DeadEnd instead of a single DeadEnd. I skimmed the implementation of `oneOf`, and I believe it’s a list because it’s possible to use `oneOf` along with the backtracking enabling functionality to allow the parsing to reach multiple dead ends.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [September 21, 2023, 8:01pm UTC](https://discourse.elm-lang.org/t/elm-parser-tried-first-time-results-not-as-expected/9313/7 "2023-09-21T20:01:53Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
