Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 135219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:45:15+00:00 2026-05-11T06:45:15+00:00

I am wondering how to do this in a functional programming language. Maybe F#

  • 0

I am wondering how to do this in a functional programming language. Maybe F# or Haskell.

Can someone show me an example without using any function calls except for find and rfind?

This function finds the next slash using i as the num of slash (<0 for backwards).

size_t findSlash(const char *sz, size_t i) {     std::string s = sz;     size_t a, b, c, n, ai = abs(i), pos=0;     for (n=0; n<ai; n++)     {         if (i<0)         {             a = s.rfind('\\', pos);             b = s.rfind('/', pos);         }         else         {             a = s.find('\\', pos);             b = s.find('/', pos);         }         if (a==-1u)         {             if (b==-1u)                 return pos;             c = b;         }         else if (b==-1u)             c = a;         else             c = min(a, b);         pos = c+1;     }     return c; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:45:15+00:00Added an answer on May 11, 2026 at 6:45 am

    In first, your code is broken. size_t is unsigned type and never can be i<0.

    In second, your code is ugly std library misuse and ineffective. There should be used regular expression library or such or use handcrafted scanner. Resulting code is far cleaner and faster. For example (I have not been using C for many years but below code crafted in 10 minutes works.):

    size_t findSlash(const char *sz, int i) {     const char *s = sz;     if (i<0) {         for(;*s;s++);         for(;;s--){             if(s<sz) return -1;             if((*s == '/') || (*s == '\\'))                 if(! ++i) break;         }     }     else {         for(;;s++){             if(! *s) return -1;             if((*s == '/') || (*s == '\\'))                 if(! i--) break;         }     }     return s-sz; } 

    I haven’t used to write Haskell or F# but for example below code in Erlang should state as example how to do it in functional language:

    findslash(L, I) when is_list(L), is_integer(I) ->     if  I<0  ->             case findslash(lists:reverse(L), -1*I - 1, 0) of                 none -> none;                 X -> length(L) - X - 1             end;         I>=0  -> findslash(L, I, 0)     end.  findslash([H|_], 0, X) when H=:=$/; H=:=$\\ -> X; findslash([H|T], I, X) when H=:=$/; H=:=$\\ ->     findslash(T, I-1, X+1); findslash([_|T], I, X) -> findslash(T, I, X+1); findslash([], _, _) -> none. 

    My attempt in Haskell with error checking and keeps laziness for i>=0:

    findSlash :: String -> Int -> Maybe Int findSlash str i   | i <  0 = reversed (_findSlash (reverse str) (-1*i-1) 0)   | i >= 0 = _findSlash str i 0     where       reversed Nothing  = Nothing       reversed (Just a) = Just ((length str) - a - 1)       _findSlash (x:xs) i n         | x == '/' || x == '\\' = if i==0 then Just n else _findSlash xs (i-1) (n+1)         | True                  =                          _findSlash xs  i    (n+1)       _findSlash []     _ _     = Nothing 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering: can infinite loops be done in functional programming? example: when using
Just wondering if anyone can optimize this usortMonths() function to be better? Basically I
I'm reading Simon Thompson's Haskell: The Craft of Functional Programming , and I'm wondering
Being relatively new to functional programming, I expend lots of energy wondering is this
I'm new to Haskell (and functional programming in general) and was wondering how I
I'm just wondering if there is a best case to write this code: $('#set_duration_30').click(function(event)
I was wondering if this was possible? I'm not familiar with using windows command
R is a functional programming language , and one of it's primary benefits is
In functional programming, functions are regarded as entities, and can be passed around as
I was wondering if there are functions in any programming languages that allow you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.