0% found this document useful (0 votes)
44 views4 pages

Lab3 201903482

This document contains a student's work on a lab assignment for an introductory Haskell programming course. It includes specifications for 8 functions related to characters, strings, and numbers. It also includes a proof by induction that a function checking for divisibility is partially correct.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Lab3 201903482

This document contains a student's work on a lab assignment for an introductory Haskell programming course. It includes specifications for 8 functions related to characters, strings, and numbers. It also includes a proof by induction that a function checking for divisibility is partially correct.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Author: Moyanah Otukile

Student ID: 201903482


Date: 2020/
Version of work: Lab4

***This piece of work was done under the University of Botswana policy. And I therefore affirm that this
was individual work. ***

1. Introduction to Haskell environment. See “getting started with Haskell” or


“Writing your first Haskell program”
Key tools:
 {editor, ghc interpretor}
 {:load, :cd, :!dir,:browse, … }

𝑒𝑑𝑖𝑡(𝑖𝑛𝑏𝑎𝑠𝑒, 𝑖𝑠𝑖𝑛, 𝑎𝑑𝑑)


𝑠𝑎𝑣𝑒_𝑝𝑟𝑖𝑣𝑎𝑡𝑒(𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛)
𝑔ℎ𝑐𝑖(: 𝑐𝑑, ∶ ! 𝑑𝑖𝑟, ∶ 𝑐𝑑, ∶ 𝑙𝑜𝑎𝑑, ∶ 𝑏𝑟𝑜𝑤𝑠𝑒)
𝑠𝑢𝑏𝑚𝑖𝑡(𝑝𝑟𝑜𝑔𝑓𝑖𝑙𝑒, 𝑑𝑜𝑐𝑓𝑖𝑙𝑒)

Specifications
4. Write a simple specification for each of the functions in question

a) Sumt(m,n) which gives the sum of first n natural numbers that are divisible by m. [Hint: may
assume m is a natural number.]
sumt : ℕ x ℕ → ℕ
sumt: (Integer m, Integer n): Integer
Purpose: computes natural numbers divisible by m up to n
check if m is divisible by n
Precondition: (n >= 0) && (m >= 1)
Post condition: m + sumt(n-1) iff [x|x <- [1...n]]

b) getChar(n) which gives the character of a given Unicode n.


getChar : ℕ → Char
getChar(integer i): Char n
Purpose: get the character at position i
Precondition: (i >= 0)
Post condition: UTF-8[0] iff 0 =< i && i =< maxPos "error" otherwise

c) getCode(c) which gives the Unicode of a given character c.


getCode : Char → String
getCode(Char c): String
Purpose: gives the Unicode of a given character c
Precondition: (i >= 0)
Post condition: getCode iff UTF-8[c] =getCode && "null" otherwise

d) isHex(c) which determines if the Unicode character c is a hexadecimal digit.


isHex: Char → Boolean
isHex(Char c): Boolean
Purpose: determines if the Unicode character c is a hexadecimal digit.
Prec: c != null
Postc: True iff 'Elem'['0'...['9']] v ['A'... 'F'] false otherwise

c isHex(c)

4 if c `elem` [0…9] || `elem` [A…F] then


True else false

j if c `elem` [0…9] || `elem` [A...F] then


True else false

e) prodt(n) which gives the product of the first n natural numbers. [Hint: the product identity is 1.]

prodt: ℕ -> ℕ

prodt(integer n):integer

Purpose: gives the product of numbers up to n

Precondition: n>=0
Postcondition:{n+ prodt ( n−1 ) iff n> 1

{1 otherwise

f) isIn(c,w) which determines if the character c is in the word w.

a.isIn:Char*String->Bool
isin(Char c,String w):Bool
purpose:checks if charater c in word w
precondition :c/=null && w/=null
postcondition:if c`elem` w then true else false

g) isHexWord

isHexWord:String ->Bool

isHexWord(String w):Bool

purpose:checks w has all hex character

precondition:w/=null

postcondition: :| True iff isHex(head(w)) = True ^ isHex(tail(w)) = True

| false otherwise

h)

4. let P(n) be isDiv(m,n)

Base Case

n = -1 : P(n) : isDiv(m,n) = True

n = 0 : P(n) : isDiv(m,n) = false


n = 1 : P(n) : isDiv(m,n) = True

Inductive Hypothesis

Assume p(k) : isDiv(m, k) Ɐk: ℤ | k >= n

Inductive Step

I will show that:

P(k + -1) isDiv(k + (-1))

isDiv(m,-1) ⋀ isDiv(m, k)

True ⋀ isDiv(m, k)

P(k + 0) isDiv(m, )

isDiv(m,) ⋀ isDiv(m, k)

True ⋀ isDiv(m, k)

P(k + 1) isDiv(k + 1)

isDiv(m,1) ⋀ isDiv(m, k)

True ⋀ isDiv(m, k)

Therefore isDiv(m,n) is partially correct.

You might also like