[swarthmore cs75] Lab 0 Warmup & Basic OCaml
课程回顾
Swarthmore学院16年开的编译系统课,总共10次大作业。本随笔记录了相关的课堂笔记以及第1次大作业。
什么是编译
编译就是执行Program->Program'转换的过程,如下图所示:

这个过程需要满足两个条件:
- The input and output program mean the same thing.
- The output is executable in a context we care about.
编译执行过程:

不可变数据结构(persistent data structure)
例1: 下图是两个列表在内存中的表示:
xs = [0, 1, 2]
ys = [3, 4, 5]

执行了连接操作,zs在内存中的表示:
```haskell
zs = xs ++ ys
```

注意到,xs被复制了一份,但是ys被共享了。最终xs和ys都没有被改变。其中xs被复制是因为,xs中最后一个元素2不能够指向ys的起始地址,因为这样的话xs就被改变了。
例2: 假设下面的数据代表下图的二叉搜索树
xs = [a, b, c, d, f, g, h]

在执行插入操作后,内存中的数据结构变为下图:
```haskell
ys = insert ("e", xs)
```

可以看到两个指针xs和ys,其中ys共享了xs中的节点。
编程作业
*OSX安装Ocaml
brew install ocaml opam
opam init
opam install extlib ounit
1.Implement fibonacci as an OCaml function that takes an integer n and returns the nth fibonacci number. Write out the evaluation of (fibonacci 3) in substitution style.
- 递归计算fibonacci数列
let rec fibonacci (n : int) : int =
if n <= 2 then 1
else (fibonacci (n - 1)) + (fibonacci (n - 2));;
- The evaluation of (fibonacci 3) in substitution style is :
=> (if 3 <= 2 then 1 else (fibonacci (3 - 1)) + (fibonacci (3 - 2)))
=> (if false then 1 else (fibonacci (3 - 1)) + (fibonacci (3 - 2)))
=> ((fibonacci (3 - 1)) + (fibonacci (3 - 2)))
=> ((fibonacci 2) + (fibonacci (3 - 2)))
=> ...
=> (1 + (fibonacci (3 - 2)))
=> (1 + (fibonacci 1))
=> ...
=> (1 + 1)
=> 2
2.Write tests for max and fibonacci using t_int.
- 测试用例
(* a helper for testing integers *)
let t_int name value expected = name>::
(fun _ -> assert_equal expected value ~printer:string_of_int);;
let max_test = t_int "" (max 4 5) 5;;
let fibonacci_test = t_int "fibonacci_test" (fibonacci 10) 55;;
let suite = "suite">:::[max_test; fibonacci_test;];;
run_test_tt_main suite
- >:: is a function that creates a TestLabel for a TestCase
val (>:
[swarthmore cs75] Lab 0 Warmup & Basic OCaml的更多相关文章
- [swarthmore cs75] Lab 1 — OCaml Tree Programming
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第2大次作业. 比较两个lists的逻辑: let rec cmp l ll = match ( ...
- CS144学习(1)Lab 0: networking warmup
CS144的实验就是要实现一个用户态TCP协议,对于提升C++的水平以及更加深入学习计算机网络还是有很大帮助的. 第一个Lab是环境配置和热身,环境按照文档里的配置就行了,前面两个小实验就是按照步骤来 ...
- [swarthmore cs75] Compiler 4 – Diamondback
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第6次大作业. 函数声明 增加函数声明.函数调用的抽象语法:在转换成anf之前还要检查函数声明和 ...
- [swarthmore cs75] Compiler 2 – Boa
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第4次大作业. A-Normal Form 在80年代,函数式语言编译器主要使用Continua ...
- [swarthmore cs75] inlab1 — Tiny Compiler
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了inlab1的实践过程. tiny compiler 这个迷你的编译器可以将一个源文件,编译成可执行的二进制代码. ...
- yii-basic-app-2.0.5/basic/config/web.php
<?php $params = require(__DIR__ . '/params.php'); $config = [ 'id' => 'basic', 'basePath' => ...
- [swarthmore cs75] Compiler 6 – Garbage Snake
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第9次大作业. 赋值的副作用:循环元组 下面的代码展示了Python3是如何处理循环列表(pri ...
- [swarthmore cs75] Compiler 6 – Fer-de-lance
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第8次大作业. First-class function: It treats function ...
- [swarthmore cs75] Compiler 3 – Cobra
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第5次大作业. 增加了bool数据表示和比较运算符的支持,具体语法参考下图: 第一种int和bo ...
随机推荐
- express 内存溢出问题分析定位
一.现象 1. 如下报错 FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 1: n ...
- 拦截过滤防御XSS攻击 -- Struts2.3 以及 2.5 的解决方式
使用Struts2框架开发的后台在防御XSS攻击的时候很多方式都不能用,因为Struts2对请求进行的二次封装有区别.以下针对Struts2的XSS攻击进行拦截过滤防御解决: Struts2.3 本方 ...
- java第一章抽象和封装
面向过程和面向对象有什么区别? 面向过程的核心是函数,以功能为中心,实现了函数级别的代码重用. 面向对象的核心是封装了属性和方法(行为)的类,以数据为中心,实现了类级别的代码重用. 面向对象因为采用了 ...
- 2018-2019-20175302实验二《Java面向对象程序设计》实验报告
2018-2019-2 学号实验二<Java面向对象程序设计>实验报告 一.实验步骤及内容 1. 面向对象程序设计 参考 http://www.cnblogs.com/rocedu/p/6 ...
- Getting.Started.with.Unity.2018.3rd.Edition
Getting Started with Unity 2018 - Third Edition: A Beginner's Guide to 2D and 3D game development wi ...
- vue-实现全选单选
在获取列表页面数据时,通过forEach遍历存储数据的对象,给对象中添加一个selected变量,值为布尔值. 点击全选时,通过遍历将对象中selected的布尔值改变 点击单选时,被点中的通过筛选加 ...
- html(),text(),var()区别与用法
text() 设置或返回所选元素的文本内容 html() 设置或返回所选元素的内容(包括HTML标记) val() 设置或返回表单字段的值 用html()效果把html标签也获取出来了: 用val() ...
- 第一个spring简单的helloworld
spring 是一个开源的框架 也是轻量级框架 1.导入jar包 spring的版本 4.0 目录: spring-framework-4.0.0.RELEASE-libs 下的jar spring ...
- Python:a,*args,**kwargs的理解
1.何时用这些参数? 在任何时候继承类和重写方法时,应当用到’*args’和’**kwargs’将接收到的位置参数和键值参数给父类方法 . 2.一句话清晰说明: a是常规的变量类型,比如int,str ...
- Rappid 消除试用版的弹出框
今天想学习JavaScript的FlowCharts,发现有个Rappid库挺不错的(如下图2所示),下了一个后发现在打开窗口时总是要弹出一个提示框,这严重影响了学习的进度,于是相办法将其去掉吧,按照 ...