📣 独立日限时特惠: VIP通行证立减$68
查看: 2831| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[Udacity CS 262] Programming Languages (Week #5)

全局:
公开课
学校名称: University of Virgnia
Unit号: 5
开课时间: 2012-07-17
课程全名: CS 262 Programming Languages
平台: Udacity

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
previously:
第四个unit  http://www.1point3acres.com/bbs/thread-37362-1-1.html
第三个unit  http://www.1point3acres.com/bbs/thread-37239-1-1.html
第二个unit  http://www.1point3acres.com/bbs/thread-37174-1-1.html
第一个unit  http://www.1point3acres.com/bbs/thread-37089-1-1.html


经过前几个unit讲述的tokenizing, parsing之后,本章接着讲interpreting的部分。把这几个unit的代码合在一起,就解释运行一个简单的javascript代码了,很有趣的。

本章讲了一下几个知识点

1. 解释器
An interpreter finds the meaning of a program by traversing its parse tree.
讲义里给了一个例子:
We're going to write a recursive procedure called interpret that's going to walk over abstract syntax trees and figure out what they mean.
For example, you could write out a webpage that said, "Hello, Friend", and it would be a list of 2 elements.
示例代码:
  1. def interpret(trees): # Hello, friend
  2.     for tree in trees: # Hello,
  3.         # ("word-element","Hello")
  4.         nodetype=tree[0] # "word-element"
  5.         if nodetype == "word-element":
  6.             graphics.word(tree[1])
  7.         elif nodetype == "tag-element":
  8.             # <b>Strong text</b>
  9.             tagname = tree[1] # b
  10.             tagargs = tree[2] # []
  11.             subtrees = tree[3] # ...Strong Text!...
  12.             closetagname = tree[4] # b
  13.   
  14.             if(tagname <> closetagname):
  15.                 graphics.warning()
  16.             else:
  17.                 graphics.begintag(tagname, tagargs)
  18.                 interpret(subtrees)
  19.                 graphics.endtag()
复制代码


2. 变量与环境
There is a special global environment that can hold variable values. Other environments have parent pointers to keep track of nesting or scoping. Environments hold storage locations and map variables to values.
关于chained environment:遇见变量名先查自己的scope,再依照chain查找parent environment。

3. 函数调用
interpreter发现有function statement被调用时,需要创建一个新环境(全局变量加上函数参数)。然后,再递归地interpret该函数体。


如果你希望在本机运行样例程序,坏消息:本章部分程序需要graphics库,需要2.5G
http://forums.udacity.com/cs262/questions/5712/unit-5-9-graphics-library

编程作业题:



I Could Wile Away The Hours

给出的代码可以完成“解释javascript的if-then结构和函数调用”的功能,你要做的是添加实现循环语句的功能。

For this assignment, you should write a procedure:
       eval_while(while_statement, evnironment)
Your procedure can (and should!) call those other procedures.


Higher Order Function


Suppose that we know that we will be filtering many lists down to only their odd

elements. Then we might want something like this:

# filter_odds = filter_maker(lambda n : n % 2 == 1)
# odds = filter_odds(numbers)

In this example, "filter_maker()" is a function that takes a function as # an argument and returns a function as its result. We say that filter_maker is a *higher order function*.






上一篇:[Udacity CS 215] Algorithms (Week #2)
下一篇:Coursera Algorithm Part 1
🔗
joshua.cong 2012-8-10 17:59:09 | 只看该作者
全局:
觉得programming language  一定要用funcional programming讲。。 scheme,LISP一类。。
回复

使用道具 举报

🔗
 楼主| defjex 2012-8-10 21:30:37 | 只看该作者
全局:
joshua.cong 发表于 2012-8-10 17:59
觉得programming language  一定要用funcional programming讲。。 scheme,LISP一类。。

恩 这门课还专门讲了lamda 毕竟python和javascript都是支持lamda的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表