COMP1811 Paradigms of Programming
Some useful functionals in Scheme
Map, filter, findf,foldl, foldr,apply
Copyright By PowCoder代写 加微信 powcoder
• 3 common functionals
• variants of map
• n-ary map
• andmap, • ormap
• Binary operations
• Variable number of arguments
Having a certain knowledge on these functionals can make you to save a lot of code!
Mind arity and/or types while typing!
Scheme is dynamically typed!! (Typing error arise on execution)
Error can be detected far from origin
• map: fnc, lst -> lst
– list whose members are results of applying fnc to every element
• filter: pred?,list -> lst
– list of elements whose eval
on pred yields true
• findf: pred?,list -> elem/#f
– first element full filling pred. #f if none
Common functionals
3 important functionals
• Provided n lists, map accepts n-ary lambda form.
• Lambda picks one element from each list as it appears
map variants
• andmap: prec?, list-> bool
– True if all members of the lists fit the property prec. (For all)
• ormap: prec?, list-> bool
– True if any member of the lists fit the property prec. (There is)
map variants
andmap ormap
• Types a,b (not necessarily same)
– op: a,b -> b
– lst: list of a.
• foldl : op,init,lst ->val
– From left to right, processes op
• list item as first operand
• acum result as second operand
– from previous op
– init on starting
– On empty lists, yields acum
Binary operations
• Types a,b (not necessarily same)
– op: a,b -> b
– lst: list of a.
• foldr : op,init,lst ->val
– From right to left, processes op
• list item as first operand
• acum result as second operand
– from previous op
– init on starting
– On empty lists, yields acum
Binary operations
– op:a,a->a
– lst: list of a.
– implicit neutral element
• foldr : op,init,lst ->val
– From right to left, processes op
• list item as first operand
• acum result as second operand
– from previous op
– init on starting
– On empty lists, yields acum
Binary operations
• How to simulate variable number of arguments?
– (f a1 a2 …an). • Fact:
– every specific call have a finite number of it
– Gather all them in a list
– Then apply the operator to the list
Variable number of arguments
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com