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

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


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