HsklFx

download HsklFx

If you can't read please download the document

description

Haskell Functions

Transcript of HsklFx

Num Fx odd a : checks if number is odd compare a b : LT if a < b ; EQ if a =b; GT if a > b signum Int : Returns sign of Input iterate f x : Produces an infinite list which applies f an increasing number of times to x repeat _ :Creates an infinite list of the argumentList Fx head [_] : returns first element in list [a] -> a tail [_] : returns all but the first element in list [a] -> [a] last [_] : returns the last element of a list [a] -> a reverse [_] :returns a reversed list[a] -> [a] init [_] : returns all but the last element of a list [a] -> a take Num [_] : take returns the first n elements in list Int -> [a] -> [a] drop Num [_] : returns all but the first n elements in list Int -> [a] -> [a] splitAt Int [_] : returns a pair of input lists, split at given index Int -> [a] -> ([a], [a]) takeWhile predicate [_] : returns elements from beginning, stops when False is returned (a -> Bool) -> [a] -> [a] break predicate [_] : returns a pair of input lists, split where predicate succeeds Imagine that break is the filter for failures, only failures pass (a -> Bool) -> [a] -> ([a], [a]) dropWhile predicate [_] : drops elements from beginning, stops when False is returned (a -> Bool) -> [a] -> [a] span predicate [_] : returns a pair of input lists, split where predicate fails Imagine that span is a metal detector scanning people, if false is returned the flow is stopped. (a -> Bool) -> [a] -> ([a], [a]) replicate Int _ : returns a fixed length repeating list of its argumentInt -> a -> [a] null [_] : Returns Bool depending on whether list is empty [a] -> Bool reverse [_] : Returns elements in a list in reverse order [a] -> [a] [_] ++ [_] : Concatenate two lists [a] -> [a] -> [a] concat [[_]] : Concatenates a list of lists of the same type into one list, or removes one layer of nest [[a]] -> [a] a : [_] : add element a to list. This order must be kept sum [Num] : Returns the summation of all numbers in list Num a => [a] -> a lines "String" : Splits up a text string on line boundaries like \n \r String -> [String] unlines [String] : Concatenates a list of strings, adding a newline to the end of each [String] -> String words "String" : split an input string on any whitespace String -> [String] unwords [String] -> joins a list of words, using a single space to seperate them [String] -> String elem _ [_] : Returns if a value is present in the given list. Eq a => a ->[a]-> Bool notElem _ [_] : returns if a value is not present in given list Eq a => a -> [a] -> Bool filter predicate [_] : returns every element in given list where element succeeds (a -> Bool) -> [a] -> [a] length [_] : returns the length of an input list [a] -> Int and [Bool] : && function over a list of boolean values [Bool] -> Bool or [Bool] : || function over a list of boolean values [Bool] -> Bool all predicate [_] : returns True if predicate returns True on every element of list (a -> Bool) -> [a] -> Bool any predicate [_] : returns True if predicate returns True on any element of list (a -> Bool) -> [a] -> Bool zip [_] [_] : `zips` up two lists in pairs. The result list is the length of the shorter list To zip more than 2 lists, call zip3 ,zip4, etc [a] -> [b] -> [(a,b)] zipWith function [_] [_] : applies function to each zip pair, generating a single list To zipWith more than 2 lists, call zipWith3 , zipWith4 etc (a -> b -> c) -> [a] -> [b] -> [c] map function [_] : Applies a function to each element of list and returns the resultant list (a -> b) -> [a] -> [b] filter predicate [_] : Applies predicate to every element in list and return a list where True (a -> Bool) -> [a] -> [a] lookup key [(_,_),(_,_),..] : looks for a signature key in the first element of each pair, returns a Just value if found, otherwise NothingEq a => a -> [(a,b)] -> Maybe b isPrefixOf [_] [_] : Returns Bool depending on whether first list is prefix of other Eq a => [a] -> [a] -> Bool isSuffixOf [_] [_] : Returns Bool depending on whether first list is suffix of other Eq a => [a] -> [a] -> Bool isInfixOf [_] [_] : Returns Bool depending on whether first list matches anywhere Eq a => [a] -> [a] -> Bool tails [Char] : Returns all the tails of a list. So tails "banana" = "banana" "anana" "ana" "na" "a" "" [a] -> [[a]] delete target [_] : Deletes the first occurance of target in a list.Eq a => a -> [a] -> [a] elemIndex element [_]: Returns the index of a first target element foundEq a => a -> [a] - Maybe IntTuple Fx fst (_,_) : Returns first element in a pair tuple snd (_,_) : Returns second element in a pair tupleError Fx error "str" : aborts evaluation of expression and prints error msg