Note some obvious operations we would likely need to perform on numbers that are missing from the Num typeclass. The main numeric type we will use in this course is Int, i.e. This granularity allows the type system to guard against improper use of numbers that might result in loss in precision or division by zero. Obviously this is a much more finely grained set of types than JavaScript’s universal “ number” type. These are Word, Integer, Int, Float and Double. The last five lines (beginning with “ instance”) tell us which types have been declared as instances of Num and hence have definitions of the necessary functions. The last is an option because a default definition exists for each in terms of the other. The first line (beginning class) tells us that for a type to be an instance of the Num typeclass, it must provide the operators , * and the functions abs, signum and fromInteger, and either (-) or negate. GHCi > : i Num class Num a where ( ) :: a -> a -> a ( - ) :: a -> a -> a ( * ) :: a -> a -> a negate :: a -> a abs :: a -> a signum :: a -> a fromInteger :: Integer -> a - Defined in `GHC.Num' instance Num Word - Defined in `GHC.Num' instance Num Integer - Defined in `GHC.Num' instance Num Int - Defined in `GHC.Num' instance Num Float - Defined in `GHC.Float' instance Num Double - Defined in `GHC.Float' If the constructor takes no type parameters the kind is just *, (it returns a type), *->* if it takes one type parameter, *->*->* for two type parameters and so on.Īnother sort of “kind” are for type classes which we will introduce more properly in a moment.įor example, the “kind” for the Ord type class (the class of things that are Orderable and which we came across in our simple implementation of quicksort) is: Note that it is like the syntax for function types (with the ->), you can think of it as information about what is required in terms of type parameters to instantiate the type. The kind syntax indicates the arity or number of type parameters a type has. GHCi allows you to use the :kind (or :k) command to interrogate the Kind of types – think of it as “meta information” about types and their type parameters. with two type parameters such that the two elements may be different types.with one type parameter (by convention called a in Haskell, and T in TypeScript).The following visual summary shows pair data structures with accessor functions fst and sec defined using Record Syntax with varying degrees of type flexibility, and compared with the equivalent typescript generic notation: (\x -> x * x))) Ĭool! High-level optimizations thanks to high-level programming.Length :: -> Int - a is a type parameter length = 0 length ( _ : rest ) = 1 length rest a - > a (of which the only useful function I can see is id or a constant function which constrains the type of the 1st argument.) b -> a (which is basically a constant function. = map (\x -> x * x) ((map (\x -> x * x)) ) Now, with the arrow constructor we can build two new types of functions. Thus mapSquare (mapSquare ) => map (\x -> x * x) (map (\x -> x * x) ) Recall, function application is left associative f e1 e2 = (f e1) e2 g) mapSquare (mapSquare ) => map (\x -> x * x) (map (\x -> x * x) ) Map obeys some general laws: map f (map g) = map (f. Sample evaluations mapSquare => map (\x -> x * x) MapSquare :: -> mapSquare = map (\x -> x * x) We can also pattern match against Integer and Booleans. Replacing the left-hand side by the right-hand yields.Hence, the clause sumNumsPat = x1 x2 =.Function clauses are checked from top to bottom.Similar game, we check which function clauses matches.We replace the left-hand side by the right-hand side which yields.We find sumNumsPat (x:xs) = x sumNums xs where.We check which function clauses matches.Starting from top to bottom and left to right (in case there are several SumNumsPat ys = case ys of -> 0 (x :xs) -> x sumNums xs Evaluation with patternįunction calls are matched against their function equations but The result (see upcoming material on type classes). The interpreter applies the overloaded show method on In this chapter the focus is on polymorphic functions, which are most commonly defined over polymorphic data types. What things are showable? Int, Float, Bool, …, all primitive The byte code is executed and the result is Haskell doesnt need parentheses for function calls.all the arguments. Is turned into some byte code format (for faster Haskell was designed as a practical, purely functional programming language. Not as fast as if we would use the compiler)
0 Comments
Leave a Reply. |