Docsity

EECS 468 Fall 2023 ICPs, Assignments of Computer Science

Toggle sidebar
bg1
Author: Mikaela Navarro
KU ID: 2998217
Date Created: 11/12/23
Program Name: Assignment 7
Description: Haskell functions for replicate, perfects, find, positions, and scalarproduct
Inputs: number of replications, element to replicate, number of perfects, element of find, list of
pairs to find, element to look for position, list to look at for position, and two lists of numbers for
scalarproduct
Outputs: list of replicated elements, list of perfect numbers, list of found values, list of index
positions, and a value of the scalar product
Collaborators: ChatGPT and an online haskell compiler (tutorialspoint)
Date Updated: 11/13/23
-- the type signature of the function takes an integer and a value (a), then it will result in a list of
the value repeated the number of times by the integer
replicate' :: Int -> a -> [a]
-- the replicate function is defined by taking in 'n' and 'x' as the arguments, and by using a list
comprehension, for every x that will appear, it will repeat it until the list is the length of n
replicate' n x = [x | _ <- [1..n]]
-- the type signature of the function takes an integer and result in a list of integer(s)
perfects :: Int -> [Int]
-- the perfects function is defined by taking in 'n' as the argument, and by using a list
comprehension, for every x (integer that will appear) it will look at the numbers of x from 1 to x-1
(y) and check if the remainder of y/[1..x-1] is zero, if it is zero, then the sum of each y should be
the same number as x, then it will be in the list
perfects n = [x | x <- [1..n], x == sum [y | y <- [1..x-1], x `mod` y == 0]]
-- the type signature of the find function takes a key and makes a table, and returns a of the
value(s) that have the key
find :: Eq a => a -> [(a,b)] -> [b]
-- the find function takes in a key and table, look for the pairs that have the key, and return a list
with those corresponding values
find key table = [value | (k,value) <- table, k == key]
-- the type signature of the positions function is taking an element and a list, and return a list of
integers
positions :: Eq a => a -> [a] -> [Int]
-- the positions functions takes in the element to look for, and a list to look into, use the zip
function to pair the elements of the list and its index, then use the find function to find the
indexes that has the element as a list
positions x xs = find x (zip xs [0..])
pf2

Partial preview of the text

Download EECS 468 Fall 2023 ICPs and more Assignments Computer Science in PDF only on Docsity!

Author: Mikaela Navarro KU ID: 2998217 Date Created: 11/12/ Program Name: Assignment 7 Description: Haskell functions for replicate, perfects, find, positions, and scalarproduct Inputs: number of replications, element to replicate, number of perfects, element of find, list of pairs to find, element to look for position, list to look at for position, and two lists of numbers for scalarproduct Outputs: list of replicated elements, list of perfect numbers, list of found values, list of index positions, and a value of the scalar product Collaborators: ChatGPT and an online haskell compiler (tutorialspoint) Date Updated: 11/13/ -- the type signature of the function takes an integer and a value (a), then it will result in a list of the value repeated the number of times by the integer replicate' :: Int -> a -> [a] -- the replicate function is defined by taking in 'n' and 'x' as the arguments, and by using a list comprehension, for every x that will appear, it will repeat it until the list is the length of n replicate' n x = [x | _ <- [1..n]] -- the type signature of the function takes an integer and result in a list of integer(s) perfects :: Int -> [Int] -- the perfects function is defined by taking in 'n' as the argument, and by using a list comprehension, for every x (integer that will appear) it will look at the numbers of x from 1 to x- (y) and check if the remainder of y/[1..x-1] is zero, if it is zero, then the sum of each y should be the same number as x, then it will be in the list perfects n = [x | x <- [1..n], x == sum [y | y <- [1..x-1], x mod y == 0]] -- the type signature of the find function takes a key and makes a table, and returns a of the value(s) that have the key find :: Eq a => a -> [(a,b)] -> [b] -- the find function takes in a key and table, look for the pairs that have the key, and return a list with those corresponding values find key table = [value | (k,value) <- table, k == key] -- the type signature of the positions function is taking an element and a list, and return a list of integers positions :: Eq a => a -> [a] -> [Int] -- the positions functions takes in the element to look for, and a list to look into, use the zip function to pair the elements of the list and its index, then use the find function to find the indexes that has the element as a list positions x xs = find x (zip xs [0..])

-- the type signature of the scalarproduct function takes in two lists of numeric type, and returns a value scalarproduct :: Num a => [a] -> [a] -> a -- the scalarproduct function takes in the two lists, use the zip function to make pairs from the two lists with the same index, take the pair and multiply them and put in a list, then return the sum of the products in the list scalarproduct xs ys = sum ([x * y | (x,y) <- zip xs ys]) -- perform the functions above and test them main = do let replicate_result = replicate' 5 "test code" print replicate_result let perfects_result = perfects 9000 print perfects_result let find_result = find 'c' [('a',1),('b',2),('c',3),('b',4),('c',25)] print find_result let positions_result = positions 1 [1,0,0,1,0,1,1,0] print positions_result let scalarproduct_result = scalarproduct [-1,2,3][-4,-5,6] print scalarproduct_result [1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... ["test code","test code","test code","test code","test code"] [6,28,496,8128] [3,25] [0,3,5,6] 12