haskell - Partial application of functions and currying, how to make a better code instead of a lot of maps? -


i beginner @ haskell , trying grasp it.

i having following problem:

i have function gets 5 parameters, lets

f x y w z = x - y - w - z - 

and apply while changing variable x 1 10 whereas y, w, z , a same. implementation achieved following think there must better way.

let's use:

x 1 10 y = 1  w = 2  z = 3  = 4 

accordingly managed apply function following:

map ($ 4) $ map ($ 3) $ map ($ 2) $ map ($ 1) (map f [1..10]) 

i think there must better way apply lot of missing parameters partially applied functions without having use many maps.

all suggestions far good. here's another, might seem bit weird @ first, turns out quite handy in lots of other situations.

some type-forming operators, [], operator maps type of elements, e.g. int type of lists of elements, [int], have property of being applicative. lists, means there way, denoted operator, <*>, pronounced "apply", turn lists of functions , lists of arguments lists of results.

(<*>) :: [s -> t] -> [s] -> [t]    -- 1 instance of general type of <*> 

rather ordinary application, given blank space, or $

($)   :: (s -> t) ->  s  ->  t 

the upshot can ordinary functional programming lists of things instead of things: call "programming in list idiom". other ingredient that, cope situation when of our components individual things, need gadget

pure :: x -> [x]   -- again, 1 instance of general scheme 

which wraps thing list, compatible <*>. pure moves ordinary value applicative idiom.

for lists, pure makes singleton list , <*> produces result of every pairwise application of 1 of functions 1 of arguments. in particular

pure f <*> [1..10] :: [int -> int -> int -> int -> int] 

is list of functions (just map f [1..10]) can used <*> again. rest of arguments f not listy, need pure them.

pure f <*> [1..10] <*> pure 1 <*> pure 2 <*> pure 3 <*> pure 4 

for lists, gives

[f] <*> [1..10] <*> [1] <*> [2] <*> [3] <*> [4] 

i.e. list of ways make application f, 1 of [1..10], 1, 2, 3 , 4.

the opening pure f <*> s common, it's abbreviated f <$> s, so

f <$> [1..10] <*> [1] <*> [2] <*> [3] <*> [4] 

is typically written. if can filter out <$>, pure , <*> noise, kind of looks application had in mind. punctuation necessary because haskell can't tell difference between listy computation of bunch of functions or arguments , non-listy computation of what's intended single value happens list. @ least, however, components in order started with, see more what's going on.

esoterica. (1) in (not very) private dialect of haskell, above be

(|f [1..10] (|1|) (|2|) (|3|) (|4|)|) 

where each idiom bracket, (|f a1 a2 ... an|) represents application of pure function 0 or more arguments live in idiom. it's way write

pure f <*> a1 <*> a2 ... <*> 

idris has idiom brackets, haskell hasn't added them. yet.

(2) in languages algebraic effects, idiom of nondeterministic computation not same thing (to typechecker) data type of lists, although can convert between two. program becomes

f (range 1 10) 2 3 4 

where range nondeterministically chooses value between given lower , upper bounds. so, nondetermism treated local side-effect, not data structure, enabling operations failure , choice. can wrap nondeterministic computations in handler give meanings operations, , 1 such handler might generate list of solutions. that's say, notation explain what's going on pushed boundary, rather peppered through entire interior, <*> , pure.

managing boundaries of things rather interiors 1 of few ideas our species has managed have. @ least can have on , on again. it's why farm instead of hunting. it's why prefer static type checking dynamic tag checking. , on...


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -