zoukankan      html  css  js  c++  java
  • 编程泛型

    http://cs.lmu.edu/~ray/

    Programming Paradigms

    The word paradigm is used a great deal when talking about programming languages. What does it mean?

    Definition

    programming paradigm is a style or "way" of programming. Some languages make it easy to write in some paradigms but not others.

    List of Common Paradigms

    Some of the more common paradigms are

    • Imperative — Control flow is an explicit sequence of commands.
    • Declarative — Programs state the result you want, not how to get it.
    • Structured — Programs have clean, goto-free, nested control structures.
    • Procedural — Imperative programming with procedure calls.
    • Functional (Applicative) — Computation proceeds by (nested) function calls that avoid any global state.
    • Function-Level (Combinator) — Programs have no variables. No kidding.
    • Object-Oriented — Computation is effected by sending messages to objects; objects have state and behavior.
      • Class-based — Objects get their state and behavior based on membership in a class.
      • Prototype-based — Objects get their behavior from a prototype object.
    • Event-Driven — Control flow is determined by asynchronous actions (from humans or sensors).
    • Flow-Driven — Computation is specified by multiple processes communicating over predefined channels.
    • Logic (Rule-based) — Programmer specifies a set of facts and rules, and an engine infers the answers to questions.
    • Constraint — Programmer specifies a set of constraints, and an engine infers the answers to questions.
    • Aspect-Oriented — Programs have cross-cutting concerns applied transparently.
    • Reflective — Programs manipulate their own structures.
    • Array — Operators are extended to arrays, so loops are normally unnecessary.

    Paradigms are not meant to be mutually exclusive; you can program in a functional, object-oriented, event-driven style. Make sure to check out Wikipedia's entry on Programming Paradigms.

    A Look At Some Major Paradigms

    Imperative Programming

    Control flow in imperative programming is explicit: commands show how the compuation takes place, step by step. Each step affects the global state of the computation.

        result =[]
        i
    =0
    start
    :
        numPeople
    = length(people)
       
    if i >= numPeople gotoend
        p
    = people[i]
        nameLength
    = length(p.name)
       
    if nameLength <=5gotonext
        upperName
    = toUpper(p.name)
        addToList
    (result, upperName)
    next:
        i
    = i +1
       
    goto start
    end:
       
    return sort(result)

    Structured Programming

    Structured programming is a kind of imperative programming where the control flow is defined by nested loops, conditionals, and subroutines, rather than via gotos. Variables are generally local to blocks (have lexical scope).

    result =[];
    for i =0; i < length(people); i++{
        p
    = people[i];
       
    if length(p.name))>5{
            addToList
    (result, toUpper(p.name));
       
    }
    }
    return sort(result);

    Early languages emphasizing structured programming: Algol 60, PL/I, Algol 68, Pascal, C, Ada 83, Modula, Modula-2. Structured programming as a discipline is sometimes though to have been started by a famous letter by Edsger Dijkstra entitled Go to Statement Considered Harmful.

    Object Oriented Programming

    OOP is based on the sending of messages to objects. Objects respond to messages by performing operations. Messages can have arguments, so "sending messages" looks a lot like calling subroutines. A society of objects, each with their own "local memory" and own set of operations has a different feel than the "monolithic processor and single shared memory" feel of non object oriented languages.

    result =[]
    for p in people {
       
    if p.name.length >5{
            result
    .add(p.name.toUpper);
       
    }
    }
    return result.sort;

    The first object oriented language was Simula-67; Smalltalk followed soon after as the first "pure" object-oriented language. Many languages designed from the 1980s to the present have been object-oriented, notably C++, CLOS (object system of Common Lisp), Eiffel, Modula-3, Ada 95, Java, C#, Ruby.

    Declarative Programming

    Control flow in declarative programming is implicit: the programmer states only what the result should look like,not how to obtain it.

    select upper(name)
    from people
    where length(name)>5
    orderby name

    No loops, no assignments, etc. Whatever engine that interprets this code is just supposed go get the desired information, and can use whatever approach it wants. (The logic and relational paradigms are generally declarative as well.)

    Functional Programming

    In functional programming control flow is expressed by combining function calls, rather than by assigning values to variables.

    let(
      f
    ,fun(
        people
    ,
       
    if(equals(people, emptylist),
            emptylist
    ,
           
    if(greater(length(name(head(people))),5),
              append
    (to_upper(name(head(people))), f(tail(people))),
              f
    (tail(people))))),
        sort
    (f(people)))

    Of course, there's usually syntactic sugar

    let
       
    fun f []=[]
         
    | f (p :: ps)=
             
    if p.name.length()>5then p.name.to_upper()::(f ps)
             
    else(f ps)
    in
        sort
    (f(people))

    The real power of this paradigm comes from passing functions to functions (and returning functions from functions).

    sort(
        filter
    ((λs. s.length()>5),
            map
    ((λp. p.name.to_upper()), people)

    Read Joel Spolsky's article on map and reduce.

    With functional programming

    • There are no commands, only side-effect free expressions
    • Code is much shorter, less error-prone, and much easier to prove correct
    • There is more inherent parallelism, so good compilers can produce faster code

    Some people like to say

    • Functional, or Applicative, programming is programming without assignment statements: one justs applies functions to arguments. Examples: Scheme, Haskell, Miranda, ML.
    • Function-level programming does away with the variables; one combines functions with "functionals". Examples: FP, FL, J.
    Exercise: Write the above example in Miranda, ML, and J.

    Python has a neat little thing called list comprehensions that combine map and filter.

    sorted([p.name.upper()for p in people if len(p.name)>5])

    Logic and Constraint Programming

    Logic and constraint programming are two paradigms in which programs are built by setting up relations that specify facts and inference rules, and asking whether or not something is true (i.e. specifying a goal.) Unification and backtracking to find solutions (i.e. satisfy goals) takes place automatically.

    Languages that emphasize this paradigm: Prolog, GHC, Parlog, Vulcan, Polka, Mercury, Fnil.

  • 相关阅读:
    Mvc5+Ef 6.0入门开发随笔(2.MVC的简单创建与使用图文详解)
    转载:CSDN mvc ef 的简单增删改查操作
    插件
    Jmeter-后置处理器--json提取器
    jmeter的几种参数化方式
    Python--变量命名规范
    Python--对list、tuple、dict的操作
    Python--列表中字符串按照某种规则排序的方法
    Python--遍历文件夹下所有文件和目录的方法(os.walk(rootdir)函数返回一个三元素元祖)
    Python--递归函数实现:多维嵌套字典数据无限遍历
  • 原文地址:https://www.cnblogs.com/blockcipher/p/3503100.html
Copyright © 2011-2022 走看看