Composing Programs(SICP python版) chap1 笔记

news2024/11/19 20:19:33

《Composing Programs》(SICP python版) chap1 笔记

持续更新中

在学习 CS61A 2022fall的时候配着看的

文章目录

  • 《Composing Programs》(SICP python版) chap1 笔记
  • Chapter 1: Building Abstractions with Functions
    • 1.1 Getting Started
      • 1.1.1 Programming in Python
      • 1.1.2 Installing Python 3
        • pip安装过程(Ubuntu18.04)
      • 1.1.3 Interactive Sessions
      • 1.1.4 First Example
        • **Statements & Expressions**.
        • **Functions**.
          • 问题
        • **Objects**.
        • **Interpreters**.
      • 1.1.5 Errors
    • 1.2 Elements of Programming
    • 1.3 Defining New Functions
    • 1.4 Designing Functions
    • 1.5 Control
    • 1.6 Higher-Order Functions
    • 1.7 Recursive Functions
  • 相关资源

本书的preface👇

Welcome to Composing Programs, a free online introduction to programming and computer science.
In the tradition of SICP, this text focuses on methods for abstraction, programming paradigms, and techniques for managing the complexity of large programs. These concepts are illustrated primarily using the Python 3 programming language.
In addition to reading the chapters below, you can apply your knowledge to the programming projects that accompany the text and visualize program execution using the Online Python Tutor.
Instructors: If you are interested in adapting any of these materials for your courses, please fill out this short survey so that we can support your efforts.

Chapter 1: Building Abstractions with Functions

1.1 Getting Started

感觉这段话写的很棒

The high productivity of computer science is only possible because the discipline is built upon an elegant and powerful set of fundamental ideas.

计算机科学是建立在一套优雅而强有力的基本思想之上的,这使得它的高生产力成为可能。

All computing begins with representing information, specifying logic to process it, and designing abstractions that manage the complexity of that logic.

所有的计算都始于信息的表示,指定处理信息的逻辑,以及设计抽象概念来应对逻辑的复杂性。

Mastering these fundamentals will require us to understand precisely how computers interpret computer programs and carry out computational processes.

掌握这些基本原理需要我们准确地理解计算机如何解释程序并执行计算过程。

1.1.1 Programming in Python

  • Python is a widely used programming language that has recruited(吸收) enthusiasts from many professions.

  • The Python language itself is the product of a large volunteer community that prides itself on the diversity of its contributors. The language was conceived(构思) and first implemented(实现) by Guido van Rossum in the late 1980’s. The first chapter of his Python 3 Tutorial explains why Python is so popular, among the many languages available today.

  • Python excels(突出) as an instructional language( 作为一种教学语言非常出色) because, throughout its history, Python’s developers have emphasized the human interpretability (可解释性)of Python code, reinforced by the Zen of Python guiding principles of beauty, simplicity, and readability. Python is particularly appropriate for this text because its broad set of features support a variety of different programming styles, which we will explore. While there is no single way to program in Python, there are a set of conventions(一组约定) shared across the developer community that facilitate reading, understanding, and extending existing programs. Python’s combination of great flexibility and accessibility allows students to explore many programming paradigms(许多编程范式), and then apply their newly acquired knowledge to thousands of ongoing projects.

  • These notes maintain the spirit of SICP by introducing the features of Python in step with techniques for abstraction and a rigorous model of computation(严格的计算模型). In addition, these notes provide a practical introduction to Python programming, including some advanced language features and illustrative examples(说明性示例). Increasing your facility with Python should come naturally as you progress through the text.

1.1.2 Installing Python 3

  • 在lab0里面解决了python3的安装,但是当时没有装pip,这里来记录一下

