RSSAmplifier

Blog

Exploring Beautiful Languages

Programming language exploration blog

langexplr.blogspot.comRSS feed ↗25 posts

Latest posts

A quick look at functors in OCaml

A couple of weeks ago I was working on a small program that required generating code in different ways depending on a user option. I was trying to make as few changes as possible. Because of the way the program was created and the language it was written in (not OCaml ), it required changing several places in the code. OCaml functors I remembered reading a little bit about the concept of a functor…

Haskell 'newtype' and the record syntax

While reading some Haskell code snippets I found something that seemed confusing. The snippet involved newtype and record syntax. A simplified example is the following: newtype PersonName = PersonName { theName :: String } ... let p1 = PersonName $ getName obj in print $ theName I was not able to find a place where the PersonName was created by specifying the value of theName explicitly for…

Some considerations for using closures in Rust/WASM

Here are a couple of things I learned while trying to pass a Rust closure to a JavaScript function. Some of these notes are a result of my lack of experience with Rust and Rust/WASM. Passing a closure that is going to outlive the current function call Passing a Rust function that exists in the stack to JavaScript is easy for example, here is a call to Array.map : #[wasm_bindgen] pub fn…

Implementing WHILE in a toy BASIC interpreter

While working on a toy BASIC implementation, I ran into an unexpected challenge implementing the WHILE instruction. The implementation of this instruction seems simple. Here is an example : 10 X = 1 20 WHILE X <> 10 30 PRINT X 40 X = X + 1 50 WEND This program is going to print the numbers from 1 to 9. The WHILE statement is really a combination of WHILE and WEND . The WEND statement indicates the…

Executing code from a buffer with Rust on Windows

Creating and executing code at runtime is an intriguing topic. It is one of the pieces that makes it possible to write JIT compilers for things like Java or C#. Creating a byte array with executable instructions and “casting” that array to a function pointer is not enough . For security reasons, modern operating systems require you to specify which region of memory of your program is executable.…

Exploring a Webpack stats file with Prolog

A couple of days ago I was reading about the Webpack statistics file created using the following command line options: npx webpack --profile --json This file contains a lot of information collected by Webpack about the project being processed. The information in this file is used by nice visualization tools like Webpack Bundle Analyzer . The dependency graph is included in this file. That is, all…

A small programming exercise in APL #2: Combinations

In this post I'm going to continue my APL exploration by working in a possible solution for the problem of generating all the combinations of 'n' elements of an array. Generating the 'n' combinations of an array means generating a sequence of all possible 'n' unique elements from the original array . For example, given the array 12, 34, 35, 65 all possible '2' combinations of this array are: 12,…

A small programming exercise in APL #1

This post shows a possible solution written in APL for the following programming problem: Given an array, determine if a value 'number' is found in every consecutive segment of 'size' elements. For example, given this array: 1,2,1,3,4,1 This predicate is true for number = 1 and size = 2 . Because '1' is found in (1,2), (1,3) and (4,1). A possible APL solution This is a possible solution to this…

Reading APL #1: Roman numerals to decimal

As part of a new recurring series I’m going to take a small APL function or program and try to break it down in to its parts. I hope this is going to help me get a better understanding of the language. Reading APL This article: The APL Programming Language Source Code has a nice introduction to the language and its history. One sentence in this article is key to understand how to read APL code:…

A small experiment with fragment shaders

I wanted to work on an experiment that allowed me to learn a little bit about modern graphics programming with OpenGL (with shaders that is). A nice choice is the 'hello world; of graphics programming: rendering the Mandelbrot set. In this post I'm going to record my experiences from zero to a basic rendering of the Mandelbrot set. Here is how the final product looks like: 1.1 The experiment To…

First programming language: GW-BASIC

Like many developers my age, the first programming language I ever used was BASIC. Specifically, GW-BASIC in a nice Epson "Abacus" XT machine with a green-on-black monitor back in primary school. For me, the experience of writing my first program was magical. Having feedback from the computer with just few key strokes was an essential part of it. 10 PRINT "hola" RUN hola Writing a small/toy…

A quick note about programming in Prolog

Prolog predicates work in different ways depending on how you use them. A simple example is the append predicate. One may say that append is used to get the result of appending two lists. For example: ?- append([1,2,3], [4,5,6], NewList). NewList = [1, 2, 3, 4, 5, 6]. But we can also use append to get the prefix of a list given a suffix. ?- append(Prefix, [5,6], [1,2,3,4,5,6]). Prefix = [1, 2, 3,…

Using Racklog for parsing

This post shows a small experiment of creating parsing predicates using the Racket's Racklog package . This package enables the use of logic programming features inside Racket . Logic programming languages, like Prolog, use the Definite clause grammars syntax for this purpose. In this post this technique is not directly used, the goal is to express the grammar in the same way the DCG syntax is…

A small Tetris-like clone using J and ncurses. Part 2

This is part two of our Tetris-like clone in J series. In this part we're going to see how the ncurses interface was created. Creating the ncurses UI To create the user interface we used the ncurses UI using the api/ncurses package. Sadly all interactions with this API makes the code look like straightforward imperative code. Since we represented the game field using a matrix, we need a way to…

A small Tetris-like clone using J and ncurses. Part 1

For me, the J programming language it's a very intriguing. It is full of ideas and concepts that I'm not familiar with. Getting to know a programming language it's not only about learning the syntax. It is learning the techniques that people use to take advantage of it what gives you more insight . This is particularly true for J. For me the best way to learn more about a programming language is…

A simple language with indentation based blocks. Part 4: Improving error messages

Going back to our first post, we defined the type of a parser function as : ReaderState -> (('a * ReaderState) option ) Which means that a parser is a function from a reader state to an Option<'a> of a value and a new reader state. Using Option<'a> is handy for writing code that may result on a value or a failure indicator. In our case it's possible that the parser fails to recognize its input.…

A simple language with indentation based blocks. Part 3: Blocks

In this post we're going to add support for statements containing blocks which is the main goal of these series of posts. Identifying blocks by using indentation The technique we're going to use to identify blocks is based the following description from the (excellent) Python documentation: Before the first line of the file is read, a single zero is pushed on the stack; this will never be popped…

A simple language with indentation based blocks. Part 2: Expressions

The focus of the second part will be expression parsing. The desired grammar for the expression small language experiment is the following (here in pseudo BNF): NESTED_EXPRESSION = '(' EXPRESSION ')' PRIMARY_EXPRESSION = SYMBOL | NUMBER | STRING | NESTED UNARY_EXPRESSION = NOT_EXPRESSION | CALL_EXPRESSION | ARRAY_ACCESS_EXPRESSION | PRIMARY_EXPRESSION MULTIPLICATIVE_EXPRESSION = UNARY_EXPRESSION (…

A simple language with indentation based blocks. Part 1: Parsing combinators

In this post, I'm going to start with the implementation of the parser using the F# programming language. There are several tools for creating parsers for example FParsec http://www.quanttec.com/fparsec/ or FsYacc/FsLex https://en.wikibooks.org/wiki/F_Sharp_Programming/Lexing_and_,arsing . However for this experiment I wanted to create a small set of simple parser combinators, just to learn more…

A simple language with indentation based blocks (part 0)

One of the first things I noticed when reading about Python, is the use of indentation for defining code blocks. For example: def foo(x): while x < 20 : if x > 10 : print "argument is greater than 10" print "value: " + x else : print "the argument is less than or equal to 10" print "its value is : " + x If you're only familiar to C-based language this seems a bit strange. Not using characters like…

Solving a small primary school problem with Prolog

A couple of days ago my small son came home with math homework from school. The problem: add parenthesis to the following arithmetic expression so it makes sense. 14 * 3 - 8 / 2 = 17 When I saw that, I thought it was a nice little programming exercise. Also Prolog seems like an appropriate language to write the a solution for this problem. To solve this problem we need at least to: Choose a…

Some things I learned while creating a small program in Mercury

Some time ago I started creating a program using the Mercury programming language to create images using the Escape Time algorithm . The goal was to learn about the language by solving a small problem. The current result of this experiment can be found here https://github.com/ldfallas/graphicswithmercury/ . Here's a list of things I learned. Terms for configuration files For this program I wanted…

Determinism categories in Mercury

When you define a predicate in Mercury , you have to specify if it can fail or succeed more than once. This is called a determinism category. The category is specified as part of a predicate (or function) declaration. For example: :- pred get_partitions( list(int)::in , list(list(int))::out) is multi . The is multi section says that this predicate belongs to the multi category. The following…

A couple of quick notes on Mercury #1

I'm trying to learn about Mercury programming language . Here's a quick list of things I learned recently about it. Getting a Windows version of the Mercury compiler I was able to get a version of the Mercury compiler by downloading a "Release of the day" version from here: http://dl.mercurylang.org/index.html . I just followed the instructions from INSTALL file. The only requirement is to have a…

Using traits to reuse code in Pharo

While working on a small Tetris-like clone in Pharo , I found an opportunity to reuse some code. It's common for Tetris implementations to show a small preview of the tetrimino that comes next. Here's how it looks: I wanted to create a separate Morphic component to show this preview. But I didn't want to duplicate the code required to paint the tetriminos. Sharing this code will allow me to have…