Question

Recommended Posts

  • 0
  On 15/05/2014 at 15:36, Andre S. said:

Recursion provides very elegant solutions . . .

 

Although I'm aware of the inherently subjective nature of 'elegance', I would argue the exact opposite. I do see how someone could consider recursion elegant. I see it as ugly and it makes re-reading code more difficult, like when trying to understand another persons code. A methodology (or sometimes necessity) of programming being ugly in no way infers its lack of usability. I completely understand the usefulness of recursion and use it accordingly. I was simply stating that I don't like to use it when I don't have to and it feels sloppy.

  • 0

Well, for example, if I had to describe the Quicksort algorithm in plain English, I would say that it consists of

1) selecting a pivot element,

2) partitioning the collection between the elements greater than the pivot and those lesser than the pivot, and then

3) repeating the procedure on each partition until the the whole collection is sorted.

Hence if I had to translate in pseudo-code: 

function quicksort collection =
    pivot := selectpivot(collection)
    lesser := takeLesserThan(pivot, collection)
    greater := takeGreaterThan(pivot, collection)
    return [quicksort lesser] + [pivot] + [quicksort greater]
This is already basically correct, except that we're missing the terminating condition, i.e. we can't subdivide the list indefinitely:

function quicksort collection =
    if collection is empty
        return collection
    else
        pivot := selectpivot(collection)
        lesser := takeLesserThan(pivot, collection)
        greater := takeGreaterThan(pivot, collection)
        return [quicksort lesser] + [pivot] + [quicksort greater]
This still reads almost like English and is obviously correct.

Well, the equivalent Python code is remarkably similar (using list comprehensions and recursion): 

def quicksort(collection):
    if list == []: 
        return []
    else:
        pivot = collection[0]
        lesser = [x for x in collection[1:] if x < pivot]
        greater = [x for x in collection[1:] if x >= pivot]
        return quicksort(lesser) + [pivot] + quicksort(greater)
How would you implement this iteratively? Try it, and I doubt you'll come back and say it was more elegant than the 8 lines of code above.

