0

I'm new to haskell and have no idea how to parse.

I have created a new type

type Field = String

Now I want parse [String] to [Field], how could I do this? I already have this:

parse :: [String] -> [Field]
1
  • 1
    But here you do not parse: a Field is a String: you have defined a type alias. So that means that [String] is [Field]. You do not have to do any processing. Commented Sep 17, 2017 at 14:25

1 Answer 1

4

I have created a new type

No you have not. If you write type Foo = ... you define a type alias: there is no new type, you only have given a certain (composite) type a new name. From now on, you can use Field and String interleaved. You define new types with data Foo = ... or newtype Foo = .... Usually type Foo = ... is used to give a more complex type a specific name. Like for instance type NamedStringCollection = (String,[String]).

Since String and Field are the same type, so are [String] and [Field]. As a result you do not need to do any processing. If you have a [String], this is at the same time a [Field].

You can write:

parse :: [String] -> [Field]
parse = id  -- not necessary

But the id simply returns what you give it. So id does not do any work.

Sign up to request clarification or add additional context in comments.

Comments

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.