confused about composite function with map in haskell -


 let f = map tail.lines  f "fsdaf\nfdsf\n" 

why work?

let f = map tail.tail.lines f "fasdf\nfasdfdsfd\n" 

i result:

["asdfdsfd"] let f = map (tail.tail).lines f "fasdf\nfasdfdsfd\n" 

i result:

["sdf","sdfdsfd"] 

i want know haskell how parse code above.

lets @ first example:

let f = map tail.tail.lines f "fasdf\nfasdfdsfd\n" 

firstly, lines breaks input string array of strings: ["fasdf","fasdfdsfd"].

now, working right left, tail drops "head" of list: ["fasdfdsfd"]

lastly, map tail applies "tail" each element in list: ["asdfdsfd"]

your second example works similarly:

let f = map (tail.tail).lines f "fasdf\nfasdfdsfd\n" 

again, break input string array of strings: ["fasdf","fasdfdsfd"]

now however, you're creating composite function (tail.tail) (drop "head" of list, twice), , mapping each element in list.

therefore drop first 2 characters of each string.

["sdf","sdfdsfd"] 

both examples working intended. have read associativity , composite functions in haskell understand more it.

edit: difference this:

map tail (tail lines) vs map (tail.tail) lines

remember in haskell, functions first class citizens - can create composite functions (example: (+1).(+1)) , other operations (such map function list) not common in other languages.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -