1

In C, reading the data in a string and putting its data into a struct is fairly straight forward, you just use something along the lines of sscanf to parse your data:

struct ingredient_dose ingr;

char *current_amount = "5 tsp sugar";

sscanf(current_amount, "%d %s %s", &ingr.amount, ingr.unit, ingr.ingredient);

Which would fill the struct/record with the given data.

How would I do something similar in Haskell? I realise you can't mutate anything once it's made, so the procedure will obviously be a bit different from the C example, but I can't seem to find a decent guide that doesn't include parsing JSON.

1
  • Derive the Read class. If the string is given in Haskell syntax (eg. Ingr 5 "tsp" "sugar"), you can have GHC do it for you. Commented Dec 5, 2014 at 16:17

1 Answer 1

4

Haskell doesn't have any built-in function which does exactly what scanf does.

However, if your stuff is space-delimited, you could easily use words to split the string into chunks, and the read function to convert each substring into an Int or whatever.

parse :: String -> (Int, String, String)
parse txt =
  let [txt1, txt2, txt2] = words txt
  in  (read txt1, txt2, txt3)

What Haskell does have an abundance of is "real" parser construction libraries, for parsing complex stuff like... well... JSON. (Or indeed any other computer language.) So if your input is more complicated than this, learning a parser library is typically the way to go.


Edit: If you have something like

data IngredientDose = IngredientDose {amount :: Double, unit, ingredient :: String}

then you can do

parse :: String -> IngredientDose
parse txt =
  let [txt1, txt2, txt2] = words txt
  in  IngredientDose {amount = read txt1, unit = txt2, ingredient = txt3}
Sign up to request clarification or add additional context in comments.

1 Comment

Added record example.

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.