News for hackers. Stuff that matters.
Re:Why your first Rust FizzBuzz implementation may not work by kyllo on Sunday October 12, 2014 @11:11PM (#8447355)
Weird to see this mix of a very imperative for-range iterative loop with a very functional pattern match, which makes it look similar to an SML or OCaml solution to FizzBuzz. I guess this is the definition of multi-paradigm right here.

Will Rust's type checker warn you of a non-exhaustive pattern match?

Re:Why your first Rust FizzBuzz implementation may not work by bjz_ on Monday October 13, 2014 @12:35AM (#8447498)
Rust was first conceived by an avid Ocamler, and it was originally implemented in Ocaml too. Although the pot has been stirred quite a bit since those early days, the influence still remains, including the expression heavy programming style, pattern matching, 'let's, HM inference, and `var: T` declaration syntax. Whilst Rust is quite procedural and (you rarely use recursion), it often feels quite functional due to those things.
Re:Why your first Rust FizzBuzz implementation may not work by steveklabnik on Monday October 13, 2014 @02:37AM (#8447713)
Small note, technically Rust never did HM, and now we _certainly_ don't. It's still inference, just not that algorithm.
Re:Why your first Rust FizzBuzz implementation may not work by bjz_ on Monday October 13, 2014 @03:39AM (#8447834)
Oh - I thought it was a variant on HM, extended for region inference?
Re:Why your first Rust FizzBuzz implementation may not work by steveklabnik on Monday October 13, 2014 @04:09AM (#8447890)
I'm not _totally_ sure, but I do know when I tried to reference HM in the docs I got yelled at... and now I'm pretty sure we do http://smallcultfollowing.com/babysteps/blog/2014/07/09/an-e...
Re:Why your first Rust FizzBuzz implementation may not work by kibwen on Monday October 13, 2014 @08:17AM (#8448382)
It wasn't strictly HM, as it had extensions for the subtyping that lifetimes require. It was based on HM, however.

The new bespoke scheme gives approximately the same results as HM but is drastically simpler. For all I know this could inhibit Rust's future ability to do even more powerful things with types, but AIUI this scheme has the advantage of being actually decidable given the extensions to HM that we would require in the current language.

I ain't a type theorist though, so take this as hearsay. :)

Re:Why your first Rust FizzBuzz implementation may not work by thinkpad20 on Monday October 13, 2014 @12:43PM (#8449419)
Strictly speaking, I think almost no extant languages, and certainly no mainstream ones, use pure HM, but many take it as a starting point. Certainly, HM has no notion of ML modules, or type classes, or record types, or lifetimes. Nevertheless many languages using those things use HM as a starting point.

I'm curious (and a bit skeptical) of your claim that the scheme is "drastically simpler" than HM. HM is a beautifully simple design, which can be expressed (abstractly) in just a couple of lines.

Re:Why your first Rust FizzBuzz implementation may not work by kibwen on Monday October 13, 2014 @01:47PM (#8449694)
I'm paraphrasing Niko Matsakis, Rust's type guru.

http://smallcultfollowing.com/babysteps/blog/2014/07/09/an-e...

"This scheme simplifies the code of the type inferencer dramatically and (I think) helps to meet our intutions (as I will explain). It is however somewhat less flexible than the existing inference scheme, though all of rustc and all the libraries compile without any changes."

Re:Why your first Rust FizzBuzz implementation may not work by icedog on Sunday October 12, 2014 @11:13PM (#8447360)
Here's the same approach in F# without the different types of String; therefore, easier to get more functional.

    let fizzbuzz num =     
       match num % 3, num % 5 with      
          | 0,0 -> "FizzBuzz"
          | 0,_ -> "Fizz"
          | _,0 -> "Buzz"
          | _,_ -> num.ToString()

    [1..100]
      |> List.map fizzbuzz
      |> List.iter (fun (s:string) -> printfn "%s" s)
Re:Why your first Rust FizzBuzz implementation may not work by minikomi on Monday October 13, 2014 @03:06AM (#8447761)
And racket, just for kicks

    #lang racket
    
    (define (fizz-buzz n)
      (match (list (modulo n 3) (modulo n 5))  
          [(list 0 0) "FizzBuzz"]
          [(list 0 _) "Fizz"]
          [(list _ 0) "Buzz"]
          [_          n]))
    
    (for [(i (range 100))]
      (displayln (fizz-buzz i)))
Re:Why your first Rust FizzBuzz implementation may not work by throwaway283719 on Monday October 13, 2014 @04:13AM (#8447901)
Haskell -

  fizzbuzz x = case (x `mod` 3, x `mod` 5) of
      (0, 0) -> "FizzBuzz"
      (0, _) -> "Fizz"
      (_, 0) -> "Buzz"
       _     -> show i

  mapM_ (putStrLn . fizzbuzz) [1..100]
Re:Why your first Rust FizzBuzz implementation may not work by drostie on Monday October 13, 2014 @12:34PM (#8449380)
Pattern matching is one of the ways to get "two-mod" code where the modulus operator is used two times. For example, string concatenation and assignment operators do the same in this Python code:

    for i in range(1, 101):
        x = ""
        if i % 3 == 0:
            x += "Fizz"
        if i % 5 == 0:
            x += "Buzz"
        if x == "":
            x += str(i)
        print(x)
If anybody is randomly curious it can be fun to solve FizzBuzz in Haskell the same way that this Python code does it, but (to be more idiomatically Haskell) storing the FizzBuzz success in a Maybe (or some other variable). If you define the appropriate `~>`, and higher-precedence `|~`, and higher-precedence `|~~>`, you can write the above function as:

    fizzbuzz x = 
        mod x 3 == 0    ~> "Fizz" 
        |~ mod x 5 == 0 ~> "Buzz" 
        |~~> show x 
It's interesting because it's sort of a "follow-through guards" situation; the (|~) operator can at least be turned into a type signature of (Monoid m) => Maybe m -> Maybe m -> Maybe m.
Re:Why your first Rust FizzBuzz implementation may not work by throway-4-0-1 on Monday October 13, 2014 @04:50AM (#8447968)
Currying often allows for elegant point free code. Like in your last line, for F# they could also have written

    [1..100] |> List.iter (fizzbuzz >> printfn "%s")
Re:Why your first Rust FizzBuzz implementation may not work by latkin on Monday October 13, 2014 @12:08AM (#8447444)
Nice F# snippet! This just looks so simple and elegant. As another approach, the "enum" types the OP mentions map to discriminated unions in F#.

FWIW your final wildcard match can be (modestly) simplified to just

  | _ -> num.ToString()
and the last line can be distilled to just

  |> List.iter (printfn "%s")
Re:Why your first Rust FizzBuzz implementation may not work by mercurial on Monday October 13, 2014 @02:19AM (#8447687)
Although I love pattern matching, I find the solution with 'if' more legible and no less functional.
Re:Why your first Rust FizzBuzz implementation may not work by chrismorgan on Sunday October 12, 2014 @11:13PM (#8447359)
Non-exhaustive pattern matching is a compilation error.
Re:Why your first Rust FizzBuzz implementation may not work by masklinn on Monday October 13, 2014 @02:23AM (#8447696)
> Will Rust's type checker warn you of a non-exhaustive pattern match?

It refuses to compile entirely.