How does one check against a List containing a certain type? For instance,
I only want to map all elements of a string through the toString function
if that list does not already contain strings. The end result would be a
string resulting from a join.
module Main exposing (..)
import List
import String
anyListToString : List a -> String
anyListToString list =
let
toStringList =
case list of
List String ->
list
_ ->
List.map toString list
in
toStringList |> String.join ""
And these are the errors I get:
⎈ elm-make Example.elm
-- NAMING ERROR ----------------------------------------------------
Example.elm
Cannot find pattern `List`
12| List String ->
^^^^^^^^^^^
Maybe you want one of the following?
Just
Maybe.Just
-- NAMING ERROR ----------------------------------------------------
Example.elm
Cannot find pattern `String`
12| List String ->
^^^^^^
Detected errors in 1 module.
On Friday, June 3, 2016 at 6:00:12 PM UTC-4, Joey Eremondi wrote:
>
> Or are String and Int on "type MyType = String | Int" just tags?
>
>
> Yes, exactly!
>
> On Fri, Jun 3, 2016 at 2:53 PM, Lambder <[email protected]
> <javascript:>> wrote:
>
>> Or are String and Int on "type MyType = String | Int" just tags?
>>
>>
>> On Friday, 3 June 2016 22:50:34 UTC+1, Lambder wrote:
>>>
>>> I still like to ask for some clarification.
>>>
>>> type MyType = String | Int
>>>
>>> is legal type definition in elm. How to define function which accept one
>>> parameter of type MyType and if it is String return it with 'x' appended
>>> as a result. If it is a number return its increment?
>>> If what I am asking makes no sense, what are the potential usages of non
>>> tagged union types?
>>>
>>> --
>>> Thanks
>>>
>>> On Friday, 3 June 2016 22:25:59 UTC+1, Joey Eremondi wrote:
>>>>
>>>> The problem here is that you're confusing Types (which you can never
>>>> pattern match on) with Value Constructors, which are the only things you
>>>> can pattern match on.
>>>>
>>>> When you declare a "type", you need to give a distinct tag name to each
>>>> variant.
>>>>
>>>> Each "type" declaration has the form
>>>> type Name typeArg1 ... typeArgn = Variant1 | ... | Variant_n
>>>> where each variant has the form
>>>> TagName arg1 ... arg_n
>>>>
>>>> So when you write
>>>> type PageDef a m = ModelAndView a m | Html a
>>>> You're saying:
>>>> PageDef is a type constructor that takes two type arguments, a and m.
>>>> It has to variants: the first has a tag named ModelAndView, which takes
>>>> two
>>>> arguments, an "a" and an "m". The second is a type constructor named Html,
>>>> which takes a single argument of type "a".
>>>>
>>>> What you probably want is something like this:
>>>> type PageDef a m = TagName1 (MyModelAndView a m) | TagName2 (Html a)
>>>>
>>>> I don't know what your program does, so I don't know what appropriate
>>>> tag names would be.
>>>>
>>>> Then, to pattern match, you do:
>>>> viewChild : PageDef a m -> Html a
>>>> viewChild pageDef =
>>>> case pageDef of
>>>> TagName1 (m,v) ->
>>>> v m
>>>> TagName2 h -> h
>>>>
>>>> Elm only supports Tagged unions: when something can be an A or a B, you
>>>> need a tag to indicate which it is.
>>>>
>>>> Also remember that Types and Value Constructors (tags) exist in totally
>>>> separate namespaces! So in your original definition, you had defined two
>>>> things, both named MyModelAndView. One was a type alias for (m, View a
>>>> m) and the other was a type constructor for the type PageDef.
>>>>
>>>> The names of types will only occur in type signatures, never in the
>>>> bodies of functions. Value constructors can be used in functions, or on
>>>> the
>>>> left-hand side of a pattern match, and will never occur in type signatures.
>>>>
>>>> It's generally bad practice to share names between these two
>>>> namespaces, unless you have a type with a single consructor. For example:
>>>> type NumPixels = NumPixels Int
>>>> Defines a new type that is just a shallow wrapper around an Int, that
>>>> creates a new type storing an Int. But again, there's no magic here, the
>>>> type name NumPixels is completely separate from the tag name NumPixels.
>>>>
>>>> On Fri, Jun 3, 2016 at 2:12 PM, Daniel Kwiecinski <
>>>> [email protected]> wrote:
>>>>
>>>>> Hi Elmers,
>>>>>
>>>>>
>>>>> How do I pattern match on aliased types? E.g lets assume I have the
>>>>> following type definitions:
>>>>>
>>>>> type alias View a m = m -> Html a -- view is a function
>>>>> which turns a model (m) into html (Html a)
>>>>> type alias ModelAndView a m = (m, View a m) -- a pair of a model
>>>>> and a view
>>>>> type PageDef a m = ModelAndView a m | Html a -- algebraic type for
>>>>> unrealised html (model+view) and realised html
>>>>>
>>>>>
>>>>> how do I pattern match on PageDef ?
>>>>>
>>>>>
>>>>> viewChild : PageDef a m -> Html a
>>>>> viewChild pageDef =
>>>>> case pageDef of
>>>>> ModelAndView a m as m_v ->
>>>>> case m_v of
>>>>> (m, v) -> v m -- !!! GOT AN ERROR HERE !!!
>>>>> Html a as h -> h
>>>>>
>>>>> Is it even possible to pattern match directly?
>>>>>
>>>>> Regards,
>>>>> Daniel
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Elm Discuss" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>> --
>> You received this message because you are subscribed to the Google Groups
>> "Elm Discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.