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

    • What is happening in the WNBA!    
    • RapidRAW 1.3.5 is out.
    • Microsoft Edge 139.0.3405.86 by Razvan Serea Microsoft Edge is a super fast and secure web browser from Microsoft. It works on almost any device, including PCs, iPhones and Androids. It keeps you safe online, protects your privacy, and lets you browse the web quickly. You can even use it on all your devices and keep your browsing history and favorites synced up. Built on the same technology as Chrome, Microsoft Edge has additional built-in features like Startup boost and Sleeping tabs, which boost your browsing experience with world class performance and speed that are optimized to work best with Windows. Microsoft Edge security and privacy features such as Microsoft Defender SmartScreen, Password Monitor, InPrivate search, and Kids Mode help keep you and your loved ones protected and secure online. Microsoft Edge has features to keep both you and your family protected. Enable content filters and access activity reports with your Microsoft Family Safety account and experience a kid-friendly web with Kids Mode. The new Microsoft Edge is now compatible with your favorite extensions, so it’s easy to personalize your browsing experience. Microsoft Edge 139.0.3405.86 changelog: Fixes Fixed an issue where MIP-protected PDF files from different sovereign cloud environments (including GCCH) failed to open and instead displayed the error message: “Need permissions. Contact the owner of the file to give you permissions.” Fixed an issue, which affected IE mode, including errors when displaying PDF files, running Java applets, and showing the Information Bar in IE mode. Improved reliability Fixed a browser crash that occurred on first launch when the BrowserSignin policy was enabled and configured to "Force (2) = Force users to sign-in to use the browser (all profiles)." Feature updates Open external links in another profile when recommended by external applications. When Microsoft Edge is set as the default browser to open external links from applications, Microsoft Edge must determine which profile to open the links. Users can control which profile to use through the “Default profile for external links” setting. Applications such as Microsoft Teams or Outlook can also recommend a profile for the links. Currently, the user setting is prioritized over application recommendations. With this feature, the application recommended profile is given priority, instead of the profile selected in the setting. Admins can control the availability of the feature using the EdgeOpenExternalLinksWithAppSpecifiedProfile policy. Note: This is a controlled feature rollout. If you don't see this feature, check back as we continue our rollout. Changes to Wallet in Microsoft Edge. Wallet is being phased out to support a streamlined experience within Microsoft Edge. This affects the Wallet feature in Settings and the Mini Wallet found by clicking the profile icon in the top banner. Users are directed to the new Passwords, Payment, and Personal Information management experience in Settings. Also, a new Password management experience is available in Settings. For more information, see Changes to Wallet in Microsoft Edge. Note: This is a controlled feature rollout. If you don't see this feature, check back as we continue our rollout. Introducing a new policy that can enable/disable Microsoft 365 Copilot Chat in Edge for Business from showing in the toolbar. Edge for Business now has a dedicated policy, Microsoft365CopilotChatIconEnabled, to enable and disable Copilot in Edge from showing in the Edge toolbar. When both this policy and HubsSidebarEnabled are configured, this policy takes precedence in determining whether Copilot appears in the toolbar. If this policy isn't configured and HubsSidebarEnabled is disabled, Copilot will remain hidden. In a future release, this policy is the sole control for managing Copilot's visibility in the toolbar. Real-time notifications for compromised passwords. Microsoft Edge is integrating an in-context password breach notification system. This feature proactively informs users if their saved login credentials have been compromised in known data breaches, enabling them to take immediate action to secure their accounts. Admins can control availability to this feature using the PasswordMonitorAllowed policy. Note: This is a controlled feature rollout. If you don't see this feature, check back as we continue our rollout. Edge Settings Improvements. Edge Settings is migrating to WebUI2 to boost page responsiveness and introducing a series of minor visual and content upgrades to improve overall usability and utility. This includes optimizing for concise wording of individual settings, simplifying the number of pages and reorganizing content, and creating a cohesive user interface. New Autofill Personal Information Settings Configuration. A web form field collection consent toggle will be available in Autofill settings (edge://settings/autofill/personalInfo). This allows users to consent to Microsoft Edge collecting web form field labels (e.g., "First Name," "Email") to improve Autofill suggestion accuracy. Only field labels are collected and not user-entered data. The web field labels are stored securely per Microsoft's privacy standards. This new setting is manageable via existing policies in Autofill (e.g., AutofillAddressEnabled), EdgeAutofillMlEnabled. AutofillAddressEnabledis the parent setting forEdgeAutofillMlEnabled. The EdgeAutofillMlEnabled policy is the parent of this new setting, thus turning off the EdgeAutofillMlEnabled policy turns off this setting. Web AI APIs for prompt and writing assistance. Microsoft Edge now implements the Writing Assistance APIs and the Prompt API (for Edge extensions) with a local language model, Phi-4-mini, that is built into the browser. These easy-to-use JavaScript APIs are made available via Edge flags (set to Enabled, by default only for the Summarizer and Prompt API for extensions) so that sites and extensions can apply AI capabilities on the web. The small language model is downloaded as the first time any of these APIs is used and later shared across all domains, serving local AI use-cases with reduced cost, network independence, and increased privacy (since data input to the model doesn't leave the user’s device). Admins can control the availability of these APIs via the GenAILocalFoundationalModelSettings policy. These APIs are currently not implemented in China. Read the announcement here, and feel free to provide feedback. Enhancements to Performance and Secure network. Browser essentials is now separated into two distinct experiences (Performance and Secure Network) - both available from the Settings and more menu (“…” on the menu bar). Reset Microsoft Edge enterprise sync. For users having problems syncing browsing data across other signed-in devices, they can reset sync data from the Microsoft servers via Edge Settings edge://settings/profiles/sync/reset. This option should only be used if the sync data is available on one of the user's devices or if they want to delete all sync data from the servers. Note: In Microsoft Edge 139, reset sync is enabled for users encountering a "No permissions" MIP error and in Microsoft Edge 140, reset sync is enabled for users encountering a "Service disabled" MIP error. Update to Microsoft AutoUpdate policy. The MAUEnabled policy allowed admins to continue using Microsoft AutoUpdate on macOS. Since Microsoft Edge now uses EdgeUpdate, the MAUEnabled policy is planned to be obsoleted in Microsoft Edge version 140. Policy updates / New policies EdgeOpenExternalLinksWithAppSpecifiedProfile - Prioritize App specified profile to open external links EnableUnsafeSwiftShader - Allow software WebGL fallback using SwiftShader MandatoryExtensionsForInPrivateNavigation - Specify extensions users must allow in order to navigate using InPrivate mode Microsoft365CopilotChatIconEnabled - Control whether Microsoft 365 Copilot Chat shows in the Microsoft Edge for Business toolbar OnSecurityEventEnterpriseConnector - Configuration policy for Microsoft Edge for Business Reporting Connectors Obsoleted policies KeyboardFocusableScrollersEnabled - Enable keyboard focusable scrollers (obsolete) SelectParserRelaxationEnabled - Controls whether the new HTML parser behavior for the SELECT element is enabled (obsolete) Download: Microsoft Edge (64-bit) | 180.0 MB (Freeware) Download: Microsoft Edge (32-bit) | 163.0 MB View: Microsoft Edge Website | Release History Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      harveycoleman123 earned a badge
      Week One Done
    • First Post
      EzraNougat earned a badge
      First Post
    • One Month Later
      westDvina earned a badge
      One Month Later
    • Community Regular
      Bern@rd went up a rank
      Community Regular
    • Week One Done
      Joey Solo earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      663
    2. 2
      +FloatingFatMan
      192
    3. 3
      ATLien_0
      154
    4. 4
      Xenon
      132
    5. 5
      wakjak
      98
  • Tell a friend

    Love Neowin? Tell a friend!