Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Awesome list in rescript
Praveen
Praveen

Posted on • Originally published atblog.techrsr.com

     

Awesome list in rescript

Prerequisite:

  • Basic understanding of functional programming.
  • Basic knowledge on rescript.

If you have already used or tried Rescript, then you should be already familiar withlist{} in rescript.list{} is powerful but may cause performance issues if you don't understand howlist{} works exactly. So lets try to understand it by creating our own list.

Lets first define the type.

typerecmylist<'a>=Nil|Cons('a,mylist<'a>)
Enter fullscreen modeExit fullscreen mode

mylist is a variant type with 2 type constructors,Nil andCons. It also takes a generic type argument'a, so that we can store any homogenous type (elements of same type) in ourmylist.

Nil represents an emptymylist.Cons('a, mylist<'a>) takes two arguments'a is the head, representing the current value andmylist<'a> is the tail, representing the followingmylist. Now inserting multiple values into the list would look like below.

letdata=Cons(1,Cons(2,Cons(3,Cons(4,Nil))))
Enter fullscreen modeExit fullscreen mode

The above code createsmylistdata with type'a' asint and values1, 2, 3, 4. As you can see theNil type constructor denotes an empty list which inturn means that this is the end of themylist.

One awesome, cool thing about ourmylist is, we can create aninfinite list very easily. Lets create one.

letreccycle=Cons(1,cycle)
Enter fullscreen modeExit fullscreen mode

Look at therec keyword after thelet keyword. Thisrec says to the compiler that there is a recursive reference in the declaration. The above code compiles without any error. Thetail of the abovemylist is nothing but itself. So this actually refers to something like below.

Cons(1,Cons(1,Cons(1,Cons(1,Cons(1,Cons(1,Cons(1,.....)))))))
Enter fullscreen modeExit fullscreen mode

Since the tail is only a reference, there will not be an infinite loop or errors when you run the above code. So thetail always contains amylist withhead value as 1. You could ask, where could this be useful? This could be useful in cases where we want a set of values to be returned in a cyclic way.

letreccycle=Cons(1,Cons(2,Cons(3,Cons(4,cycle))))
Enter fullscreen modeExit fullscreen mode

The abovecycle will cycle through the values1, 2, 3, 4, 1, 2, 3, ... each time you access thehead upon itstail. Lets try to write the abovecycle using thelist{} in rescript.

letreccycleList=list{1,2,3,4,...cycleList}
Enter fullscreen modeExit fullscreen mode

This is same as the abovecycle. Here, it is easy to mistake the spread operator as something that is eagerly evaluated, but it is not....cycleList just places the samelist at tail position. That is the reason why you cannot do things like below.

//CANNOTdothisletreccycleList=list{...cycleList,1,2,3,4}
Enter fullscreen modeExit fullscreen mode

Here you are trying to place the list in the head position, while you can only place it in the tail position.

//CANNOTdothisletreccycleList=list{...cycleList,...cycleList}
Enter fullscreen modeExit fullscreen mode

Here you are trying to place the list in the head position as well as in the tail position, while you can only place it in the tail position.

list{} in rescript exactly works like themylist that we created and are nothing but just a syntactic sugar. To access any ith element inmylist we have to iterate from head until the ith element. That is why the Rescriptdocumentation says thatlist{} is fast at prepending items and getting the tail value but slow at everything else.

Hope you enjoyed! Happy Hacking!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a passionate full stack developer. I develop primarily using JS specifically TS. I prefer functional programming. I like exploring latest languages and frameworks.
  • Location
    India
  • Work
    Full Stack Engineer
  • Joined

More fromPraveen

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp