Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • CS-210 Functional programming CS-210 Functional programming
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 9
    • Issues 9
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • Deployments
    • Deployments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Metrics
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • Repository
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • lamp
  • CS-210 Functional programmingCS-210 Functional programming
  • Issues
  • #63
Closed
Open
Issue created Oct 07, 2021 by Bryan Marcel Gotti@gotti

Calling an anonymous function from its own body

Hello,

I had a question regarding recursion calls to anonymous function.

Suppose I have an anonymous function and I want to make it recursive:

x => if x == 0 then 0 else x + callToItself(x - 1)

Obviously, callToItself is not valid syntax, but is there a way to implement this in Scala ? I think it would be nice if we could do that without having to use named functions like this, which clutters the code in my opinion:

x => 
    def f(x): Int => Int = if x == 0 then 0 else x + f(x - 1)
    f(x)

I've thought of using this.apply but it does not compile either.

Assignee
Assign to
Time tracking