60: Getting Haskell Unit 2 Types
Javascript to Elm
English - November 29, 2018 10:00 - 27 minutes - 23.2 MB - ★★★★★ - 4 ratingsTechnology elm javascript programming Homepage Download Apple Podcasts Google Podcasts Overcast Castro Pocket Casts RSS feed
Previous Episode: 59: Getting Haskell Unit 1 (part 2)
Next Episode: 61: Getting Haskell Unit 2 (TypeClasses)
We're moving pass primitives and diving into types, types, types. I've really struggle with this in previous Haskell attempts, this is usually the part where I get frustrated and bail, but not this time!!!
Into to Types
Basic types
this is where we get the : : and then the variable definition, we tried assignment, something to differentiate it from the typical variable assignment we do in JS starts with a capital letter Tuple typestype inference
i think this is under appreciated, but so far in 2 weeks we have been writing Haskell without type signatures. relaying on the compilers ability to infer those typesfunction types
the -> is used to separate arguments and return valuePolymorphic
Haskell does not DO implicit type conversion, or what we affectionately call coercion in JS “their type is determined from the compiler based on the way they’re used” Simple example is 5/2 you don’t need to put 5.0 / 2.0 to get a double back of 2.5 show and readfunctions with multiple arguments
partial application, remember lazy evaluation, more on that later I see nested function calls and I just want to shout ‘pipe operator!!!!’ i think it’s called bind in Haskelltypes for first class functions
remember functions as arguments bc they are first class! ifEven :: (Int -> Int) -> Int -> Inttype variables
this is where we start to get into the weeds, and maybe it’s all in my head simple :: a -> a with a definition of simple x = x the a here is literally a variable for any kind of type, which one? doesn’t matter. You want to know, I want to know. But this is important, it doesn’t matter. What does matter is that whatever type it is that you pass as an argument to simple, you are guaranteed that you will get the same type back, in this case a. Give it a Int, you get an Int back… it represents a type, rather than a value now this has tripped me up f1 : : a -> a f2 : : a -> b We went over the first function, it is restricted to returned the same type as the argument. The value can be totally different, and will likely be, unless we’re talking about the identity method. Here’s the curveball. a -> b represents a much broader behavior, this function can take type a and return not only a different value, but a different type!! Here’s the part I’ve missed before, BUT IT DOESN’T HAVE TO f2 can take a Int -> String, but it can also Int -> Int !! That’s totally ok its these little details that make a big difference! creating types type synonyms, in elm we call them type alias’ type keyword data as a key work for creating a new type. capitalize the type constructorthe data constructor is used to create a concrete instance of the type
it can also take arguments!!! like we do in Custom Types, We don’t refer to them as Union Types anymore? record syntax unordered key/value pair key :: type type classes group types based on shared behavior like Interfaces, but not I have googled many times ‘fat arrow in Haskell’ with little to know success so before the arguments denoted by the -> we might see this ( + ) : : Num a => a -> a -> a this is telling us that sum (+) is a part of the Num type class ? so what does that mean? if we :info Num then we get a list of type definitions, and can see that to be a Num type class you must return the same type as the argument passed in. “type classes are a constraint on the categories of types that a type variable can represent” Common type classes are Ord, Eq, Bounded, and Show let’s pick up there with declaring type classes, using type classes and deriving type classes, which I have not even heard up till nowand there’s a super cool type class roadmap we’ll look through next week!
Resources Get Programming With Haskell FollowJavaScript to Elm
Twitter: @jstoelmEmail: [email protected]
Jesse Tomchak
Twitter: @jtomchak