(By the way, in a real functional language, the above boils down to 5 lines of code (using F#):

let rec quicksort = function
   | [] -> []                         
   | first::rest -> 
        let smaller,larger = List.partition ((>=) first) rest 
        List.concat [quicksort smaller; [first]; quicksort larger]
  • 0
  On 15/05/2014 at 22:24, Andre S. said:

Try it, and I doubt you'll come back and say it was more elegant than the 8 lines of code above.

 

<ass>

lst.sort()

</ass>

 

You consider 8 lines elegant. I consider 30 lines elegant if it clearly illustrates its intent. In this case yes, it's pretty nice. I hate needing to understand the logic of recursion as it's not immediately apparent to me. I go through it like this:

 

"Ok, so if this then call it again, the parameters would now... be... this?.... Yes, I think so... which now means when it calls it... It's this? Alright how is this terminated.... Ah when this value is..... This? I think?.... gah!"

 

I'm not as strong of a programmer as you are though, this I am certain.

  • 0
  On 15/05/2014 at 05:18, Torolol said:

well, chess alogirthm usually demands recursion,

on games that have finites moves like othello/reversi, using recursion would usually ensure the computer would win or at least draw.

 

I interviewed at Microsoft once... and decided to implement the solutions to the problems they gave me using recursion.

 

One of the Windows developers quickly reminded me that stack space in the kernel is limited and is pretty easy to overflow.

 

Something to think about.

  • 0
  On 15/05/2014 at 23:57, rfirth said:

I interviewed at Microsoft once... and decided to implement the solutions to the problems they gave me using recursion.

 

One of the Windows developers quickly reminded me that stack space in the kernel is limited and is pretty easy to overflow.

 

Something to think about.

That's what tail calls are for.

  • 0
  On 16/05/2014 at 02:46, Andre S. said:

That's what tail calls are for.

Can all recursive functions be converted to tail calls? Tak for example. And just because something is tail recursive doesn't mean it has predictable memory usage.
  • 0
  On 17/05/2014 at 13:48, simplezz said:

Can all recursive functions be converted to tail calls? Tak for example. And just because something is tail recursive doesn't mean it has predictable memory usage.

They have to be tail-recursive, meaning any recursive call has to be in tail position. Any recursive function can be converted to a tail-recursive function if necessary in continuation-passing style. Asynchronous programming relies heavily on that technique already, where you start a long-running task and sign the rest of the operation (a continuation) as a callback.

 

Tail call optimisation avoids the problems entailed (lol) by the creation of a stack frame for each call, but it's certainly no guarantee that the function even terminates, that's still up to the programmer.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • You cannot disable or fully delete Safari on either iOS or macOS. You can ignore it, just like you can with Edge, if that's what you want to do.
    • UniGetUI 3.3.1 by Razvan Serea UniGetUI is an application whose main goal is to create an intuitive GUI for the most common CLI package managers for Windows 10 and Windows 11, such as Winget, Scoop and Chocolatey. With UniGetUI, you'll be able to download, install, update and uninstall any software that's published on the supported package managers — and so much more. UniGetUI features Install, update and remove software from your system easily at one click: UniGetUI combines the packages from the most used package managers for windows: WinGet, Chocolatey, Scoop, Pip, Npm and .NET Tool. Discover new packages and filter them to easily find the package you want. View detailed metadata about any package before installing it. Get the direct download URL or the name of the publisher, as well as the size of the download. Easily bulk-install, update or uninstall multiple packages at once selecting multiple packages before performing an operation Automatically update packages, or be notified when updates become available. Skip versions or completely ignore updates in a per-package basis. Manage your available updates at the touch of a button from the Widgets pane or from Dev Home pane with UniGetUI Widgets. The system tray icon will also show the available updates and installed package, to efficiently update a program or remove a package from your system. Easily customize how and where packages are installed. Select different installation options and switches for each package. Install an older version or force to install a 32bit architecture. [But don't worry, those options will be saved for future updates for this package] Share packages with your friends to show them off that program you found. Here is an example: Hey @friend, Check out this program! Export custom lists of packages to then import them to another machine and install those packages with previously-specified, custom installation parameters. Setting up machines or configuring a specific software setup has never been easier. Backup your packages to a local file to easily recover your setup in a matter of seconds when migrating to a new machine UniGetUI 3.3.1 changelog: Adress executable corruption/integrity detection and semi-automatic resolution: UniGetUI will check for corruption issues. If found, the user will be prompted to repair them. The crash report will contain an integrity report In both cases, if UniGetUI detects an integrity violation and the UniGetUI installer is placed on the installation directory (which will be by default since this release), UniGetUI will ask the user to confirm to start an automated reinstall process. UniGetUI can be reinstalled from Windows Apps and Features -> UniGetUI -> Modify. This will force a reinstall Fixed crashes and issues with GitHub cloud backup and GitHub login Fixed an issue where certain in-app popups wouldn't show the acrylic background properly Migrated to AppSdk Titlebar, and removed WinUIEx as a dependency Improvements to UniGetUI Elevator Fix a few UI crashes and deadlocks involving loading dialogs Reverted a Toolbar UI change that combined different options into a menu. Now the main action has its own button again Other fixes and improvements What's Changed Update icons and screenshots from the excel file by @github-actions[bot] in #3884 Load translations from Tolgee by @martinet101 in #3937 Download: UniGetUI 3.3.1 | 53.3 MB (Open Source) Links: WingetUI Home Page | GitHub | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • You have reported this issue at least once into their feedback hub?
    • You can disable Chrome on Android and forget that it even exists, you can do the same for Safari in Apple's operating systems. Only ChromeOS is tied with Chrome because the whole OS is Chrome. Safari doesn't have dominant market share but Apple allows you to disable Safari. So why exactly we can't have the same with Edge and only EU Windows users can uninstall Edge? There isn't any valid reason having Edge, the browser, needed for any other app, for widgets etc. That is what Edge webview2 is for.
    • China throws a wrench into Nvidia's H20 comeback plan by Paul Hill China’s Cyberspace Administration (CAC) has summoned Nvidia over alleged security vulnerabilities in its H20 chips. The CAC said that the chips have serious security issues with specific concerns over tracking and positioning and remote shutdown technologies. This development poses a big hurdle for Nvidia just as H20 sales were set to resume after the US reversed a ban that was first imposed in April. According to CNBC, Nvidia had already taken a $4.5 billion writedown on the unsold H20 inventory in May and said sales in its last financial quarter would have been $2.5 billion if export curbs could have been avoided. The CAC is concerned about the chips because of calls from US lawmakers for mandatory tracking features on advanced chip exports. For example, the proposed US Chip Security Act from Senator Tom Cotton and others would require location reporting and remote modification capabilities. If these backdoors have already been baked in, or get added in the future, China would view them as a threat to national security and data privacy. US lawmakers argue that these tracking capabilities are needed to prevent chip smuggling to ensure compliance with export controls which also apply to China. Given its actions, the CAC has reason to believe that the tracking technology is already present. Nvidia CEO Jensen Huang recently visited the Chinese capital, Beijing, to signal the company’s commitment to the Chinese market and announced the resumption of sales of the H20. It is believed that Nvidia placed new orders for 300,000 H20 chipsets with TSMC to meet the anticipated demand. Given the CAC’s decision, it could mess up Nvidia’s plans and force it to consider new ways to navigate US export policies and retain its lucrative China market share. The US is keen to hamper China's AI efforts so that it maintains a lead in the AI race. By limiting access to powerful hardware, it forces China to make more efficient models, or develop powerful hardware itself, slowing down its progress.
  • Recent Achievements

    • Week One Done
      whiloh earned a badge
      Week One Done
    • Week One Done
      memnoch earned a badge
      Week One Done
    • First Post
      UAVXP earned a badge
      First Post
    • Dedicated
      Xinotema earned a badge
      Dedicated
    • Rookie
      MrNukes went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      664
    2. 2
      ATLien_0
      207
    3. 3
      Xenon
      133
    4. 4
      neufuse
      124
    5. 5
      Michael Scrip
      122
  • Tell a friend

    Love Neowin? Tell a friend!