首页 > 其他分享 >cs3110-2.6exercises

cs3110-2.6exercises

时间:2024-01-30 22:33:58浏览次数:29  
标签:function assert your Write exercises expression 2.6 Exercise cs3110

Exercise: values [✭]

What is the type and value of each of the following OCaml expressions?

7 * (1+2+3)
"CS " ^ string_of_int 3110

Hint: type each expression into the toplevel and it will tell you the answer. Note: ^ is not exponentiation.

int
string

Exercise: operators [✭✭]

Examine the table of all operators in the OCaml manual (you will have to scroll down to find it on that page).

  • Write an expression that multiplies 42 by 10.
  • Write an expression that divides 3.14 by 2.0. Hint: integer and floating-point operators are written differently in OCaml.
  • Write an expression that computes 4.2 raised to the seventh power. Note: there is no built-in integer exponentiation operator in OCaml (nor is there in C, by the way), in part because it is not an operation provided by most CPUs.

Exercise: equality [✭]

  • Write an expression that compares 42 to 42 using structural equality.
  • Write an expression that compares "hi" to "hi" using structural equality. What is the result?
  • Write an expression that compares "hi" to "hi" using physical equality. What is the result?

Exercise: assert [✭]

  • Enter assert true;; into utop and see what happens.
  • Enter assert false;; into utop and see what happens.
  • Write an expression that asserts 2110 is not (structurally) equal to 3110.

The expression assert expr evaluates the expression expr and returns () if expr evaluates to true. If it evaluates to false the exception Assert_failure is raised with the source file name and the location of expr as arguments. Assertion checking can be turned off with the -noassert compiler option. In this case, expr is not evaluated at all.

Exercise: if [✭]

Write an if expression that evaluates to 42 if 2 is greater than 1 and otherwise evaluates to 7.

Exercise: double fun [✭]

Using the increment function from above as a guide, define a function double that multiplies its input by 2. For example, double 7 would be 14. Test your function by applying it to a few inputs. Turn those test cases into assertions.

Exercise: more fun [✭✭]

  • Define a function that computes the cube of a floating-point number. Test your function by applying it to a few inputs.
  • Define a function that computes the sign (1, 0, or -1) of an integer. Use a nested if expression. Test your function by applying it to a few inputs.
  • Define a function that computes the area of a circle given its radius. Test your function with assert.

Exercise: date fun [✭✭✭]

Define a function that takes an integer d and string m as input and returns true just when d and m form a valid date. Here, a valid date has a month that is one of the following abbreviations: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec. And the day must be a number that is between 1 and the minimum number of days in that month, inclusive. For example, if the month is Jan, then the day is between 1 and 31, inclusive, whereas if the month is Feb, then the day is between 1 and 28, inclusive.

How terse (i.e., few and short lines of code) can you make your function? You can definitely do this in fewer than 12 lines.

标签:function,assert,your,Write,exercises,expression,2.6,Exercise,cs3110
From: https://www.cnblogs.com/sysss-blogs/p/17998060

相关文章

  • [Mac软件]DoubleTake for Mac(全景拼图软件) v2.6.12 (1086) 激活版本
    DoubleTakeforMac是一款功能强大的全景拼图软件,专为Mac用户设计,可以帮助用户轻松地将多张照片拼接成一张全景图像。这款软件具有直观的用户界面和丰富的功能,使得全景图像的制作变得简单快捷。本文将详细介绍DoubleTakeforMacv2.6.12激活版本的特点和功能。首先,DoubleTakefor......
  • springboot升级到2.6.x和2.7.x 兼容hystrix
    一、pom.xml需要引入的依赖二、项目开启熔断器开关2.1注解方式2.2xml方式三、依赖类缺失问题四、版本匹配安全检查问题五、测试验证六、结论一、pom.xml需要引入的依赖1<!--springboot升级到2.6.7,同样适用于2.7.0,2.7.18等-->2<parent>3......
  • Solon v2.6.5 发布(助力信创)
    Solon是什么框架?Java"生态级"应用开发框架。从零开始构建,有自己的标准规范与开放生态(历时六年,具备全球第二级别的生态规模)。相对于Spring,有什么特点?国产!国产!国产!启动快5~10倍。(更快)qps高2~3倍。(更高)低流量内存节省1/3~1/2。(更少)打包可以缩小到1/2~1/10;......
  • [Mac软件]App Cleaner & Uninstaller 8.2.6应用程序清理和卸载
    AppCleaner&Uninstaller是一款Mac应用程序,它可以帮助用户完全删除应用程序及其相关的服务文件、扩展文件等。以下是该应用程序的主要功能:完全删除应用程序:通过将应用程序图标拖到垃圾桶中删除程序,可以彻底清除应用程序及其相关文件,释放磁盘空间。删除所有类型的服务文件:除了删除......
  • macOS Monterey 12.6.8 (21G725) 正式版发布,ISO、IPSW、PKG 下载
    macOSMonterey12.6.8(21G725)正式版发布,ISO、IPSW、PKG下载本站下载的macOS软件包,既可以拖拽到Applications(应用程序)下直接安装,也可以制作启动U盘安装,或者在虚拟机中启动安装。另外也支持在Windows和Linux中创建可引导介质。2023年7月24日(北京时间25日凌晨),App......
  • macOS Monterey 12.6.8 (21G725) Boot ISO 原版可引导镜像
    macOSMonterey12.6.8(21G725)BootISO原版可引导镜像本站下载的macOS软件包,既可以拖拽到Applications(应用程序)下直接安装,也可以制作启动U盘安装,或者在虚拟机中启动安装。另外也支持在Windows和Linux中创建可引导介质。2023年7月24日(北京时间25日凌晨),Apple为......
  • macOS Monterey 12.6.3 (21G419) 正式版 ISO、IPSW、PKG 下载
    macOSMonterey12.6+,皆为安全更新,不再赘述。macOSMonterey12.6,发布于2022年9月12日(北京时间今日凌晨),本次为安全更新。今日(2022-07-21)凌晨,Apple终于发布了macOSMonterey12.5,包括功能增强、错误修复和安全性更新。macOSMonterey12.5自2022年5月18日起进入开发......
  • macOS Monterey 12.6.3 (21G419) Boot ISO 原版可引导镜像
    macOSMonterey12.6+,皆为安全更新,不再赘述。macOSMonterey12.6,发布于2022年9月12日(北京时间今日凌晨),本次为安全更新。今日(2022-07-21)凌晨,Apple终于发布了macOSMonterey12.5,包括功能增强、错误修复和安全性更新。macOSMonterey12.5自2022年5月18日起进入开发......
  • macOS Monterey 12.6.6 (21G646) 正式版发布,ISO、IPSW、PKG 下载
    macOSMonterey12.6.6(21G646)正式版发布,ISO、IPSW、PKG下载本站下载的macOS软件包,既可以拖拽到Applications(应用程序)下直接安装,也可以制作启动U盘安装,或者在虚拟机中启动安装。另外也支持在Windows和Linux中创建可引导介质。2023年5月18日(北京时间19日凌晨),App......
  • [Mac软件]实用的屏幕录像工具:Apeaksoft Screen Recorder Mac v2.2.6
    编辑ApeaksoftScreenRecorder是一款专为Mac用户设计的高效、实用的屏幕录制工具。它可以帮助用户轻松地在计算机上录制任何视频、音频和屏幕截图,无论是在线电影、广播、游戏、视频教程还是网络摄像头视频,都可以轻松应对。此外,ApeaksoftScreenRecorder还提供了丰富的编辑功能,如......