[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 ...
随机推荐
- 在类文件中创建 写入Json文件
由于业务需要 今天写了一个方法能够定时更新Json文件 即定时从数据库中查询数据 然后转化为Json对象 如果有数据的话 删掉之前的Json文件 重新创建一个文件 然后写入Json对象 中间走了很多弯 ...
- jupyter notebook 代码自动补齐插件
安装: . pip install jupyter_contrib_nbextensions -i https://pypi.mirrors.ustc.edu.cn/simple 2. jupyter ...
- 云笔记项目-Spring事务学习-传播Requried
在准备好前期的项目搭建后,接下来就一个个的测试,首先测试事务传播的Required Service层两个实现类 Service层两个实现类,只是更换了方法事务传播的属性,其他都一样,后续测试也只修改传 ...
- [Solution] 885. Spiral Matrix Ⅲ
Difficulty: Medium Problem On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) f ...
- 从零开始学spring cloud(四) -------- 基础项目搭建
1.创建一个spring cloud项目 1.1.使用工具创建--idea 点击creat new project,选择spring initializr 点击next,选择下一步 填入自己的Grou ...
- L2-026 小字辈(dfs)
本题给定一个庞大家族的家谱,要请你给出最小一辈的名单. 输入格式: 输入在第一行给出家族人口总数 N(不超过 100 000 的正整数) —— 简单起见,我们把家族成员从 1 到 N 编号.随后第二行 ...
- 解决layui table方法渲染时时间格式问题
在显示时间时没有成功 ,{field:'showTime',title:'要显示的时间'} 崎岖过程就不详述了,直接上干货 @官网相关文档1.@官网相关文档2.@参考文章1.@参考文章2 浏览了很多资 ...
- database锁实现
单独创建一张表存放获取锁所需的key和value,key值保持唯一,value从0开始按1递增,在代码中用私有成员变量ConcurrentHashMap存储每个key value值,初始化时每个线程的 ...
- 关于java工程打exe包的一些问题
这两天在把一个Java project打包成exe文件时碰到了一些问题,现在把这些问题和解决办法记下来. 1.用java swing做前端时,背景图片无法显示 Solution:把jpg图片换成png ...
- linux学习第十四天 (Linux就该这么学)找到一本不错的Linux电子书
今天老师讲了,DNS的相关,安装,配置,由来,13台根服务器,配置了主服务器,从服务器,和缓存服务器,等,今天补个大概吧,没有 记 还有正向解析,反向解析.