Skip to content

Instantly share code, notes, and snippets.

@fardinshikhiyev
Created March 21, 2018 22:45
Show Gist options
  • Save fardinshikhiyev/20e149eaea32a3efde136bb49e089be2 to your computer and use it in GitHub Desktop.
Save fardinshikhiyev/20e149eaea32a3efde136bb49e089be2 to your computer and use it in GitHub Desktop.
module ex51
import StdEnv
// Exercises
// 1. Write a recursive function that computes the n-th multiple of an x.
// I am not sure if this means power or multiplication, I did for multiplication
f1 :: Int Int -> Int
f1 x 1 = x
f1 x n
| n == 0 = 0
= x + f1 x (n-1)
//Start = f1 4 3 // 12
// 2. Add 2 to every odd number of a list, and subtract 2 from every even number.
f2 :: [Int] -> [[Int]]
f2 [] = []
f2 x = [map (\k=k-2)(filter(isEven) x), map (\j=j+2) (filter(isOdd) x)]
//Start = f2 [1..10] // [[0,2,4,6,8],[3,5,7,9,11]]
// 3. Compute the triple of the negative elements of a list up to the first positive number.
f3 :: [Int] -> [Int]
f3 [] = []
f3 [h:t]
| h < 0 = [h*3 : f3 t]
= []
//Start = f3 [-3,-4,5,-2,5,-7] // [-9,-12]
// 4. Write a function that keeps the non-zero elements of a list and then multiply by 2 every element.
f4 :: [Int] -> [Int]
f4 [] = []
f4 x = map (\x=x*2) (filter(not0) x)
not0 :: Int -> Bool
not0 x // I didn't find a way to give this condition as an argument to filter
| x <> 0 = True
= False
//Start = f4 [1,0,2,3,0,4,5,0,12,13,0,0,0,10,0] // [2,4,6,8,10,24,26,20]
// 5. Write a function for the square, the cube, and so on up to the n-th power of a number,
// so that increasing powers of a number are obtained in a list.
f5 :: Int Int -> [Int]
f5 x y
| y == 0 = [1]
= f5 x (y-1) ++ [x^y]
//Start = f5 2 8 // [1,2,4,8,16,32,64,128,256]
// 6. Replicate n>0 times a list.
f6 :: Int [Int] -> [[Int]]
f6 x y
| x < 0 = abort "no way dude"
| x == 0 = []
= [y] ++ f6 (x-1) y
//Start = f6 2 [1..5] // [[1,2,3,4,5],[1,2,3,4,5]]
// 7. Insert 0 at the middle of each sublist.
// I assume that middle means length x/2
f7 :: [[Int]] -> [[Int]]
f7 [] = []
f7 [h:t] = [take mid h ++ [0] ++ drop mid h : f7 t]
where mid = h!!(length h/2)
//Start = f7 [[1,2,3,4,5], [1,2,3,4,5]] // [[1,2,3,0,4,5],[1,2,3,0,4,5]]
// 8. Extract the elements smaller then the head element of a list.
f8 :: [Int] -> [Int]
f8 [] = []
f8 x = filter ((>)(hd x)) x // as an alternative to hd x, x!!0 also works
//Start = f8 [4,5,6,7,2,1,3,9] // [2,1,3]
//Start = filter ((<)4) [1..10]
// 9. Eliminate in a list the sublists that are longer then 10.
f9 :: [[Int]] -> [[Int]]
f9 [] = []
f9 x = filter (shorterThan10) x
shorterThan10 :: [Int] -> Bool
shorterThan10 x = length x < 10 // Again, couldn't find a way to pass this inside filter
//Start = shorterThan10 [1..5] //True
//Start = f9 [[1..13], [1..15], [1..5], [1..19], [1..4], [1..3]] // [[1,2,3,4,5],[1,2,3,4],[1,2,3]]
// 10. Compute the greatest common divisor in a recursive function.
// Assuming that the second argument is greater than the first one,
// and both of them are greater than zero.
f10 :: Int Int -> Int
f10 x y
| x == y = x
| y < x = 1
= f10 x (y-x)
//Start = f10 5 17 //1
//Start = f10 4 64 //4
// 11. Compute the Euler number aproximation in n steps: e = 1/0! + 1/1! + 1/2! + 1/3! + ...
// formula is (1 + 1/n)^n (found on google)
f11 :: Int -> Real
f11 x = ((1.0 + (1.0/toReal(x)))^toReal(x))
//Start = f11 100000 // 2.7182682371745
Start = f11 10000000000000000 // 2.71828182763131
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment