课程回顾

Swarthmore学院16年开的编译系统课,总共10次大作业。本随笔记录了相关的课堂笔记以及第1次大作业。

什么是编译

编译就是执行Program->Program'转换的过程,如下图所示:

这个过程需要满足两个条件:

  1. The input and output program mean the same thing.
  2. 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的更多相关文章

  1. [swarthmore cs75] Lab 1 — OCaml Tree Programming

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第2大次作业. 比较两个lists的逻辑: let rec cmp l ll = match ( ...

  2. CS144学习(1)Lab 0: networking warmup

    CS144的实验就是要实现一个用户态TCP协议,对于提升C++的水平以及更加深入学习计算机网络还是有很大帮助的. 第一个Lab是环境配置和热身,环境按照文档里的配置就行了,前面两个小实验就是按照步骤来 ...

  3. [swarthmore cs75] Compiler 4 – Diamondback

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第6次大作业. 函数声明 增加函数声明.函数调用的抽象语法:在转换成anf之前还要检查函数声明和 ...

  4. [swarthmore cs75] Compiler 2 – Boa

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第4次大作业. A-Normal Form 在80年代,函数式语言编译器主要使用Continua ...

  5. [swarthmore cs75] inlab1 — Tiny Compiler

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了inlab1的实践过程. tiny compiler 这个迷你的编译器可以将一个源文件,编译成可执行的二进制代码. ...

  6. yii-basic-app-2.0.5/basic/config/web.php

    <?php $params = require(__DIR__ . '/params.php'); $config = [ 'id' => 'basic', 'basePath' => ...

  7. [swarthmore cs75] Compiler 6 – Garbage Snake

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第9次大作业. 赋值的副作用:循环元组 下面的代码展示了Python3是如何处理循环列表(pri ...

  8. [swarthmore cs75] Compiler 6 – Fer-de-lance

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第8次大作业. First-class function: It treats function ...

  9. [swarthmore cs75] Compiler 3 – Cobra

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第5次大作业. 增加了bool数据表示和比较运算符的支持,具体语法参考下图: 第一种int和bo ...

随机推荐

  1. python flask 解决中文乱码

    response = make_response(output_string)response.headers['Content-Type'] = 'text/plain;charset=UTF-8' ...

  2. tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("arg0:0", shape=(), dtype=float32, device=/device:CPU:0)'

    tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype flo ...

  3. python day11 函数(第三篇)

    2019.4.11 S21 day11笔记总结 1. 函数小高级 ( 5* ) 1 函数名可以当作变量来使用 def func(): print(123) v1 = func # func代表函数的地 ...

  4. CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-1虚拟机安装及环境初始化

    1.软件准备: VMware-workstation-full-14.1.2-8497320.exe CentOS-7-x86_64-DVD-1804.iso 2.VMare激活码: AU5WA-0E ...

  5. java中比较特殊的三个浮点数Infinity、-Infinity、NaN

    学过javaScript的应该都知道,在js中的数值型number类型中有几个特殊的数,一个正无穷大.一个负无穷大.一个不是一个数NaN. 后来无意中发现java中也有这三个数,不过这三个数是浮点数, ...

  6. CSS中的字体描边

    兴趣使然,突然看见网上的一些带有描边的字体,觉得有点意思,便尝试去做了下 不是什么很厉害的技巧,当然也有参考张鑫旭大神写的文章 只能感叹,css的世界还很大,很广阔 直入主题: 对于文字的描边,一般都 ...

  7. Kubernetes helm配置国内镜像源

    1.删除默认的源 helm repo remove stable 2.增加新的国内镜像源 helm repo add stable https://burdenbear.github.io/kube- ...

  8. py文件的运行

    安装过程及配置 安装过程准备: 下载好Python的安装程序后,开始安装,在进入安装界面后一定确保勾选将Python加入到系统环境变量的路径里.如图所示: 2 如果没有选取,那么按照下面的步骤进行操作 ...

  9. java maven compiler设置默认1.8

    方法一: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupI ...

  10. vue中部分api解释 ($nextTick)

    1:this.$nextTick(function(){ }) 传如的参数是一个函数 这个API主要是获取dom元素 为什么需要这个api,在vue框架开发中,更新dom是一个异步操作,如果更新完do ...