pip安装过程(Ubuntu18.04)

  • 一开始以为自带了

    image-20221221193242133

  • 于是sudo apt install python-pip

    装好之后发现版本好低诶

    image-20221221193327995

    突然意识到可能是python2的pip

    image-20221221193405224

  • 于是查了一下Linux安装pip

    • 前往pip官网 https://pip.pypa.io/en/stable/installation/

      image-20221221193818935
      • 先是试了试那个ensurepip ,结果Ubuntu搞不了

        image-20221221193901785

    • 于是用下面那个get-pip.py

      wget https://bootstrap.pypa.io/get-pip.py
      

      习惯了win上直接python ,然鹅应该用python3
      image-20221221194001603

      查看了一下发现自己默认py3版本还是3.6

      先改成3.9

      ~$ sudo rm /usr/bin/python3
      ~$ sudo ln -s /usr/bin/python3.9 /usr/bin/python3
      
      
  • 以为可以python3 get-pip.py

    报了个错ModuleNotFoundError: No module named 'distutils.cmd'

    image-20221221194226906

    在askubuntu上面查了个解决方案

    sudo apt-get install python3-distutils
    sudo apt-get install python3-apt
    

    又来个错误ModuleNotFoundError: No module named 'apt_pkg'

    image-20221221194711938

    看了StackOverflow上的[解决方案](python-dev installation error: ImportError: No module named apt_pkg)

    image-20221221194947119

    我那个路径下有python3.6对应的,于是我sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

  • 我以为可以了 ,结果依旧是ModuleNotFoundError: No module named 'distutils.cmd'

    image-20221221195527170
  • 于是我对症下药了,我寻思已经python3默认版本改为python3.9了但是它为啥好像给python3.6安装了distutils但是没给py3.9装,于是我

    受到这个回答的启发

    image-20221221200254315
    sudo apt-get install --reinstall python3.9-distutils
    

    image-20221221195803308

  • 接下来可以了

    python3 get-pip.py

    image-20221221195929786

    这个问题先留个链接WARNING: The script pip3.8 is installed in ‘/usr/local/bin’ which is not on PATH,回头看看

  • 然后…

    我猜它not found的原因就是上面那个没有安装在path上?呜呜不清楚

    image-20221221200622854

  • 再接着 sudo apt install python3-pip

    可以pip3 list了,但是遇到一个warning

    image-20221221201455200
  • 查看一下pip和pip3

    image-20221221201532354

    Warning: pip is being invoked by an old script wrapper

    大家的做法基本是reinstall

    这个里面的最后一个解决方案好像因为他也有我上面那个遗留的问题,所以没法下一步reinstall

  • 先回头去解决上一个遗留的问题

    就像windows里面 cmd 输入path,就可以打印环境变量路径

    image-20221221202155027

    接下来按照这个教程来https://stackoverflow.com/a/70680333/18159372

    export PATH="/home/bkin/.local/bin:$PATH"
    
    source ~/.bash_profile
    
    echo $PATH
    

    看看PATH

    好像没source成功也加进去了?

    image-20221221202720838

    再pip3 list康康

    神奇,没有reinstall,这个warning就消失了

    image-20221221202814539

1.1.3 Interactive Sessions

  • In an interactive Python session, you type some Python code after the prompt(提示符), >>>. The Python interpreter reads and executes what you type, carrying out your various commands.

    • If you see the Python prompt, >>>, then you have successfully started an interactive session. These notes depict example interactions using the prompt, followed by some input.

      >>> 2 + 2
      4
      
  • Interactive controls.

    • Each session keeps a history of what you have typed.
    • To access that history, press <Control>-P (previous) and <Control>-N (next).
    • <Control>-D exits a session, which discards this history.
    • Up and down arrows also cycle through history on some systems.

1.1.4 First Example

soga,cs61A lec1里面用的就是这个William ShakeSpeare的例子

And, as imagination bodies forth

The forms of things to unknown, and the poet’s pen

Turns them to shapes, and gives to airy nothing

A local habitation and a name.

—William Shakespeare, A Midsummer-Night’s Dream

wow,仲夏夜之梦

  • This section:an example that uses several language features of python as a sneak preview(预览) of features to come.
  • Next section:start from scratch and build up the language piece by piece.

Python has built-in support for a wide range of common programming activities, such as manipulating text, displaying graphics, and communicating over the Internet. The line of Python code

>>> from urllib.request import urlopen

is an import statement that loads functionality for accessing data on the Internet. In particular, it makes available a function called urlopen, which can access the content at a uniform resource locator (URL), a location of something on the Internet.

Statements & Expressions.

  • Python code consists of expressions and statements(表达式和语句). Broadly, computer programs consist of instructions to either compute some value or carry out some action.

    • Statements typically describe actions. When the Python interpreter executes a statement, it carries out the corresponding action. (当 Python 解释器执行一条语句时,它会执行相应的操作)
    • On the other hand, expressions typically describe computations. When Python evaluates an expression, it computes the value of that expression.
  • The assignment statement

    >>> shakespeare = urlopen('http://composingprograms.com/shakespeare.txt')
    

    associates the name shakespeare with the value of the expression that follows =. That expression applies the urlopen function to a URL that contains the complete text of William Shakespeare’s 37 plays, all in a single text document.

    image-20221221185744707

Functions.

Functions encapsulate logic that manipulates data(函数封装了操作数据的逻辑).

  • urlopen is a function. A web address is a piece of data, and the text of Shakespeare’s plays is another. The process by which the former leads to the latter may be complex, but we can apply that process using only a simple expression because that complexity is tucked away within a function(因为这种复杂性隐藏在一个函数中).

  • Another assignment statement

    >>> words = set(shakespeare.read().decode().split())
    

    associates the name words to the set of all unique words that appear in Shakespeare’s plays, all 33,721 of them.

    The chain of commands to read, decode, and split, each operate on an intermediate computational entity(运算中间实体?): we read the data from the opened URL, then decode the data into text, and finally split the text into words(拆分为单词). All of those words are placed in a set.

    image-20221221190114038

问题
  • 在我的Linux虚拟机上搞这一步的时候遇到问题了

    image-20221221203546625

  • 尝试了这个回答里面的 https://stackoverflow.com/a/65629120/18159372

    无济于事

    哈哈要是看到旁边的votes我就不试了

    image-20221221203904594

  • 更奇怪的事情发生了

    我用vim写入test.py,然后再执行它,发现可以输出

    image-20221221204542516

    另外,在windows上输出words单词顺序好像不一样…? 这又是为何

    image-20221221204635547

    又试了一下… 我相信是网络问题了

    交互模式也可以输出,另外这次words里面的单词顺序又不一样了(

    image-20221221204729183

Objects.

  • A set is a type of object, one that supports set operations like computing intersections and membership.

  • An object seamlessly bundles together data and the logic that manipulates that data, in a way that manages the complexity of both.

  • Finally, the expression

    {w for w in words if len(w) == 6 and w[::-1] in words}
    {'reward', 'diaper', 'drawer', 'redder', 'repaid'}
    

    is a compound expression(复合表达式) that evaluates to the set of all Shakespearian words that are simultaneously(同时) a word spelled in reverse.
    单词和它的倒序单词同时在文本里
    The cryptic notation(神秘符号) w[::-1] enumerates(枚举) each letter in a word, but the -1 dictates to step backwards.

Interpreters.

  • Evaluating compound expressions requires a precise procedure that interprets code in a predictable way. A program that implements such a procedure, evaluating compound expressions, is called an interpreter.(解释器)

  • When compared with other computer programs, interpreters for programming languages are unique in their generality. Python was not designed with Shakespeare in mind. However, its great flexibility allowed us to process a large amount of text with only a few statements and expressions.


  • In the end, we will find that all of these core concepts are closely related: functions are objects, objects are functions, and interpreters are instances of both. However, developing a clear understanding of each of these concepts and their role in organizing code is critical to mastering the art of programming.(清楚地理解这些概念中的每一个及其在组织代码中的作用对于掌握编程艺术至关重要)

1.1.5 Errors

  • While computers are tremendously fast and flexible, they are also extremely rigid. The nature of computers is described in Stanford’s introductory course as

上面这个斯坦福cs101的链接挺有趣的

The fundamental equation of computers is:

computer = powerful + stupid

Computers are very powerful, looking at volumes of data very quickly. Computers can perform billions of operations per second, where each operation is pretty simple.

Computers are also shockingly stupid and fragile(脆弱). The operations that they can do are extremely rigid, simple, and mechanical(机械的). The computer lacks anything like real insight(真正的洞察力) … it’s nothing like the HAL 9000 from the movies. If nothing else, you should not be intimidated(恐吓) by the computer as if it’s some sort of brain. It’s very mechanical underneath it all(这一切都非常机械化).

Programming is about a person using their real insight to build something useful, constructed out of these teeny(微小), simple little operations that the computer can do.

—Francisco Cai and Nick Parlante, Stanford CS101

The rigidity of computers will immediately become apparent as you experiment with the Python interpreter: even the smallest spelling and formatting changes will cause unexpected output and errors.

Learning to interpret errors and diagnose the cause of unexpected errors is called debugging. Some guiding principles of debugging are:

  1. Test incrementally: Every well-written program is composed of small, modular components that can be tested individually. Try out everything you write as soon as possible to identify problems early and gain confidence in your components.
  2. Isolate(隔离) errors: An error in the output of a statement can typically be attributed to a particular modular component. When trying to diagnose a problem, trace the error to the smallest fragment of code you can before trying to correct it.(在尝试更正错误之前,先将错误追踪到最小的代码片段。)
  3. Check your assumptions(假设): Interpreters do carry out your instructions to the letter (一字不漏)— no more and no less. Their output is unexpected when the behavior of some code does not match what the programmer believes (or assumes) that behavior to be. Know your assumptions, then focus your debugging effort on verifying that your assumptions actually hold(验证假设是否成立).
  4. Consult others: You are not alone! If you don’t understand an error message, ask a friend, instructor, or search engine. If you have isolated an error(如果已经找出错误), but can’t figure out how to correct it, ask someone else to take a look. A lot of valuable programming knowledge is shared in the process of group problem solving.

Incremental testing, modular design, precise assumptions, and teamwork are themes that persist throughout this text. Hopefully, they will also persist throughout your computer science career.

1.2 Elements of Programming

1.3 Defining New Functions

1.4 Designing Functions

1.5 Control

1.6 Higher-Order Functions

1.7 Recursive Functions

相关资源

  • 开源书网址

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/106709.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

python安装face_recognition

本人使用系统为windows10,python的版本是3.8&#xff0c;在安装face_recognition之前需要安装以下内容&#xff1a; 1.cmake 2.dlib&#xff0c;dlib的安装依赖于cmake 1 安装CMake 1.1 官网下载&#xff1a;CMake 1.2 开始安装CMake: 1.3 验证是否安装成功&#xff1a; 打开…

10 Mysql中各种锁

概述 MySQL中的也存在一些类型的锁&#xff0c;用来保证多个连接同时操作数据时的安全即数据的一致性问题&#xff1b;同时&#xff0c;虽然锁能够解决一些数据的一致性和有效性&#xff0c;但是我们还是要选择合适的锁来降低锁对于并发问题的影响 1. 全局锁 全局锁就是对整…

傻白探索Chiplet,互连技术研究现状(七)

目录 一、串行互连 二、并行互连 三、串行与并行互连的比较 四、互连标准接口 &#xff08;1&#xff09;背景 &#xff08;2&#xff09;UCIe Chiplet的可行性常常受到片间互连的性能、可用性以及功耗和成本问题的限制&#xff0c;各种异构芯片的互连接口和标准的设计在技…

Web3中文|恐惧vs伦理:AI艺术评论家错在哪里?

本周&#xff0c;人工智能引发众怒。随着“AI艺术”在网络的流行&#xff0c;一群艺术家正在知名艺术家平台Art Station上掀起一场反AI艺术的抗议活动&#xff0c;而人工智能技术的拥趸者也及时回击了这波反对热潮。 这种充斥着反对意见的热潮是迟早会出现的。现在这些人认为“…

简单说手什么是JWT?

JSON Web Token&#xff08;缩写 JWT&#xff09;是目前最流行的跨域认证解决方案。 传统的session认证 http协议本身是一种无状态的协议&#xff0c;而这就意味着如果用户向我们的应用提供了用户名和密码来进行用户认证&#xff0c;那么下一次请求时&#xff0c;用户还要再一…

【Lingo】【MATLAB】【求解运筹学问题模板题】

文章目录一、线性规划模型&#xff08;Lingo&#xff09;1.线性规划问题&#xff08;模板&#xff09;2.求解最优化问题3.包装箱平板车问题4.职员时序安排问题5.运输问题6.排菜单问题7.工地施工问题8.生产计划优化研究&#xff08;柴油机生产&#xff09;二、线性规划问题&…

机器学习算法基础——逻辑回归

01逻辑回归可以用来解决简单的二分类问题。 逻辑回归的预测函数为hθ(x)g(θTx)h_\theta (x)g(\theta^Tx)hθ​(x)g(θTx)&#xff0c;其中g(x)g(x)g(x)为sigmoidsigmoidsigmoid函数&#xff0c;用于将数值映射到区间[0,1][0,1][0,1]中&#xff0c;然后再取对数值用于刻画损失函…

51单片机实训day3——点亮LED灯、闪烁LED灯(一)理论

内 容&#xff1a;编写代码实现LED灯的点亮功能 学 时&#xff1a;2学时 知识点&#xff1a;分析原理图、LED灯控制原理 重点&#xff1a;GPIO参数配置、LED原理图分析 难点&#xff1a;编写 GPIO参数配置函数、LED点亮函数 时间&#xff1a;2022年12月21日 9:00&#xff5e;…

实验1 数据库定义与操作语言实验

前言&#xff1a;实验本身并不是很难&#xff0c;照着实验指导书抄就行&#xff0c;不过注意有些sql语句和mysql语句是不相同的&#xff0c;需要进行一定的修改 数据集链接 实验1 数据库定义与操作语言实验 实验1.1 数据库定义实验 1.实验目的 理解和掌握数据库DDL语言&am…

指挥中心显示大屏类型简介

因工作需要&#xff0c;现在需要不断补充指挥中心建设过程中各种设备知识&#xff0c;怕被别人忽悠了也不知道&#xff0c;抓紧学习了解。今天学习大屏部分&#xff0c;目前来说&#xff0c;常见的显示大屏主要分为DLP拼接屏、LCD拼接屏和LED小间距大屏几种类型。 1、DLP大屏 …

【maven工程的pom.xml文件内部结构详解+maven工程的多层次依赖管理】

目录pom文件内部【结构详解】pom文件内部【依赖管理】1、依赖传递&#xff1a;2、依赖传递过程中&#xff0c;版本冲突&#xff1a;3、依赖传递过程中&#xff0c;对外隐藏主动断开&#xff1a;pom文件内部【依赖的作用范围】pom文件内部【结构详解】 <?xml version"…

【ARMv8 异常模型入门及渐进 11 - Linux 中断上下文 irq_enterirq_exit】

文章目录1.1 背景1.1.1 in_interrupt 定义1.1.2 irq_count 定义1.1.3 preempt_count 各域含义1.1.4 ARMv8 中断处理流程回顾1.1 背景 在 Linux 代码中经常会看到 WARN_ON(in_interrupt()); 或者 BUG_ON(in_interrupt()); 从名字可以看出这两句的含义是&#xff1a;如果当前处在…

运用手机多媒体之使用通知

文章目录使用通知将程序运行到手机上使用通知创建通知渠道通知的基本用法通知的进阶技巧setStyle()方法不同重要等级的通知渠道使用通知 将程序运行到手机上 在AS当中除了使用模拟器来运行我们的程序,还可以使用真机来运行我们写的程序想要将程序运行到手机上,首先需要将手机…

推荐系统学习笔记-推荐系统分布式离线训练

背景 在推荐、广告、搜索等互联网场景下&#xff0c;动则TB甚至PB级数据量。导致几乎不可能在传统单机环境下完成机器学习模型的训练。分布式机器学习训练成为称为唯一选择。 主要手段 • Spark MLlib • Parameter Server • Tensorflow Spark MLlib MLlib从功能上说与Sc…

如何利用地表温度遥感数据和气象资料计算农田地表水热通量

地表水热通量主要包括感热/显热通量和潜热通量&#xff0c;是陆-气交互以及水-热-碳循环研究的重要变量。其中&#xff0c;潜热通量是地表蒸散发的能量形式&#xff0c;对农业水资源管理、作物水分利用效率等非常关键。由于热红外遥感对地表干湿变化、以及农业干旱响应快速&…

WeakHashMap源码解析

WeakHashMap源码解析 简介 WeakHashMap 是一种 弱引用 map&#xff0c;内部的 key 会存储为弱引用&#xff0c;当 jvm gc 的时候&#xff0c;如果这些 key 没有强引用存在的话&#xff0c;会被 gc 回收掉&#xff0c;下一次当我们操作 map 的时候会把对应的 Entry 整个删除掉…

金融信息科技服务外包风险管理能力成熟度评估规范 学习笔记 附录下载地址

金融信息科技服务外包风险管理的范围 本标准规定了金融业信息科技服务外包风险管理能力成熟度评估体系以及对发包方和承包方的总体要求&#xff0c;分别对发包方、承包方的服务外包风险管理能力成熟度进行了分级定义&#xff0c;并规定了对发包方和承包方进行服务外包风险管理…

《OpenGL 模型》 渲染出帅气的暗影战士

模型Assimp流程网格模型效果Assimp 3D建模工具&#xff0c;可以让艺术家创建复杂的形状&#xff0c;Assimp库用于加载&#xff0c;如加载obj格式的文件到我们的程序之中&#xff0c;下载CMAKE用于构建该库&#xff08;会有很多问题&#xff09;&#xff0c;不过&#xff01;我…

【小程序】小程序代码的构成

目录 项目结构 1. 了解项目的基本组成结构 2. 小程序页面的组成部分 JSON配置文件 1. JSON 配置文件的作用 2. app.json 文件 3. project.config.json 文件 4. sitemap.json 文件 5. 页面的 .json 配置文件 6. 新建小程序页面 7. 修改项目首页 项目结构 1. 了解项…

别再用过时的方式了!全新版本Spring Security,这样用才够优雅!

基本使用 我们先对比下Spring Security提供的基本功能登录认证&#xff0c;来看看新版用法是不是更好。 升级版本 首先修改项目的pom.xml文件&#xff0c;把Spring Boot版本升级至2.7.0版本。 <parent><groupId>org.springframework.boot</groupId><art…