-
Notifications
You must be signed in to change notification settings - Fork 8
Type annotations
Problem 1
/ works with floats Float:
> (/)
<function> : Float -> Float -> Float... and so a function that feeds a parameter to it must make that parameter a Float:
-- f1 : number -> number
-- f1 x =
-- x / 3
f1 : Float -> Float
f1 x =
x / 3Elm is actually cheating a bit here. 3 is typed as number and Elm silently coerces it to be a Float. As noted in the text, number is a special case.
Problem 2
-- f2 : List a -> List b -> List b
-- f2 one two =
-- one ++ two
f2 : List a -> List a -> List a
f2 one two =
one ++ twoWere the type annotation correct, the function could take a List String and a List Int and add them together to produce a List Int. But (++) requires its arguments to have exactly the same List type. You can have a function that works with lists of different type, but if it concatenates them, it has to convert to a common type:
f2a : List a -> List String -> List String
f2a nonstrings strings =
List.map toString nonstrings ++ stringsProblem 3
Just an extra argument.
-- f3 : List a -> Int -> List a
-- f3 list = List.reverse list
f3 : List a -> List a
f3 list = List.reverse listProblem 4
This shows what happens if you accidentally leave an parameter off a function application: reverse expects an argument, but I forgot to give it.
-- f4: List a -> List a
-- f4 list = List.reverse
f4: List a -> List a
f4 list = List.reverse listIn this particular case, we could just forget about the argument and use point-free style:
f4a: List a -> List a
f4a = List.reverseProblem 5
For whatever reason, I want to name a function just like map, but with the arguments flipped. But I forgot the parentheses around the type of the function parameter.
-- f5: List a -> a -> b -> List b
-- f5 = flip List.map
f5: List a -> (a -> b) -> List b
f5 = flip List.map