Advertisement
Guest User

Untitled

a guest
Feb 8th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.35 KB | None | 0 0
  1. namespace Lab2
  2.  
  3. // C:\Users\anton\OneDrive\Dokument\Programmering\FSkarp\DVA229Lab2
  4.  
  5. module List =
  6.  
  7.  
  8.  
  9.   // When implemented correctly, all functions in this module will be
  10.   // polymorphic. For example, mem will be given the following type:
  11.   //
  12.   //   val mem : 'a -> 'a list -> bool (where 'a has equality).
  13.   //
  14.   // The type annotations and descriptions that accompany each function are
  15.   // there to guide you in your work - you are not meant to constrain the
  16.   // functions to be defined for integers only.
  17.   //
  18.   // Do not worry! You will not have to do any extra work to make the functions
  19.   // polymorphic. F# uses a Hindley-Milner type system, where polymorphism
  20.   // is the default.
  21.  
  22.  
  23.  
  24.   // val mem : int -> int list -> bool
  25.   // Given an integer x and a list of integers xs, it returns true if
  26.   // xs contains x and false if it does not.
  27.   //
  28.   // Note:
  29.   //   * You cannot use builtin list functions.
  30.  
  31.  
  32.  
  33.   let rec mem x xs = match xs with
  34.                         | num::xs when num = x -> true
  35.                         | num::xs -> mem x xs
  36.                         | num::[] -> false
  37.                         | [] -> false
  38.                                            
  39.  
  40.   // val union : int list -> int list -> int list
  41.   // Given two lists, it returns their union.
  42.   // The output list cannot contain duplicates.
  43.   //
  44.   // Note:
  45.   //   * You cannot use builtin list functions.
  46.  
  47.  
  48.  
  49.   let rec union (xs : int list) (ys : int list) =
  50.     let zs = xs @ ys
  51.     match zs with
  52.     | z::zt when mem z zt -> union zt []
  53.     | z::zt -> z::(union zt [])
  54.     | [] -> xs
  55.  
  56. // union [1;2;3;2;1] [2;3;4;4;4]
  57.  
  58.   // val foo1 : int -> int list
  59.   // The function takes as input an integer x and an integer list xs.
  60.   // The function returns an integer list containing n repetitions of the
  61.   // value x, where n is the number of times that x occurs in xs.
  62.   //
  63.   // Examples:
  64.   //  foo1 1 [2;1;2;1;4;1] => [1;1;1]
  65.   //  foo1 4 [2;1;2;1;4;1] => [4]
  66.   //
  67.   // Note:
  68.   //   * You cannot use builtin list functions.
  69.  
  70.  
  71.  
  72.   let rec foo1 x xs =
  73.     match xs with
  74.     | z::zs when z = x -> z::(foo1 x zs)
  75.     | z::zs -> foo1 x zs
  76.     | [] -> []
  77.  
  78.  // foo1 2 [2;3;2;3]
  79.  
  80.  
  81.   // val foo2 : (int -> int -> bool) -> int -> int list
  82.   // The function takes as input a predicate function f, an integer x1
  83.   // and an integer list xs. The function returns an integer list ys
  84.   // containing all elements x2 in xs for which f x1 x2 is true. The elements
  85.   // in ys are ordered according to their order of appearance in xs.
  86.   //
  87.   // Examples:
  88.   //  let eq x y = x = y
  89.   //  let lt x y = x < y
  90.   //  let gt x y = x > y
  91.   //
  92.   //  foo2 eq 2 [1;2;4;2;5] => [2;2]
  93.   //  foo2 lt 2 [1;2;4;2;5] => [4;5]
  94.   //  foo2 gt 2 [1;2;4;2;5] => [1]
  95.   //
  96.   // Note:
  97.   //   * You cannot use builtin list functions.
  98.  
  99.  
  100.   let eq x y = x = y
  101.  
  102.   let lt x y = x < y
  103.  
  104.   let gt x y = x > y
  105.  
  106.   let rec foo2 f x xs =
  107.     match xs with
  108.     | h::t when f x h -> h::(foo2 f x t)
  109.     | h::t -> foo2 f x t
  110.     | [] -> []
  111.  
  112.     //foo2 eq 2 [1;2;4;2;5]
  113.     //foo2 lt 2 [1;2;4;2;5]
  114.     //foo2 gt 2 [1;2;4;2;5]
  115.    
  116.  
  117.  
  118.  
  119.   // Question:
  120.   //   Look at these function calls:
  121.   //     foo2 eq 2 [1;2;4;2;5]
  122.   //     foo2 lt 2 [1;2;4;2;5]
  123.   //     foo2 gt 2 [1;2;4;2;5]
  124.   //   How can you express the same computations without first defining
  125.   //   the functions eq, lt and gt?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement