Skip to content

Calling getDivisor instead of mkDivisor in Unit1 reader #4

@Okan1701

Description

@Okan1701

In the Unit1 reader, you have the following code example:

let getDivisors =
  fun n ->
    let rec mkDivisors =
      fun n i ->
        if i > n then
          ""
        else
          if (n % i = 0) then
            i + " " + (getDivisors n (i + 1))
          else
            getDivisors n (i + 1)
    mkDivisors n 1

However this code will not compile. Calling getDevisors within mkDivisors will result in a compile error about getDivisor not being defined in the body of mkDivisor. I believe the correct thing is to call mkDevisor instead of getDevisor.

My IDE also complained about i + " " + (mkDivisors n (i + 1)) resulting in a type error. I would need to do i.ToString() + " " + (mkDivisors n (i + 1)) instead

So the correct version would be:

    let getDevisors =
      fun n ->
        let rec mkDevisors =
          fun n i ->
            if i > n then
              ""
            else
              if (n % i = 0) then
                i.ToString() + " " + (mkDevisors n (i + 1))
              else
                mkDevisors n (i + 1)        
        mkDevisors n 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions