News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Haskell

Started by jj2007, May 08, 2022, 07:33:44 PM

Previous topic - Next topic

jj2007

From https://www.quora.com/What-are-the-things-that-C-cant-do-that-C-can-do-and-what-are-the-things-that-C-cant-do-that-assembly-can-do/answer/Dr-Jo-6 (a lengthy rant about the virtues of a Turing machine):

QuoteHere's factorial written in Common Lisp:

    (defun factorial (n)
      (if (zerop n) 1 (* n (factorial (1- n)))))

Here's one way of doing the same in Haskell:

    factorial n = product [1..n]

I couldn't resist, of course :biggrin:

include \masm32\MasmBasic\MasmBasic.inc         ; download

product macro range     ; Haskell source
Local is, first, last
  is INSTR <range>, <..>
  first SUBSTR <range>, 1, is-1
  last SUBSTR <range>, is+2
  push first
  fild stack
  .Repeat
        pop eax
        inc eax
        push eax
        fimul stack
  .Until eax>last
  pop eax
  EXITM <ST(0)>
ENDM

  Init
  Inkey Str$("The factorial 1...9 = %i", product(1..9)v)
EndOfCode

Vortex

Hi Jochen,

Your solution is quick and effective. There are sometimes good questions there but that website \ forum you mentioned is mainly for individuals with inflated egos. No point of complicating simple questions with the Turing Machine. Professor Yada Yada of University A or Mr. X holding a doctorate degree from super University Y etc.. All they are ridiculous people.

jj2007

Here is an advanced example (source & exe attached) - I wonder if you can do that in Haskell:

include \masm32\MasmBasic\MasmBasic.inc         ; download

product macro range                             ; Haskell source
Local is, first, last
  is INSTR <range>, <..>
  first SUBSTR <range>, 1, is-1
  last SUBSTR <range>, is+2
  push first
  fild stack
  .While 1
       pop eax
       inc eax
       push eax
       .Break .if eax>last
       fimul stack
  .Endw
  EXITM <ST(0)>
ENDM

  Init
  For_ ct=1 To 20
        Print Str$("\nThe factorial 1...%i\t = ", ct), Str$(product(1..ct)v)    ; the v means "fstp st"
  Next
EndOfCode


The factorial 1...1      = 1
The factorial 1...2      = 2
The factorial 1...3      = 6
The factorial 1...4      = 24
The factorial 1...5      = 120
The factorial 1...6      = 720
The factorial 1...7      = 5040
The factorial 1...8      = 40320
The factorial 1...9      = 362880
The factorial 1...10     = 3628800
The factorial 1...11     = 39916800
The factorial 1...12     = 479001600
The factorial 1...13     = 6227020800
The factorial 1...14     = 87178291200
The factorial 1...15     = 1307674368000
The factorial 1...16     = 20922789888000
The factorial 1...17     = 355687428096000
The factorial 1...18     = 6402373705728000
The factorial 1...19     = 121645100408832000
The factorial 1...20     = 2432902008176640000


My point here is that some of the esoteric stuff like Haskell's product, functional programming and the like are often easy to implement in Masm, as a macro. Some HLL languages boast with such features, but they cover very special functions, so it is at the discretion of the language's author to add them or not. The language is not intrinsically "better" if it has such features.

Re Quora, my experience (as a lurker) is very mixed. Often it's just rants, of course. You get what you ask for: every day you get an email with suggestions, and if you repeatedly click on titles like Why do the British spell 'humour' instead of the correct "humor", Quora will send you to the section where the British make fun of the Muricans :biggrin:

FORTRANS

Hi,

   Just curious.  Why:

  .While 1
       pop eax
       inc eax
       push eax
       .Break .if eax>last
       fimul stack
  .Endw

instead of:

  .While
       inc stack
       .Break .if stack>last
       fimul stack
  .Endw

Cheers,

Steve

jj2007

One byte shorter, and it works with non-immediates as in product(1..ct), too.