联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2020-04-18 11:24

Homework 1 (Haskell)

Please follow carefully all of the following steps:

1. Prepare a Haskell (or literate Haskell) file (ending in .hs or .lhs, respectively) that compiles without errors

in GHCi. (Put all non-working parts in comments.)

2. Submit one solution per group (each group can have up to 5 members) through Canvas.

Late submissions will not be accepted. Do not send solutions by email.

Insert the following definitions at the top of your file.

import Data.List (nub,sort)

norm :: Ord a => [a] -> [a]

norm = sort . nub

Exercise 1. Programming with Data Types

Here is the definition of a data type for representing a few basic shapes. A figure is a collection of shapes. The type

BBox represents bounding boxes of objects by the points of the lower-left and upper-right hand corners of the smallest

enclosing rectangle.

type Number = Int

type Point = (Number,Number)

type Length = Number

data Shape = Pt Point

| Circle Point Length

| Rect Point LengthLength

deriving Show

type Figure = [Shape]

type BBox = (Point,Point)

(a) Define the function width that computes the width of a shape.

width :: Shape -> Length

For example, the widths of the shapes in the figure f are as follows.

f = [Pt (4,4), Circle (5,5) 3, Rect (3,3) 7 2]

> map width f

[0,6,7]

(b) Define the function bbox that computes the bounding box of a shape.

CS 381, Spring 2020, Homework 1 2

bbox :: Shape -> BBox

The bounding boxes of the shapes in the figure f are as follows.

> map bbox f

[((4,4),(4,4)),((2,2),(8,8)),((3,3),(10,5))]

(c) Define the function minX that computes the minimum x coordinate of a shape.

minX :: Shape -> Number

The minimum x coordinates of the shapes in the figure f are as follows.

> map minX f

[4,2,3]

(d) Define a function move that moves the position of a shape by a vector given by a point as its second argument.

move :: Shape -> Point -> Shape

It is probably a good idea to define and use an auxiliary function addPt :: Point -> Point -> Point, which

adds two points component wise.

(e) Define a function alignLeft that transforms one figure into another one in which all shapes have the same

minX coordinate but are otherwise unchanged.

alignLeft :: Figure -> Figure

Note: It might be helpful to define an auxiliary function moveToX :: Number -> Shape -> Shape that changes

a shape’s position so that its minX coordinate is equal to the number given as first argument.

(f) Define a function inside that checks whether one shape is inside of another one, that is, whether the area

covered by the first shape is also covered by the second shape.

inside :: Shape -> Shape -> Bool

Hint: Think about what one shape being inside another means for the bounding boxes of both shapes.

Note that this remark is meant to help with some cases, but it doesn’t solve all.


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp