Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>

MononcQc posted:

if your programming language doesn't have pattern matching it's a dumb language from the past.

Why wouldn't it just be a library

Adbot
ADBOT LOVES YOU

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Zizzyx posted:

why doesn't that also print butts idgi

it matches the first one, so you can have a catchall farther down that only gets called when nothing above it matches

MononcQc
May 29, 2007

Cocoa Crispies posted:

if you can't build it yourself lol
code:
    p = Matcher.new
    p.match(:alpha, wc) do
      puts "farts"
    end
    p.match(wc, :bravo) do
      puts "butts"
    end
    p[:alpha, :bravo] #=> "farts"
as long as you can pattern match on other data types that can be decent.

vapid cutlery posted:

Why wouldn't it just be a library

It can be. The important thing is that everyone should be using it in that community.

Opinion Haver
Apr 9, 2007

MononcQc posted:

if your programming language doesn't have pattern matching it's a dumb language from the past.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

MononcQc posted:

as long as you can pattern match on other data types that can be decent.

yeah that uses the "===" in ruby which does a fuzzy comparison
Ruby code:
/abc/ === "abc"
# => true
/abc/ === "abcde"
# => true
String === "abcde"
# => true
"abc" === "abcde"
# => false
5 === 6
# => false
6 === false
# => false
Fixnum === 6
# => true
Integer === 6
# => true
Integer === 6.5
# => false
Numeric === 6.5
# => true
for a Regexp it matches, for a Class it checks ancestry, by default it uses ==

i just realized i could override Wildcard#=== instead of special casing it, hello revisiting throw-away toy project from three years ago

MononcQc
May 29, 2007

Cocoa Crispies posted:

yeah that uses the "===" in ruby which does a fuzzy comparison
Ruby code:
/abc/ === "abc"
# => true
/abc/ === "abcde"
# => true
String === "abcde"
# => true
"abc" === "abcde"
# => false
5 === 6
# => false
6 === false
# => false
Fixnum === 6
# => true
Integer === 6
# => true
Integer === 6.5
# => false
Numeric === 6.5
# => true
for a Regexp it matches, for a Class it checks ancestry, by default it uses ==

i just realized i could override Wildcard#=== instead of special casing it, hello revisiting throw-away toy project from three years ago

This does sound more like dynamic dispatch (CLOS-like) than pattern matching. You have to add something a bit like clojure's destructuring bind to it. Here's in Racket how you could pattern match on a list:

code:
> (match '(1 2 3)
    [(list 1 a ...) a])

'(2 3)
In Erlang, it would be based on something like [1, A | _] to bind A to the 2nd item in the list.

Racket has it powerful as hell -- you can expand it to use arbitrary data structures or even regexes:

code:
> (match "apple"
    [(regexp #rx"p+(.)" (list _ "l")) 'yes]
    [_ 'no])

'yes
Compared to just matching on a regex on true/false, it checks if the regex succeeds and then can extract data from the result and keep going from there. In that case, it looks whether the match ends with an "l".

Pattern matching goes one step further than just destructuring binds or dynamic dispatch.

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>

MononcQc posted:

The important thing is that everyone should be using it in that community.

for what?

MononcQc
May 29, 2007


condition branching/control flow and a large part of assignment in that language.

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>

MononcQc posted:

condition branching/control flow and a large part of assignment in that language.

i'm not really convinced

Zombywuf
Mar 29, 2008

You really need pattern matching and unification.

Also backtracking.

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>
oh cool the "everyone should use hard computer science to write their applications" crew is out in force. cya

Sneaking Mission
Nov 11, 2008

i recommend some duck typing in your next program. make sure its duck typed. you gotta do it

Rufus Ping
Dec 27, 2006





I'm a Friend of Rodney Nano
duck typing is wack

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>
moé! duck typing is moé!

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

my programs are swan typed in the sense that users often break their arms while using them

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>
lmao php doesn't have a library function for building paths

Sneaking Mission
Nov 11, 2008

have you tried: autovivification?

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

vapid cutlery posted:

lmao php doesn't have a library function for building paths

you mean it doesn't have a concatenation operator?? wtf

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>

Jonnty posted:

you mean it doesn't have a concatenation operator?? wtf

that isn't all there is to a function like that

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

vapid cutlery posted:

that isn't all there is to a function like that

i get super upset at double slashes in my paths too

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>

Jonnty posted:

i get super upset at double slashes in my paths too

typical PHP developer settling for mediocrity and rewriting the same thing a hundred times i guess

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

vapid cutlery posted:

oh cool the "everyone should use hard computer science to write their applications" crew is out in force. cya

lol

basically think of how you open a file in c
C code:
FILE* file_of_dicks = fopen("dicks.txt","r");
process_dicks(file_of_dicks);
fclose(file_of_dicks);
"tere's no error checking retard"

C code:
void process_dicks(NULL) {
  /* bitch about failure */
}

void process_dicks(FILE* file_of_dicks) {
  …
}
now there is, by having the first version that only takes null do it, and the second version only gets a file handle

if that's a "hard computer science" thing maybe you should look into php/jquery

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>

Cocoa Crispies posted:

lol

basically think of how you open a file in c
C code:
FILE* file_of_dicks = fopen("dicks.txt","r");
process_dicks(file_of_dicks);
fclose(file_of_dicks);
"tere's no error checking retard"

C code:
void process_dicks(NULL) {
  /* bitch about failure */
}

void process_dicks(FILE* file_of_dicks) {
  …
}
now there is, by having the first version that only takes null do it, and the second version only gets a file handle

if that's a "hard computer science" thing maybe you should look into php/jquery

that's a nice trivial example that can actually be written in C in fewer, easier to understand lines than what you posted

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>
i'm looking through the php api docs and apparently everyone has their own way of parsing and gluing together URL's from individual components. that's pretty loving hilarious for a language that lives on web servers

MononcQc
May 29, 2007

vapid cutlery posted:

oh cool the "everyone should use hard computer science to write their applications" crew is out in force. cya

It's actually simpler using it. The example below creates a little hash with 3 users as keys, and the value for each is a list of their password and a secret to return whenever the password is right.

Here's the hash definition:
code:
(define users (make-hash (list (cons "tef" (list "password" "I Love Ruby"))
                               (cons "mononcqc" (list "hunter2" "I expect *******"))
                               (cons "vapid cutlery" (list "mylittlebrony" "IDGI")))))
The normal way to do things is then to:

  • Fetch the user from the hash
  • if it's there, extract the password and the secret from the value returned
  • if the password is the good one, return the secret, otherwise return 'false'

this is how it's done in racket:
code:
(define (login1 user pass)
  (let ([data (hash-ref users user 'undefined)]) ; fetch the entry
    (if (eq? 'undefined data)                    ; check if it's there
        #f
        (let ([passwd (car data)]                ; extract the password
              [secret (cadr data)])              ; extract the secret
          (if (eq? passwd pass)                  ; check if the password is right
              secret                             ; return the secret
              #f)))))
Using pattern matching, I can fetch from the hash, extract the values and compare them all at once

Here's the same function implemented using pattern matching:
code:
(define (login2 user pass)
  (match (hash-ref users user 'undefined)        ; fetch the value
    [(list (== pass) secret) secret]             ; deconstruct, compare & return
    [_ #f]))                                     ; not found
It's not hard to use, just somewhat harder to implement if you're writing the language or library. Having pattern matching makes code much shorter and simpler at once.

E: usage

code:
> (login1 "bad user" "nknlk")
#f
> (login1 "tef" "password")
"I Love Ruby"
> (login1 "mononcqc" "heh")
#f
> (login2 "bad user" "kjdsf")
#f
> (login2 "tef" "password")
"I Love Ruby"
> (login2 "bad user" "kjdsf")
#f

MononcQc fucked around with this message at 21:22 on Aug 5, 2012

Zizzyx
Sep 18, 2007

INTERGALACTIC CAN CHAMPION

vapid cutlery posted:

i'm looking through the php api docs and apparently everyone has their own way of parsing and gluing together URL's from individual components. that's pretty loving hilarious for a language that lives on web servers

this is desired behavior. you just don't get php apparently

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>

MononcQc posted:

It's actually simpler using it. The example below creates a little hash with 3 users as keys, and the value for each is a list of their password and a secret to return whenever the password is right.

Here's the hash definition:
code:
(define users (make-hash (list (cons "tef" (list "password" "I Love Ruby"))
                               (cons "mononcqc" (list "hunter2" "I expect *******"))
                               (cons "vapid cutlery" (list "mylittlebrony" "IDGI")))))
The normal way to do things is then to:

  • Fetch the user from the hash
  • if it's there, extract the password and the secret from the value returned
  • if the password is the good one, return the secret, otherwise return 'false'

this is how it's done in racket:
code:
(define (login1 user pass)
  (let ([data (hash-ref users user 'undefined)]) ; fetch the entry
    (if (eq? 'undefined data)                    ; check if it's there
        #f
        (let ([passwd (car data)]                ; extract the password
              [secret (cadr data)])              ; extract the secret
          (if (eq? passwd pass)                  ; check if the password is right
              secret                             ; return the secret
              #f)))))
Using pattern matching, I can fetch from the hash, extract the values and compare them all at once

Here's the same function implemented using pattern matching:
code:
(define (login2 user pass)
  (match (hash-ref users user 'undefined)        ; fetch the value
    [(list (== pass) secret) secret]             ; deconstruct, compare & return
    [_ #f]))                                     ; not found
It's not hard to use, just somewhat harder to implement. Having pattern matching makes code much shorter and simpler at once.

E: usage

code:
> (login1 "bad user" "nknlk")
#f
> (login1 "tef" "password")
"I Love Ruby"
> (login1 "mononcqc" "heh")
#f
> (login2 "bad user" "kjdsf")
#f
> (login2 "tef" "password")
"I Love Ruby"
> (login2 "bad user" "kjdsf")
#f

it would be nice if you used a language that was actually relevant, or even comprehensible

Zizzyx
Sep 18, 2007

INTERGALACTIC CAN CHAMPION

those loving parens everywhere

Toady
Jan 12, 2009

but but learning lisp makes you a better programmer in all languages!

MononcQc
May 29, 2007

vapid cutlery posted:

it would be nice if you used a language that was actually relevant, or even comprehensible

maybe if your lovely language of choice supported pattern matching I could show the features, but your language sucks because it doesn't have pattern matching.

Zizzyx
Sep 18, 2007

INTERGALACTIC CAN CHAMPION

MononcQc posted:

maybe if your lovely language of choice supported pattern matching I could show the features, but your language sucks because it doesn't have pattern matching.

do one in haskell it probably has the least gay syntax

salted hash browns
Mar 26, 2007
ykrop
i want to learn clojure but don't know where to start

MononcQc
May 29, 2007

Languages that support pattern matching to some extent from the top of my head:

Racket, Erlang, Mercury, Haskell, OCaml, Mathematica, Elixir, REBOL, SNOBOL, Scala, SML, io, etc.

Zombywuf
Mar 29, 2008

A better language would have:
code:
file_of_dicks(Name, Dicks) :-
   read_file(Name, Contents),
   process_dicks(Contents, Dicks).

> file_of_dicks("dicks.txt", Dicks).
no.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

salted hash browns posted:

i want to learn clojure but don't know where to start

4clojure.org

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
did i ever mention that this
code:
Clojure> (cons 'foo 'bar)
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol
Clojure> (cons 'foo '[bar])
(foo bar)
Clojure> (cons '[foo] 'bar)
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol
is obnoxious? because it is.

La petite z0rt
Aug 4, 2012

salted hash browns posted:

i want to learn but don't know where to start

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>

MononcQc posted:

Languages that support pattern matching to some extent from the top of my head:

Racket, Erlang, Mercury, Haskell, OCaml, Mathematica, Elixir, REBOL, SNOBOL, Scala, SML, io, etc.

drat. it's like the greatest hits of toy research languages

MononcQc
May 29, 2007

Otto Skorzeny posted:

did i ever mention that this
code:
Clojure> (cons 'foo 'bar)
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol
Clojure> (cons 'foo '[bar])
(foo bar)
Clojure> (cons '[foo] 'bar)
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol
is obnoxious? because it is.

so what is this? clojure supports using cons, but not pairs? I guess that's a way to protect yourself from "lists that don't end right".

Adbot
ADBOT LOVES YOU

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

vapid cutlery posted:

drat. it's like the greatest hits of toy research languages

erlang's not so much a toy research language as a Serious Production Language for infrastructure like telephone switches

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply