# HG changeset patch # User IBBoard # Date 1560629247 -3600 # Node ID 7e4afb129bef4a30d63c11a893659f5655171396 # Parent d6855f9d7eae759b225e2d32adf57636516fc06c Add initial Day 2 notes with functions, partially applied, and currying diff -r d6855f9d7eae -r 7e4afb129bef 7-Haskell/README.txt --- a/7-Haskell/README.txt Sat Jun 15 20:51:08 2019 +0100 +++ b/7-Haskell/README.txt Sat Jun 15 21:07:27 2019 +0100 @@ -32,4 +32,28 @@ [(a,b) | a <- crew, b <- crew] calculates all combinations of crew names. Changing it to: [(a,b) | a <- crew, b <- crew, a /= b] -lets you add filtering to stop people being paired with themselves. Or you could use "a < b" to make it return unordered unique pairings. \ No newline at end of file +lets you add filtering to stop people being paired with themselves. Or you could use "a < b" to make it return unordered unique pairings. + +Haskell has "map" - "map func list" - where "func" can be an anonymous function. + +Anonymous functions are defined as: + (\param_1 … param_n -> function_body) +They can be called in-line, which looks odd: + (\x -> x) "Logical." +return "Logical." (because the anon function returns the value passed to it). + +Alternatively, anonymous functions can be written as locally scoped functions after a main function definition, using "where": + squareAll list = map square list + where square x = x * x + +Map can also be used with part of a function such as "(+ 1)". The book calls this a "section", but it isn't clear what it means. The wiki says a "section" is a partially applied infix operator (https://wiki.haskell.org/Section_of_an_infix_operator). + +Haskell also has the standard functional filter (takes two parameter: a boolean "keep in list" function and a list) and foldl/foldr (take three parameters - a two-value function (value and accumulator), a starting value and a list). +You can even "foldl (+) 0 [1..3]" to sum by using "+" as the two-parameter folding function. + +All of this has apparently been a lie, though. Haskell functions only have one argument! If you check the type of a multi-argument method then you get "arg_1_type -> arg_2_type -> arg_3_type -> result" rather than just "arg_1_type, arg_2_type, arg_3_type -> result" (Note: it won't be bracketed on the left because it's not expecting a three-tuple) +This world view can be made more apparent with the following: + let prod x y = x * y + let double = prod 2 + let triple = prod 3 +double and triple are partially applied functions, and the only (sane) way to be able to do that is if "prod x y" is a two-part function! The book says "When Haskell computes prod 2 4, it is really computing "(prod 2) 4". [insert anonymous functions]". This is called currying.