Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-8081-

Functional Programming

Name: Anonymous 2007-12-05 16:29

Hi /prog/, I needs help.

Could people give me some code in a functional language, specifically, I'd like some Haskell and OCaml. Lisp is also welcome. Other functional languages are also welcome (and appreciated), though I'm most interested in the mentioned ones.

The code snippet I'm looking for is to convert a string to uppercase, but I want the code to do this manually (so no calling some magical toupper function or whatever).
That means, this Haskell code is not valid, since it uses toUpper (though, if you define toUpper, I suppose I'll count it as valid).

import Data.Char
s = "alphaBETA"
upper = map toUpper s

Preferably I'd like code which does not use map or other such constructs (though if thats too dificult, then ignore this requirement). Also, if possible, I'd like code that converts the string both in place and by constructing a new string.


Basically, I want to see different approaches to doing this in different languages. All serious replies are appreciated. Trolls appreciated only if they make me laugh.

Name: Anonymous 2007-12-05 16:32

fib = 1 : 1 : zipWith (+) fib (tail fib)

Name: Anonymous 2007-12-05 16:36

>>2
Is that Haskell?

Name: Anonymous 2007-12-05 16:38

Other functional languages are also welcome (and appreciated), though I'm most interested in the mentioned ones.

There are no other functional languages besides Lisp, Haskell and OCaml.

Name: Anonymous 2007-12-05 16:41

>>4
Java has functions, I would call it functional.

Name: Anonymous 2007-12-05 16:43


#include <stdio.h>
#include <stdlib.h>

char to_upper( char foo )
{
    return foo >= 'a' && foo <= 'z' ? foo + ('A' - 'a') : foo;
}

char to_lower( char foo )
{
    return foo >= 'A' && foo <= 'Z' ? foo + ('a' - 'A') : foo;
}

void map_str( char * str, char (func)(char) )
{
    if ( !*str )
    {
        return;
    }

    *str = func(*str);
    map_str( ++str, func );
}


void map_str_to( const char * str, char * dst, char (func)(char) )
{
    if ( !*str )
    {
        return;
    }

    *dst = func(*str);
    map_str_to( ++str, ++dst, func );
}


int main( int argc, char ** argv )
{
    char convert[] = "ZERO 1 infinity";
    char *converted = malloc(sizeof(convert)+1);
    map_str( convert, to_upper );
    puts( convert );
    map_str_to( convert, converted, to_lower );
    puts( converted );
    return 0;
}

Name: Anonymous 2007-12-05 16:43

>>5
Java also functions. So it must be functional.

Name: Anonymous 2007-12-05 16:46

int *compose(int(*f)(int),int(*g)(int))(int){
 int ret(int n){
  return f(g(n));
 }
 return ret;
}

Name: Anonymous 2007-12-05 17:00

>>6
Doesn't work for Unicode, bitch.

Name: Anonymous 2007-12-05 17:05

>>9

    .    //            _/_,∠__     
        /.:/     '"  ̄ ̄ ヘ     __ ¨.二ニ=-
        {::{  /: : : : : : : : : : : : :\: : : : : : : : : :`丶、
         ヾ /.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:ヽ.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:ヽ 
         >′.::.::.::.::.::.::.::.::.ヽ.::.::.::.::.::. \-.::.::.::.::.:\_.::.::.\
    .     /.::.::.::/ :.::.::.::.::.::.:: |.::.::.::.::.::.::.|::\.::.\.::.::.:\`\
        |.::.:: /.::.::.::.::.::/|⌒:j.::.::.:.:::.::.:: |.::| _\.::.ヽ.::.::. ヽ 
        |.::.:/.::.:: /.::.::.::.: | .:/ \.::.::.::.::.:レチ斤| ト.::.',.::.::.::.:l
        |.:/.:,'.::.::|.::.::.::.::/|:/__  ヽ.::.::.::|ち:::| {.::?\.::.j 
         Y.:/! .:: l.::.::.: / ィチ行   ヽ.::.| Vヒi ',::.l      So what, you weeaboo.
         レ.::|.::.::.|.::.::.ハfヘノ:::::ハ   ∨    ・}.::.l    
         lrーゝ、∧.::.l∧ V;少''     _'    イ.::.:|. 
         j \ \∨.::. ヽ, -、    こ.ノ ∠l | ::.:
    .     ,'.::.::.( ̄`ヾー、_/ /、 __ ///}│ .:l 
    .     /.::.::.: ( ̄ ヽノ    {\   ハ /V/│.::.!
       /.::.::.::.:r'¨`ーt'   ノ.::. \ー‐'´| 川{ ∨リ  
    .  / :.::.::.::. `ト ー′  イ \.::ヽ\ | // ノ 
     /.::.::.::.::.::.:人ゝ、_   lノ ハ.:.:}∨ヽ!シイ 
    /.::/.:::.:::.::.::〃 \ \/ /) V二ニ¨´¨`ヽ
    ::/|.::/.:::.::.::{{   \三/人    /  }  \ 
    / V.:::.:::.:::.:::ヾ      イ  `  (   {`ヽ、_ >

Name: Anonymous 2007-12-05 17:09

>>10
Moonspeak is not the only language that uses Unicode. Plenty of other equally worthless languages use it too. Personally, I fully support intentionally breaking applications and websites for non-English speakers. It gives them more motivation to learn English.

Name: Anonymous 2007-12-05 17:28

>>11
Enjoy your λack of proper symbols in programming λanguages.

Name: Anonymous 2007-12-05 17:34

It doesn't matter because women prefer cunnilingus anyway you dumb stupidhead

Name: Anonymous 2007-12-05 17:44

>>13
brilliant troll

Name: Anonymous 2007-12-05 17:47

thanks. it was so organic. the urge to paste it in just came through me like a spirit guide

Name: Anonymous 2007-12-05 18:12

>>1
By disallowing map you're basically disallowing functional style, and by disallowing the built-in upper-case function you're just asking for incomplete upcase implementations that don't handle Unicode characters or show off the language in question (unless you're just wondering how to add 32 to a number in various languages). I'm not sure what you hope to learn from asking such a stupid question.

But here's Lisp anyway. This is the usual style.

(defun my-string-upcase (string)
  (let ((a ""))
    (tagbody
     capo
       (let ((c (elt string 0)))
     (setf string (subseq string 1))
     (if (and (char< c #\A) (alpha-char-p c))
         (setf a (concatenate 'string
              a (string (code-char (+ (char-code c) 32)))))
         (setf a (concatenate 'string a (string c)))))
       (unless (= (length string) 0) (go capo)))
    a))

And Lisp isn't a functional language. It's the multi-paradigm language.

Name: Anonymous 2007-12-05 18:14

>>16
Dang. Tab issues.

Name: Anonymous 2007-12-05 18:16

What does tagbody and capo mean

Name: Anonymous 2007-12-05 18:16

>>16
Wait... that's not right.

Name: Anonymous 2007-12-05 18:18

>>16
CL-USER> (my-string-upcase "test")
"test"

I lol'd heartily.

Name: Anonymous 2007-12-05 18:19

>>16
Here's an optimized version of your code:

(defun my-string-upcase (string)
    string)

Name: Anonymous 2007-12-05 18:22

>>1
converts the string in place destructively in lisp

(map-into s #'to-upper s)

in python I guess you could write something simpler like

def mapInto(c, f):
    for i in xrange(c.length):
         c[i] = f(c[i])

now you can write something like

mapInto("alphaBETA", upper)

Name: Anonymous 2007-12-05 18:23

>>22
You didn't read >>1, did you?

Name: Anonymous 2007-12-05 18:24

Preferably I'd like code which does not use map or other such constructs (though if thats too dificult, then ignore this requirement).
besides, I just wanted to post something in the code in place. I just didn't define the upper functions.

Name: Anonymous 2007-12-05 18:25

(defun my-string-upcase (string)
  (let ((a "")
    (c))
    (tagbody
     capo
       (setf c (elt string 0))
       (setf string (subseq string 1))
       (if (and (char> c #\Z) (alpha-char-p c))
       (setf a (concatenate 'string
                a (string (code-char (- (char-code c) 32)))))
       (setf a (concatenate 'string a (string c))))
       (unless (zerop (length string)) (go capo)))
  a))

fix'd

Name: Anonymous 2007-12-05 18:27

>>25
Is there a reason you use tagbody?

Name: Anonymous 2007-12-05 18:32

>>26
For proper GOTO style.

Name: Anonymous 2007-12-05 18:35

>>25
[code]    upper(string)[/code[
fix'd

Name: Anonymous 2007-12-05 18:37

Fuck proper GOTO style, I want GTFO style!

YEAH!!!

Name: Anonymous 2007-12-05 18:39

((x>='a')&&(x<='z'))?x&223:x

Name: Anonymous 2007-12-05 18:40

>>28
Way to not follow the instructions. That would be (string-upcase string), in any event.

Name: Anonymous 2007-12-05 19:05

And so life imitates art.

Name: Anonymous 2007-12-05 19:12

>>31
Enjoy your lisp.

Name: Anonymous 2007-12-05 19:30

>>33
(thank you)

Name: Anonymous 2007-12-05 21:56

>>33
(domo 'arigato)

Name: Anonymous 2007-12-05 22:07

var s = 'alphaBETA';
s = ''['toUpperCase']['apply'](s);

Name: Anonymous 2007-12-05 22:08

>>36
you could just do
s=s.toUpperCase();
but that's boring.

Name: Anonymous 2007-12-05 22:19

>>16, OP here.
Well, using map is fine, but I wanted to see alternatives (different ways to loop through each character in a string, check is it lowercase and if yes, add 32 to that character, then finally returning the new string).
As for why, I just used it as a random piece of sample code, anything that contained some sort of loop, some sort of codnition and some sort of modification or construction of data would be fine. What I am trying to do is figure out what fundamental constructs can be used to do most kinds of operations, things like map, fold, filter and so on.

Name: Anonymous 2007-12-05 23:50

>>38
Uh, ``fundamental constructs'' are relative to the language. You really shouldn't be using loops in functional languages, unless you have to.

Name: Anonymous 2007-12-06 0:31

Name: Anonymous 2007-12-06 0:32

>>39
GOTO is okay, right?

Name: Anonymous 2007-12-06 0:40

>>41
what functional language has goto?

Name: Anonymous 2007-12-06 0:45

>>42
Lisp!

Name: Anonymous 2007-12-06 1:02

>>41
Not really, no, unless we're talking about the recursion.

Name: Anonymous 2007-12-06 3:48

>>39
Well, no, not loops, but most algorithms still require them. Ok, in a functional language you would use recursion, or a construct like map, but they are still loops really. Thats what I meant by loops, I didn't mean you're standard for or while loops as seen in imperative languages.

By fundamental constructs I meant for functional languages specifically. From what I've seen of Haskell, mercury and OCaml (the functional aspect of OCaml anyway), their basic constructs are all more or less the same. When using Lisp functionally, to me anyway, it's constructs seem pretty similar too. Maybe I'm wrong, but then, thats why I posted this.

Basically, I'm wondering what are the least amount of constructs a functional language needs for it to be complete. Or at least, whats the least amount of construct any language needs, regardless of paradigm, but I wanted to deal with functional languages first.
By complete, I don't mean turing complete, but rather what a language needs for it to be considered useful.


I find it funny that I actually did learn something from this thread and, in fact, from /prog/ in general. And the trolls just keep it entertaining.

Name: Anonymous 2007-12-06 5:42

>>45
Ah, and so life imitates art.

Name: Anonymous 2007-12-06 5:51

>>46
I lol'd. Best application of that meme yet. Well done, sir.

Name: Anonymous 2007-12-06 6:04

>>45
Case in point, Brainfuck is turing complete, and yet highly useless (practitioners of bf need not apply.)
I think superficial constructs, such as syntax and readability, play as an important role as does the actual low level stuff supported by the language.

Name: Anonymous 2007-12-06 7:28

Basically, I'm wondering what are the least amount of constructs a functional language needs for it to be complete.
http://web.archive.org/web/20061105204247/http://ling.ucsd.edu/~barker/Iota/

Name: Anonymous 2007-12-06 8:03

at all the faggots doing
if(c >= 'a' && c <= 'z')
That shit assumes ASCII.
The C standard only guarantees that '0' .. '9' will be sequential.
Take EBCDIC for example.

Name: Anonymous 2007-12-06 8:25

>>50
Who the fuck uses EBCDIC these days?

Name: Anonymous 2007-12-06 8:37

>>50

Post some code or stfu

Name: Anonymous 2007-12-06 9:55

"alphaBETA" >upper

Name: Anonymous 2007-12-06 10:23

>>50
ASCII or GTFO

Name: Anonymous 2007-12-06 11:40

λambda fucking caλcuλus

Name: Anonymous 2007-12-06 11:47

>>45
car and cudder.

Name: Anonymous 2007-12-06 17:31

Scheme:

(define (my_toUpper str)
        (define (helper i len ret)
           (cond ((= i len) ret)
              ((char>? (string-ref str i) #\A) (helper (+ i 1) len (string-append ret (string (digit->char (- (char->digit (string-ref str i)) 32))))))
              (else (helper (+ i 1) len (string-append ret (string (string-ref str i)))))))
              (helper 0 (string-length str) ""))

Name: Anonymous 2007-12-06 17:32

(string-ref str i) #\Z)

fix'd

Name: Anonymous 2007-12-06 17:50

>>53
Well, that defeats the purpose of this thread. I'm not so much interested in how to actualy convert a string to uppercase as I am in the constructs required to do so, should one implement it themselves for some reason. Pick another example, if you prefer.

>>56
Well, yes, I have already added powerful list handling features and constructs which act on lists (like map and fold and so on, as well as car, cdr, cons and more) to my list (pun intended) of needed features for a useful language. Since strings could be thought of as lists of characters, that works fine for the toUpper sample code.

Name: Anonymous 2007-12-06 17:55

>>57
Wow, that Scheme code is massive fail. I'm glad I do CL instead.

Name: Anonymous 2007-12-06 18:26

>>57
>>60

same person

Name: Anonymous 2007-12-06 18:30

>>57
>>61
SP

Name: Anonymous 2007-12-06 18:55

In the event that OP was serious, here's a real CL version:

(defun my-upcase-char (char)
  (if (and (char> char #\Z) (alpha-char-p char))
      (code-char (- (char-code char) 32))
      char))

(defun my-loop-string-upcase (string)
  (with-output-to-string (s)
    (loop for c across string do
     (write-char (my-upcase-char c) s))))


It demonstrates pretty well that macros are awesome and should be in every language.

Name: Anonymous 2007-12-06 19:28

>>60
Enjoy your Lisp-2

Name: Anonymous 2007-12-06 19:35

>>64

As you can see from the code, I am.

Name: Anonymous 2007-12-06 19:56

let to_upper s =
  let u x = match x with
    'a'..'z' -> char_of_int (int_of_char x - 32)
  | _        -> x
  in let rec h s i =
    if i = String.length s then s else
    (s.[i] <- u s.[i]; h s (i+1))
  in h (String.copy s) 0;;



toUpper []        = [];
toUpper (car:cdr) =
    (if elem car ['a'..'z'] then iterate pred car !! 32 else car):toUpper cdr;

Name: Anonymous 2007-12-06 20:17

>>66
Fuck off with your voluntary indentation of code.

Name: Anonymous 2007-12-07 6:41

>>66
Is that Haskell?

Name: Anonymous 2007-12-07 6:45

To me, having separate namespaces for functions and not being able to use their values transparently is pretty much the same as not having first-class functions. Hence Common Lisp, Ruby, etc. fail.

Name: Anonymous 2007-12-07 7:15

>>68
are you aware of the function special form (#') ???

Name: Anonymous 2007-12-07 9:24

>>70
``Is that Haskell? '' is going to be a meme soon.

Name: Anonymous 2009-03-06 7:50

The program is an   initial list of   x y z   U V A   long doc and   i make programs   wF my API   if you dont   repost this comment   on 10 other   pages i will   not kill myself   just for uttering.

Name: Anonymous 2009-09-19 1:36

Lain.

Name: Anonymous 2009-09-19 1:36

Lain.

Name: U MENA HASKELL 2009-09-19 2:01

>>71
LITTLE DID THEY KNOW

Name: UMH memesmith !qD.NH0oTGY 2009-09-19 11:05

>>75
Fuck you. So very hard.

Name: Anonymous 2009-09-19 12:49

functional / fictional
same difference
neither exists in the real world

Name: Anonymous 2009-09-19 22:34

Do not try to write code - that's impossible. Instead, only try to realize the truth: there is no computer.

Name: Anonymous 2009-09-20 0:17

>>7

Java doesn't have functions. It has methods. It is a methodical language.

Name: UMH memesmith !gNlkr4vCuc 2009-09-20 1:40

>>77

       Identification division.
       Program-ID. Who-the-fuck-are-you.
       Author. UMH memesmith.
       Date-written. September 2009.
       Procedure division.
       Control-paragraph.
           Display "What? Who the fuck are you?"
           Display "You don't even know COBOL."
           Stop run.

Name: Anonymous 2009-09-20 4:07


up [] = []
up (x:xs) = toUpper x:up xs

Name: Anonymous 2009-09-20 7:02

>>83
EXPERT UNICODE PROGRAMMER

Name: Anonymous 2009-09-20 7:49

>>83
up = map toUpper

Name: testing 2009-09-20 9:28

#include <stdio.h>
#include <stdlib.h>

char to_upper( char foo )
{
    return foo >= 'a' && foo <= 'z' ? foo + ('A' - 'a') : foo;}

char to_lower( char foo )
{
    return foo >= 'A' && foo <= 'Z' ? foo + ('a' - 'A') : foo;}

void map_str( char * str, char (func)(char) )
{
    if ( !*str )
    {
        return;    }

    *str = func(*str);
    map_str( ++str, func );
}


void map_str_to( const char * str, char * dst, char (func)(char) )
{
    if ( !*str )
    {
        return;    }

    *dst = func(*str);
    map_str_to( ++str, ++dst, func );
}


int main( int argc, char ** argv )
{
    char convert[] = "ZERO 1 infinity";
    char *converted = malloc(sizeof(convert)+1);
    map_str( convert, to_upper );
    puts( convert );
    map_str_to( convert, converted, to_lower );
    puts( converted );
    return 0;
}

Name: testing 2009-09-20 9:38

Let me just EXPERT FORMAT(TM) that for you:

#include <stdio.h>
#include <stdlib.h>

char to_upper( char foo )
{
return foo >= 'a' && foo <= 'z' ? foo + ('A' - 'a') : foo;}

char to_lower( char foo )
{
return foo >= 'A' && foo <= 'Z' ? foo + ('a' - 'A') : foo;}

void map_str( char * str, char (func)(char) )
{
if ( !*str )
{
return;}

*str = func(*str);
map_str( ++str, func );
}


void map_str_to( const char * str, char * dst, char (func)(char) )
{
if ( !*str )
{
return;}

*dst = func(*str);
map_str_to( ++str, ++dst, func );
}


int main( int argc, char ** argv )
{
char convert[] = "ZERO 1 infinity";
char *converted = malloc(sizeof(convert)+1);
map_str( convert, to_upper );
puts( convert );
map_str_to( convert, converted, to_lower );
puts( converted );
return 0;
}

Name: Anonymous 2009-09-20 11:56

>>87

waste of time, bru, there's probably a pipeable filter for that

Name: Anonymous 2009-09-20 13:18

>>88
% tr a-z A-Z

(% tr '[:lower:]' '[:upper:]' might do more.)

Name: Anonymous 2009-09-20 14:23

#define CASE_SINGLE(value,offset) if(code==value) return code+offset;
#define CASE_RANGE(left,right,offset) if(code>=left && code<=right) return code+offset;
#define CASE_RANGE_EVEN(left,right,offset) if(code%2==0 && code>=left && code<=right) return code+offset;
#define CASE_RANGE_ODD(left,right,offset) if(code%2==1 && code>=left && code<=right) return code+offset;

unsigned int ucs_toupper(unsigned int code){
    switch(code>>8){
    case 0x00:
        CASE_RANGE     (0x0061,0x007a,-32)
        CASE_SINGLE    (0x00b5,743)
        CASE_RANGE     (0x00e0,0x00f6,-32)
        CASE_RANGE     (0x00f8,0x00fe,-32)
        CASE_SINGLE    (0x00ff,121)
        break;
    case 0x01:
        CASE_RANGE_ODD (0x0101,0x012f,-1)
        CASE_SINGLE    (0x0131,-232)
        CASE_RANGE_ODD (0x0133,0x0137,-1)
        CASE_RANGE_EVEN(0x013a,0x0148,-1)
        CASE_RANGE_ODD (0x014b,0x0177,-1)
        CASE_RANGE_EVEN(0x017a,0x017e,-1)
        CASE_SINGLE    (0x017f,-300)
        CASE_SINGLE    (0x0180,195)
        CASE_RANGE_ODD (0x0183,0x0185,-1)
        CASE_SINGLE    (0x0188,-1)
        CASE_SINGLE    (0x018c,-1)
        CASE_SINGLE    (0x0192,-1)
        CASE_SINGLE    (0x0195,97)
        CASE_SINGLE    (0x0199,-1)
        CASE_SINGLE    (0x019a,163)
        CASE_SINGLE    (0x019e,130)
        CASE_RANGE_ODD (0x01a1,0x01a5,-1)
        CASE_SINGLE    (0x01a8,-1)
        CASE_SINGLE    (0x01ad,-1)
        CASE_SINGLE    (0x01b0,-1)
        CASE_RANGE_EVEN(0x01b4,0x01b6,-1)
        CASE_SINGLE    (0x01b9,-1)
        CASE_SINGLE    (0x01bd,-1)
        CASE_SINGLE    (0x01bf,56)
        CASE_SINGLE    (0x01c5,-1)
        CASE_SINGLE    (0x01c6,-2)
        CASE_SINGLE    (0x01c8,-1)
        CASE_SINGLE    (0x01c9,-2)
        CASE_SINGLE    (0x01cb,-1)
        CASE_SINGLE    (0x01cc,-2)
        CASE_RANGE_EVEN(0x01ce,0x01dc,-1)
        CASE_SINGLE    (0x01dd,-79)
        CASE_RANGE_ODD (0x01df,0x01ef,-1)
        CASE_SINGLE    (0x01f2,-1)
        CASE_SINGLE    (0x01f3,-2)
        CASE_SINGLE    (0x01f5,-1)
        CASE_RANGE_ODD (0x01f9,0x021f,-1)
        break;
    case 0x02:
        CASE_RANGE_ODD (0x01f9,0x021f,-1)
        CASE_RANGE_ODD (0x0223,0x0233,-1)
        CASE_SINGLE    (0x023c,-1)
        CASE_SINGLE    (0x0242,-1)
        CASE_RANGE_ODD (0x0247,0x024f,-1)
        CASE_SINGLE    (0x0250,10783)
        CASE_SINGLE    (0x0251,10780)
        CASE_SINGLE    (0x0253,-210)
        CASE_SINGLE    (0x0254,-206)
        CASE_RANGE     (0x0256,0x0257,-205)
        CASE_SINGLE    (0x0259,-202)
        CASE_SINGLE    (0x025b,-203)
        CASE_SINGLE    (0x0260,-205)
        CASE_SINGLE    (0x0263,-207)
        CASE_SINGLE    (0x0268,-209)
        CASE_SINGLE    (0x0269,-211)
        CASE_SINGLE    (0x026b,10743)
        CASE_SINGLE    (0x026f,-211)
        CASE_SINGLE    (0x0271,10749)
        CASE_SINGLE    (0x0272,-213)
        CASE_SINGLE    (0x0275,-214)
        CASE_SINGLE    (0x027d,10727)
        CASE_SINGLE    (0x0280,-218)
        CASE_SINGLE    (0x0283,-218)
        CASE_SINGLE    (0x0288,-218)
        CASE_SINGLE    (0x0289,-69)
        CASE_RANGE     (0x028a,0x028b,-217)
        CASE_SINGLE    (0x028c,-71)
        CASE_SINGLE    (0x0292,-219)
        break;
    case 0x03:
        CASE_SINGLE    (0x0345,84)
        CASE_RANGE_ODD (0x0371,0x0373,-1)
        CASE_SINGLE    (0x0377,-1)
        CASE_RANGE     (0x037b,0x037d,130)
        CASE_SINGLE    (0x03ac,-38)
        CASE_RANGE     (0x03ad,0x03af,-37)
        CASE_RANGE     (0x03b1,0x03c1,-32)
        CASE_SINGLE    (0x03c2,-31)
        CASE_RANGE     (0x03c3,0x03cb,-32)
        CASE_SINGLE    (0x03cc,-64)
        CASE_RANGE     (0x03cd,0x03ce,-63)
        CASE_SINGLE    (0x03d0,-62)
        CASE_SINGLE    (0x03d1,-57)
        CASE_SINGLE    (0x03d5,-47)
        CASE_SINGLE    (0x03d6,-54)
        CASE_SINGLE    (0x03d7,-8)
        CASE_RANGE_ODD (0x03d9,0x03ef,-1)
        CASE_SINGLE    (0x03f0,-86)
        CASE_SINGLE    (0x03f1,-80)
        CASE_SINGLE    (0x03f2,7)
        CASE_SINGLE    (0x03f5,-96)
        CASE_SINGLE    (0x03f8,-1)
        CASE_SINGLE    (0x03fb,-1)
        break;
    case 0x04:
        CASE_RANGE     (0x0430,0x044f,-32)
        CASE_RANGE     (0x0450,0x045f,-80)
        CASE_RANGE_ODD (0x0461,0x0481,-1)
        CASE_RANGE_ODD (0x048b,0x04bf,-1)
        CASE_RANGE_EVEN(0x04c2,0x04ce,-1)
        CASE_SINGLE    (0x04cf,-15)
        CASE_RANGE_ODD (0x04d1,0x0523,-1)
        break;
    case 0x05:
        CASE_RANGE_ODD (0x04d1,0x0523,-1)
        CASE_RANGE     (0x0561,0x0586,-48)
        break;
    case 0x1d:
        CASE_SINGLE    (0x1d79,35332)
        CASE_SINGLE    (0x1d7d,3814)
        break;
    case 0x1e:
        CASE_RANGE_ODD (0x1e01,0x1e95,-1)
        CASE_SINGLE    (0x1e9b,-59)
        CASE_RANGE_ODD (0x1ea1,0x1eff,-1)
        break;
    case 0x1f:
        CASE_RANGE     (0x1f00,0x1f07,8)
        CASE_RANGE     (0x1f10,0x1f15,8)
        CASE_RANGE     (0x1f20,0x1f27,8)
        CASE_RANGE     (0x1f30,0x1f37,8)
        CASE_RANGE     (0x1f40,0x1f45,8)
        CASE_RANGE_ODD (0x1f51,0x1f57,8)
        CASE_RANGE     (0x1f60,0x1f67,8)
        CASE_RANGE     (0x1f70,0x1f71,74)
        CASE_RANGE     (0x1f72,0x1f75,86)
        CASE_RANGE     (0x1f76,0x1f77,100)
        CASE_RANGE     (0x1f78,0x1f79,128)
        CASE_RANGE     (0x1f7a,0x1f7b,112)
        CASE_RANGE     (0x1f7c,0x1f7d,126)
        CASE_RANGE     (0x1f80,0x1f87,8)
        CASE_RANGE     (0x1f90,0x1f97,8)
        CASE_RANGE     (0x1fa0,0x1fa7,8)
        CASE_RANGE     (0x1fb0,0x1fb1,8)
        CASE_SINGLE    (0x1fb3,9)
        CASE_SINGLE    (0x1fbe,-7205)
        CASE_SINGLE    (0x1fc3,9)
        CASE_RANGE     (0x1fd0,0x1fd1,8)
        CASE_RANGE     (0x1fe0,0x1fe1,8)
        CASE_SINGLE    (0x1fe5,7)
        CASE_SINGLE    (0x1ff3,9)
        break;
    case 0x21:
        CASE_SINGLE    (0x214e,-28)
        CASE_RANGE     (0x2170,0x217f,-16)
        CASE_SINGLE    (0x2184,-1)
        break;
    case 0x24:
        CASE_RANGE     (0x24d0,0x24e9,-26)
        break;
    case 0x2c:
        CASE_RANGE     (0x2c30,0x2c5e,-48)
        CASE_SINGLE    (0x2c61,-1)
        CASE_SINGLE    (0x2c65,-10795)
        CASE_SINGLE    (0x2c66,-10792)
        CASE_RANGE_EVEN(0x2c68,0x2c6c,-1)
        CASE_SINGLE    (0x2c73,-1)
        CASE_SINGLE    (0x2c76,-1)
        CASE_RANGE_ODD (0x2c81,0x2ce3,-1)
        break;
    case 0x2d:
        CASE_RANGE     (0x2d00,0x2d25,-7264)
        break;
    case 0xa6:
        CASE_RANGE_ODD (0xa641,0xa65f,-1)
        CASE_RANGE_ODD (0xa663,0xa66d,-1)
        CASE_RANGE_ODD (0xa681,0xa697,-1)
        break;
    case 0xa7:
        CASE_RANGE_ODD (0xa723,0xa72f,-1)
        CASE_RANGE_ODD (0xa733,0xa76f,-1)
        CASE_RANGE_EVEN(0xa77a,0xa77c,-1)
        CASE_RANGE_ODD (0xa77f,0xa787,-1)
        CASE_SINGLE    (0xa78c,-1)
        break;
    case 0xff:
        CASE_RANGE     (0xff41,0xff5a,-32)
        break;
    case 0x104:
        CASE_RANGE     (0x10428,0x1044f,-40)
        break;
    }
   
    return code;
}

Name: Anonymous 2009-09-20 17:18

#include <stdio.h>      // putc
#include <stdint.h>     // SIZE_MAX
#include <ctype.h>      // isalpha

void its_functional(char *arg)
{
    if(*arg != '\0')
    {
        int result = EOF;
        if(isalpha(*arg))
        {
            result = putchar(*arg - 0x20);
        }
        else
        {
            result = putchar(*arg);
        }

        if(result != EOF)
        {
            its_functional(++arg);
        }
    }
}

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        fprintf(stderr, "Usage: %s <string>\n", argv[0]);
        return -1;
    }

    its_functional(&argv[1][0]);
    return 0;
}


If anyone can provide a working Unicode version then I'm all eyes. My version fails with "Invalid multi-byte character" whenever I try to feed it an actual UTF-8 character. Here it is:

#include <string.h>
#include <stdio.h>
#include <wctype.h>
#include <wchar.h>
#include <limits.h>
#include <errno.h>

void its_functional(wchar_t *arg)
{
    if(*arg != L'\0')
    {
        putwchar(towupper(*arg));
        its_functional(++arg);
    }
}

int main(int argc, char *argv[])
{
    wchar_t arg[_POSIX_ARG_MAX] = {L'\0'};

    if(fwide(stdout, 0) == 0)
    {
        if(fwide(stdout, 1) <= 0)
        {
            fputs("Failed to switch to wide character mode\n", stderr);
            return -1;
        }
    }

    if(argc != 2)
    {
        fwprintf(stderr, L"Usage: %s <string>\n", argv[0]);
        return -1;
    }

    if(mbstowcs(arg, argv[1], strlen(argv[1])) == (size_t)-1)
    {
        fputs(strerror(errno), stderr);
        return -1;
    }
    its_functional(arg);
    return 0;
}

Name: Anonymous 2009-09-20 17:40

>>15
HOLY SHIT PPPP NO YOU HOT GRIDS

Don't change these.
Name: Email:
Entire Thread Thread List